I created a character using unity 2d tools. I sliced a Texture2D to create the sprites.
Now I want him to change skins during runtime. So I have two Texture2D with differences in its clothes but the same UV coords.
I thought that changing the spriteRenderers material _MainTex would do it, but it keeps using the original Sprites.
SpriteRenderer[] renderers = GetComponentsInChildren<SpriteRenderer>();
if(use skin 1){
skin = texture2D_1;
}else{
skin = texture2D_2;
}
for (int i = 0; i < renderers.Length; i++) {
renderers*.material.SetTexture("_MainTex",skin);*
} I also tried renderers*.material.mainTexture = skin;* And I tried with sharedMaterial. I would like to change the Texture2d from where Sprites are taken, in runtime of course. How can I do this?
Found the answer reading the Sprites/Default shader.
The _MainTex property has the [PerRendererData] attribute which means the texture is provided by the renderers MaterialPropertyBlock and not by the material.
So instead of setting the materials texture, I had to set a new property block to the renderer like this:
SpriteRenderer[] renderers = GetComponentsInChildren<SpriteRenderer>();
if(use skin 1){
skin = texture2D_1;
}else{
skin = texture2D_2;
}
for (int i = 0; i < renderers.Length; i++) {
MaterialPropertyBlock block = new MaterialPropertyBlock();
block.AddTexture("_MainTex",skin);
renderers*.SetPropertyBlock(block);*
} Now it works [20640-screen+shot+2014-01-14+at+11.36.30+am.png|20640]* [20642-screen+shot+2014-01-14+at+11.38.47+am.png|20642]*
*
*