I'm trying to create a material during runtime like this:
- In my Assets I have a Render Texture and one Material
- I created an Script where I attach the Render Texture to Material
I save in my project a PNG Image File for later use
public void CreateAndSaveMaterial(){ RenderTexture.active = _renderTexture; Texture2D tex = new Texture2D(_renderTexture.width, _renderTexture.height, TextureFormat.ARGB32, false); tex.ReadPixels(new Rect(0, 0, _renderTexture.width, _renderTexture.height), 0, 0); tex.Apply(); File.WriteAllBytes(Application.dataPath + url, tex.EncodeToPNG()); tempObject.mainTexture = tex; RenderTexture.active = null;}
*The PROBLEM:
1.The Material works in rutime, after quit playing, the Material resets :( 2.The created PNG File not working only if the file is reimported in Unity, I tried with:
AssetDatabase.CreateAsset(obj, fileName);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
But unsuccessfully, works only in Editor not in build app :(
Any suggestions? Thanks in advance!