Hi,
I'm trying to do some color replacement for player customization purpose.
I want the player to be able to choose the color of his avatar. Imagine a sprite of a football player, I want to change the shirt color only.
I've been looking at some "green screen" technique, where the initial shirt color would be of a color I don't use (full green, or full pink), and then use Unity3D GetPixels / SetPixels to replace only pixels of this specific color with the one chosen by players.
It works fine with static sprites. But I wonder how to perform such replacement with sprite animation?
Also, if you have any better idea on how to perform such color replacement, please share it
Here is the code I use so far :
private void Awake()
{
Texture2D tex = Instantiate(renderer.material.mainTexture) as Texture2D; // I guess it's because animator doesn't use mainTexture.
renderer.material.mainTexture = tex;
Color search = new Color(1.0f, 0.0f, 1.0f); // looking for pink color
Color replace = new Color(Random.value, Random.value, Random.value); // changing it by a random color
for (int y = 0; y < tex.height; ++y)
{
for (int x = 0; x < tex.width; ++x)
{
if (tex.GetPixel(x, y) == search)
{
tex.SetPixel(x, y, replace);
}
}
}
tex.Apply();
}
Thanks for your help