2D Toolkit Forum

2D Toolkit => Support => Topic started by: spudubi on January 16, 2014, 11:17:12 am

Title: Change 2dTk shader of sprite at runtime
Post by: spudubi on January 16, 2014, 11:17:12 am
Hi,

My Sprite Collection uses a material with the 2dTk "BlendVertexColor" shader. I'm creating sprites from that collection at runtime like this:

Code: [Select]
tk2dSpriteCollectionData spriteCollection = Resources.Load("HexTilesData", typeof(tk2dSpriteCollectionData)) as tk2dSpriteCollectionData;
tk2dSprite tileSprite = tk2dSprite.AddComponent(goTemp,spriteCollection, "hexBorder");

Now I'm trying to change the shader of one sprite at runtime to the 2dTk shader "AdditiveVertexColor" like this:

Code: [Select]
selectedHexTile.sprite.renderer.material.shader = Shader.Find("AdditiveVertexColor");
But it doesn't work. The Sprite will be rendered in solid pink  :). I guess that's because Unity can't find the "AdditiveVertexColor" shader. I tried to put the shader in a Resources folder but that didn't worked either.

What is the right way to change the shader?
Title: Re: Change 2dTk shader of sprite at runtime
Post by: unikronsoftware on January 16, 2014, 11:39:41 am
1. it needs to be in resources
2. the name is "tk2d/AdditiveVertexColor"
Title: Re: Change 2dTk shader of sprite at runtime
Post by: spudubi on January 16, 2014, 11:49:17 am
Wow, thanks for your fast reply!

Now it works!

Is this method the best method (performance wise) to change the render style of a sprite to additive rendering during runtime?
Title: Re: Change 2dTk shader of sprite at runtime
Post by: unikronsoftware on January 16, 2014, 04:25:13 pm
Probably? There aren't any other ways of changing to additive when rendering as far as I can tell.