- Home /
Why does setPixel persist after exit ??
Hello,
I wrote this code to change a part of texture with another texture
// the texture that i will paint with
public Texture2D targetTexture ;
//temporary texture
public Texture2D tmpTexture, tmpMainTexture;
void Start ()
{
//setting temp texture width and height
tmpMainTexture = (Texture2D)renderer.material.mainTexture;
tmpTexture = (Texture2D)renderer.material.mainTexture;
for (int y =30; y<tmpTexture.height; y++) {
for (int x = 30; x<tmpTexture.width; x++) {
//filling the temporary texture with the target texture
tmpTexture.SetPixel (x, y, targetTexture.GetPixel (x, y));
}
}
//Apply
tmpTexture.Apply ();
//change the object main texture
renderer.material.mainTexture = tmpTexture;
}
what i don't understand is that why the texture itself (in the project hierarchy)also get changed ? i checked out the PNG file and it is intact, also when i restart the project or change the texture type (from Advanced to Texture for example), the texture get back to it's original state, any help please ?
Answer by Eric5h5 · Jan 22, 2014 at 11:18 PM
You should operate on an instance of the texture, rather than the actual texture in the project.
so ins$$anonymous$$d of renderer.material.mainTexture i use another texture ? if yes, how can i apply it to the object without using the main texture ?
this is what instance mean right ? cause the problem is not solved yet,
// the texture that i will paint with
public Texture2D targetTexture ;
//temporary texture
public Texture2D tmpTexture;
public Texture theTexture;
void Start ()
{
//setting temp texture width and height
theTexture = renderer.material.mainTexture;
tmpTexture = (Texture2D)theTexture;
for (int y =30; y<tmpTexture.height; y++) {
for (int x = 30; x<tmpTexture.width; x++) {
//filling the temporary texture with the target texture
tmpTexture.SetPixel (x, y, targetTexture.GetPixel (x, y));
}
}
//Apply
tmpTexture.Apply ();
//change the object main texture
theTexture = tmpTexture;
}
Okay i fixed it, but is this a good approach ? knowing that i will need to do this at runtime with every mouse click (looking for a painting effect)
void Start ()
{
//setting temp texture width and height
tmpTexture = new Texture2D (renderer.material.mainTexture.width, renderer.material.mainTexture.height);
//fill the new texture with the original one
for (int y =0; y<tmpTexture.height; y++) {
for (int x = 0; x<tmpTexture.width; x++) {
tmpTexture.SetPixel (x, y, originalTexture.GetPixel (x, y));
}
}
//Apply
tmpTexture.Apply ();
//filling the temporary texture with the target texturetheT
for (int y =30; y<tmpTexture.height; y++) {
for (int x = 30; x<tmpTexture.width; x++) {
tmpTexture.SetPixel (x, y, targetTexture.GetPixel (x, y));
}
}
//Apply
tmpTexture.Apply ();
//change the object main texture
renderer.material.mainTexture = tmpTexture;
}
You would only need to create the texture once. You can also just instantiate the texture like you do with other objects.
Answer by Ucuri · Jan 23, 2014 at 01:52 AM
I had the same question just yesterday, take a look here:
http://answers.unity3d.com/questions/622444/setpixel-on-a-sprite-texture-without-changing-it-g.html
I supplied some code there, I don't know if it is the most elegant solution, but I created a new texture with the same height and width and just took all the pixels from the previous texture via setPixels32().
Your answer
Follow this Question
Related Questions
Assigning UV Map to model at runtime 0 Answers
GetPixel Returning Inaccurate Color 1 Answer
Why are these two textures always the same? 1 Answer