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:
UnityWebRequest www = UnityWebRequestTexture.GetTexture(Reference.URL);
yield return Timing.WaitUntilDone(www.SendWebRequest());
if (www.isNetworkError || www.isHttpError) {
throw new Exception(www.error);
} else {
LoadedTexture = ((DownloadHandlerTexture)www.downloadHandler).texture;
LoadedTexture.name = fileName;
}
Then I save in the file system. The next time it is requested, it’s loaded this way:
byte[] fileData = File.ReadAllBytes(FilePath);
LoadedTexture = new Texture2D(2, 2);
LoadedTexture.name = fileName;
LoadedTexture.LoadImage(fileData);
I have to compress in runtime to decrease the file size:
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:
LoadedTexture.hideFlags = HideFlags.HideAndDontSave;
The code that I use to delete is:
if (LoadedTexture != null) {
UnityEngine.Object.Destroy(LoadedTexture);
LoadedTexture = null;
}
The last thing that I tried was to call this functions:
Resources.UnloadUnusedAssets();
But it still not releasing memory.
Is it a bug in Unity or am I doing something wrong?