Search Unity

Texture2D not destroying in Android player

Discussion in 'General Graphics' started by funbites, Nov 24, 2019.

  1. funbites

    funbites

    Joined:
    Oct 9, 2015
    Posts:
    17
    Hi,
    I built a system that downloads images from web and store them in the local storage. The game uses a lot of textures and while the player progress I have to delete old textures to make room for new ones.
    The way I implemented works fine in Unity Editor, but the Android build is crashing. I look in the profiler and realize that the textures are not been disposed. I'm using Unity 2019.2.12f1.

    I create the texture from web like this:

    Code (CSharp):
    1. UnityWebRequest www = UnityWebRequestTexture.GetTexture(Reference.URL);
    2. yield return Timing.WaitUntilDone(www.SendWebRequest());
    3. if (www.isNetworkError || www.isHttpError) {
    4.     throw new Exception(www.error);
    5. } else {
    6.     LoadedTexture = ((DownloadHandlerTexture)www.downloadHandler).texture;
    7.     LoadedTexture.name = fileName;
    8. }
    Then I save in the file system. The next time it is requested, it's loaded this way:

    Code (CSharp):
    1. byte[] fileData = File.ReadAllBytes(FilePath);
    2. LoadedTexture = new Texture2D(2, 2);
    3. LoadedTexture.name = fileName;
    4. LoadedTexture.LoadImage(fileData);
    5.  
    I have to compress in runtime to decrease the file size:

    Code (CSharp):
    1. LoadedTexture.Compress(true);
    I realize that the asset was related with the scene, so I set the hideFlag to hide and don't save. It changes where the asset is located in Profiler from Scene Assets to Not Saved, but they are also not deleted:

    Code (CSharp):
    1. LoadedTexture.hideFlags = HideFlags.HideAndDontSave;
    The code that I use to delete is:

    Code (CSharp):
    1. if (LoadedTexture != null) {
    2.     UnityEngine.Object.Destroy(LoadedTexture);
    3.     LoadedTexture = null;
    4. }
    The last thing that I tried was to call this functions:

    Code (CSharp):
    1. Resources.UnloadUnusedAssets();
    But it still not releasing memory.

    Is it a bug in Unity or am I doing something wrong?
     
    joaobsneto likes this.
  2. hemoryi

    hemoryi

    Joined:
    Mar 31, 2022
    Posts:
    4
    I also had that problem. The memory is not really freed on android