1
\$\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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.