Hello Guest

Author Topic: different materials on sprite prefab  (Read 6442 times)

vevusio

  • Newbie
  • *
  • Posts: 4
    • View Profile
different materials on sprite prefab
« on: November 02, 2013, 05:05:30 pm »
i want to create a prefab for a sprite, and then change the material on the game objects using that prefab (i.e. different shaders etc.)

currently when i set a different material on a game object using the sprite component in the inspector, for example change the default shader to litvertexblend, the material changes correctly, however upon clicking play the material is reverted to the original material associated with the sprite in the collection

ofc i could create 2 sprites with the same image with different materials, but then i would have to create x bounding boxes, x animations, etc. per x different materials

so how can i override the default material and make it stick?

vevusio

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: different materials on sprite prefab
« Reply #1 on: November 04, 2013, 07:24:37 pm »
is no one having this use case? applying different shaders to sprites and animations to get different visuals without recreating every single sprite and sprite-animation per-material?!

i'd like to know if i am totally missing or misunderstanding some core concept here, if this can't be done, and you in fact have to do that, then it would be nice to know for sure at least

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: different materials on sprite prefab
« Reply #2 on: November 05, 2013, 11:49:21 am »
The reason you can't update the material per instance, is that each animated sprite can consist of multiple materials. What happens is that the tk2dSprite will assign the sprites assigned material, whenever the sprite changes.

if you'd like to assign different materials, you can change this behaviour in tk2dSprite.cs, UpdateMaterial, to not assign a material if its not null.
Code: [Select]
if (renderer.sharedMaterial != collectionInst.spriteDefinitions[spriteId].materialInst)
renderer.material = collectionInst.spriteDefinitions[spriteId].materialInst;
to

Code: [Select]
if (renderer.sharedMaterial == null)
renderer.material = collectionInst.spriteDefinitions[spriteId].materialInst;
but remember that doing that means you'l have to clear out the material if you do want it to change.

Alternatively, you can write a script to deal with this - tk2dBaseSprite.SpriteChanged is called whenever the sprite is changed, and you can assign your version of the material for that sprite there.