- Home /
Change SpriteRenderers Texture2D in runtime
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[i].material.SetTexture("_MainTex",skin);
}
I also tried renderers[i].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?
try SpriteRenderer.sprite
http://docs.unity3d.com/Documentation/ScriptReference/SpriteRenderer-sprite.html
thats not what I want, I want the sprite renderers to use the UV coords defined in the import settings, but change the Texture2D they use.
I think thats not the way its done to change the skin of a 3d model. Besides, with this approach I could easily add new skins, without major changes nor editing the textures I already have. It would be so easy if I could change the texture in the material and reuse the UV coords in each quad...
I'm using a 2D character and I'm using SpriteRenderers and Sprites. But it works in the exact same way as 3d because the SpriteRenderer renders a quad, and the quad has UV coords.
Gnp89, please just ignore Invertex. He obviously does not understand what you're talking about. He should just be ignored. It was an interesting read, as any outside person can clearly see Invertex was wrong from start to finish, and had no idea what you were trying to do. He didn't even seem to understand anything you said, at any time you said it.
If he didn't insist so much that he was right, and didn't have some sort of odd view that he HAS to be right, then he could have understood. Ins$$anonymous$$d, he chose to ignore everything you said, never attempted communication, and just wanted to rant on a soap box. I see that attitude all the time. Humility is not common on the internet.
Answer by gnp89 · Jan 14, 2014 at 01:40 PM
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[i].SetPropertyBlock(block);
}
Now it works :D
Ingenious solution, sadly it only kind of worked for me :|.
Basically, it swaps the sprites on load, for the visible sprites, but, when I move the character (and as a result) transition between animator states or swap to new poses (e.g. left, right, forward, backward) the old "original" sprites are loaded. I hacked for a while and came up with a solution that does seem to work. Feel free to use it.
http://2tonstudios.com/ideas.htm#Skins2DSprites
Cheers!
$$anonymous$$y suggested solution of course doesn't work because AssetDatabase is part of UnityEditor, which not available in any build outside the editor... :|.
Okay, hacked a bit more. Came up with a true solution. For commentary see my website http://2tonstudios.com/ideas.htm
// new stuff
using System;
// standard
using UnityEngine;
using System.Collections;
public class SkinController : $$anonymous$$onoBehaviour
{
public string activeSkin;
// Use this for initialization
void Awake ()
{
// default loaded sprites
SpriteRenderer[] loadedRenderers = GetComponentsInChildren <SpriteRenderer>(true);
Texture2D atlas = Resources.Load (activeSkin, typeof(Texture2D)) as Texture2D;
for (int i = 0; i < loadedRenderers.Length; i++)
{
try
{
// replace the current sprite with the desired sprite, but using the loaded sprite as a cut out reference via 'rect'
loadedRenderers[i].sprite = Sprite.Create (atlas, loadedRenderers[i].sprite.rect, new Vector2 (0.5f, 0.5f));
// update name, main texture and shader, these all seem to be required... even thou you'd think it already has a shader :|
loadedRenderers[i].sprite.name = loadedRenderers[i].name + "_sprite";
loadedRenderers[i].material.mainTexture = atlas as Texture;
loadedRenderers[i].material.shader = Shader.Find ("Sprites/Default");
}
catch (Exception e) {}
}
}
}
Oh I hadn't noticed that because my characters just had a walking animation and there were no transitions. Perfect!
Hey thanks for this answer, spent way too long searching for it.You can avoid having to create a new sprite every time by creating it with a Sprite$$anonymous$$eshType.FullRect.