- Home /
Changes to Texture2D persist after I stop playing
I'm modifying a Texture2D
with SetPixel()
, but when I stop playing my game the changes are still present in the scene view and the next time I play my game. I have to close Unity and restart it to have my texture reset to its original state.
How can I have my Texture2D
reset when I stop playing my game without restarting Unity?
Answer by robertbu · Sep 21, 2014 at 08:26 AM
Before you manipulate your texture, make a copy:
var tex : Texture2D = Instantiate(renderer.material.mainTexture) as Texture2D;
renderer.material.mainTexture = tex;
That worked perfectly. Thanks :) I'm surprised that unity acts like this to begin with.
Textured are stored on the GPU. The local copy is apparently being garbage collected when there is no longer a reference to it. I agree that this should be avoided by the editor without extra coding requirements.
I ran across this using a non-main texture, and solved with the following (in case anyone comes searching for this, like I did)
originalTex = my$$anonymous$$aterial.GetTexture ("_CustomTex");
...
void OnDisable() { my$$anonymous$$aterial.SetTexture("_CustomTex", originalTex); }
Your answer
Follow this Question
Related Questions
drawing on texture during runtime 0 Answers
Texture2D update every frame - The right method 4 Answers
create readable colour map 1 Answer
Fastest rendering of 2D world with lots of changes to screen every frame? 3 Answers
Fastest way to update Texture2D 0 Answers