Hello Guest

Author Topic: Changing Shaders Of tk2dAnimatedSprite  (Read 4332 times)

xecut

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 11
    • View Profile
Changing Shaders Of tk2dAnimatedSprite
« on: May 09, 2013, 08:22:16 am »
Hi all,

Is it  possible to change a animated sprite's shader through code?

I tried calling  animation.renderer.material.shader = Shader.Find("myShader"); in update.
But the anmation tries to swap back to the preset shader every loop, and the sprite becomes flashing/flickering.


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Changing Shaders Of tk2dAnimatedSprite
« Reply #1 on: May 09, 2013, 09:59:06 am »
The animated sprite shares materials with the sprite collection.
If you do what you just showed there, that will keep creating duplicate materials every time you call it.

The material for the clip is stored in the sprite collection (an animation clip can contain sprites from many collections).
If you want to switch just one instance, you will have to swap it every frame. LateUpdate is probably best, but make sure to set to
1. Create a dictionary for Material, Material, mapping the source material to one of yours.
2. Every frame, check sharedMaterial, and if there is a matching swap, then swap it to the new material.
Make sure you use the dictionary, otherwise you'll keep creating duplicates.