Search Unity

Resolved Final build does not save files correctly

Discussion in 'Scripting' started by JSmithIR, May 17, 2023.

  1. JSmithIR

    JSmithIR

    Joined:
    Apr 13, 2023
    Posts:
    113
    I have a simple script that saves a material into a png file, which works perfectly in editor mode. I can press the designated key "r" to execute the script, and I see the png file appear in the destination folder. I can delete the png file, press the "r" key again, and it creates the png file again. Great. It is actually saving in two locations for my test - one is a folder on my desktop, another is in the application.persidentdata path. It works in both locations.

    I go to build the project. The FIRST time the build is executed, I test out the functionality by pressing "r" key and it works. ONCE. If i delete the png file, and re-open the standalone build, it does NOT save the png files again. It will only work the first time the build is executed. WHY in the world...

    Code (CSharp):
    1. void BakeTexture()
    2.     {
    3.         string path;
    4.         path = Application.persistentDataPath + "/Maps/" + "test.png";
    5.  
    6.  
    7.  
    8.  
    9.             string fileNum = startTime.ToString();
    10.             fileNum = fileNum.Replace(".", String.Empty);
    11.             string PNGName = "Assets/Resources/" +  "test" + fileNum + ".png";
    12.  
    13.  
    14.  
    15.             //render material to rendertexture
    16.             RenderTexture renderTexture = RenderTexture.GetTemporary(Resolution.x, Resolution.y);
    17.  
    18.             ScreenCapture.CaptureScreenshotIntoRenderTexture(renderTexture);
    19.  
    20.             //Graphics.Blit(null, renderTexture, ImageMaterial);
    21.  
    22.             //transfer image from rendertexture to texture
    23.             Texture2D texture = new Texture2D(Resolution.x, Resolution.y);
    24.             RenderTexture.active = renderTexture;
    25.             texture.ReadPixels(new Rect(Vector2.zero, Resolution), 0, 0);
    26.  
    27.             //save texture to file
    28.             byte[] png = texture.EncodeToPNG();
    29.            // byte[] png = ImageConversion.EncodeArrayToPNG(texture.GetRawTextureData(), texture.graphicsFormat, (uint)Resolution.x, (uint)Resolution.y);
    30.  
    31.         if (!Directory.Exists(Application.persistentDataPath + "/Maps"))
    32.             Directory.CreateDirectory(Application.persistentDataPath + "/Maps");
    33.  
    34.  
    35.             File.WriteAllBytes(PNGName, png);
    36.             File.WriteAllBytes(path, png);
    37.            // AssetDatabase.Refresh();
    38.  
    39.             //clean up variables
    40.             RenderTexture.active = null;
    41.             RenderTexture.ReleaseTemporary(renderTexture);
    42.             DestroyImmediate(texture);
    43.        
    44.     }
     
    Last edited: May 17, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Line 35 if executed in a build will almost certainly throw an exception.

    Look where
    PNGName
    pointed to, something like
    /Assets/Resources/etc.


    Once line 35 blows up, nothing else runs beyond that.
     
    JSmithIR likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,004
    I'm also confused as he said

    "Assets/Resources" is neither of that and does not exist in a build game. Since it's a relative path it depends on the environment path that was set when the game was run. So it may be when you click "build and run" that the environment path may still point to the project folder. Use a proper absolute path because a relative path like that most likely does not exist in your build game.
     
    JSmithIR likes this.
  4. JSmithIR

    JSmithIR

    Joined:
    Apr 13, 2023
    Posts:
    113
    Thanks guys you are likely correct, will test later today. i dont know why i said desktop
     
  5. JSmithIR

    JSmithIR

    Joined:
    Apr 13, 2023
    Posts:
    113
    Yes you two were correct, thanks for your help!
     
    Kurt-Dekker likes this.