2
\$\begingroup\$

I'm trying to create a material during runtime like this:

  1. In my Assets I have a Render Texture and one Material
  2. I created an Script where I attach the Render Texture to Material
  3. 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!

\$\endgroup\$

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.