SetPixel and cloning sprite.texture
Hello,
nooby question here. I have an object with a sprite render with a Sprite assigned to it and a simple script.
I was able to color the pixels of the sprite by doing:
SpriteRenderer rend = gameObject.GetComponent<Renderer>() as SpriteRenderer;
Texture2D tex = rend.sprite.texture;
tex.SetPixel(x, y, Color.smthng)
This though effectivelly changed my image permanently when I play the game. So I am trying to clone sprite.texture and then assign it. Code below is indicative, value are just made up.
void Start()
{
SpriteRenderer rend = gameObject.GetComponent<Renderer>() as SpriteRenderer;
Bounds b =rend.sprite.bounds;
Texture2D tex = rend.sprite.texture;
Texture2D clone = Instantiate(tex);
rend.material.mainTexture = clone;
for (int y = 500; y < tex.height; y++)
{
for (int x = 700; x < tex.width; x++)
{
Color color = Color.blue; // ((x & y) != 0 ? Color.white : Color.gray);
clone.SetPixel(x, y, color);
break;
}
}
clone.Apply();
}
Question 1: why do I have to use rend.material.mainTexture to assign the clone? sprite.texture=clone would feel more natural to me but it's read only. Question 2: why I dont see the clone rendered?
Thanks a lot!
Your answer
Follow this Question
Related Questions
2D Sprite.Create isn't working 0 Answers
Texture not centered in sprite 0 Answers
How best to implement a tileable ground. 0 Answers
Question for @Mavina , What Program? 1 Answer
TextureImporterType.Sprite does not work 0 Answers