- Home /
Resetting a Material's Texture After GetPixels/SetPixels
I have a texture, renderer.material.mainTexture that I'm painting with GetPixels and SetPixels at runtime. So, I start playback and paint the texture. How do I set this texture back to its original, unpainted state when I stop playback in the editor and start playback again? Right now the painted pixels carry over from session to session. The only way I can reset the texture is to go into the project inspector, select the texture image, and select reset. Also, what is the intermediate state that the texture is stored in for this to happen? I'm used to authoring programs where you have original, saved state, and then the rendered state which only occurs at runtime.
Answer by ckfinite · Apr 24, 2011 at 03:23 PM
Why this is happening is related to the difference between value types and reference types. When you access renderer.material.mainTexture, you are getting a reference to that object, and when you alter it, you alter the referenced object. What you might be able to do is store a copy of the texture in the script that alters it, then set the mainTexture to that.
Thanks. I had previously tried copying the main texture and editing it, but had the same results. Tried it again after your suggestion and it seems to work as expected. I'm still curious though as to why main texture doesn't reset to the version that's in the file when you stop and start playback.
Your answer
Follow this Question
Related Questions
Scramble GetPixels function... 1 Answer
Merging Textures At Runtime - Not Correct? 1 Answer
Changing color of Texture2d with transparency 1 Answer
Painting stencil on a surface. 6 Answers
Setting pixels on a texture? 2 Answers