I am really close to releasing my current game, but I cannot seem to find the source of this current leak which is plaguing my entire game.
It appears that everything that is in the Menu scene of my game is leaked over in memory to any other scene in my game. I have followed what other people have suggested on these forums, by creating a loading scene which calls Resources.UnloadUnusedAssets, before loading the next scene, but this appears to not help in the least.
I am using this code to check for my leaks:
using UnityEngine;
using System.Collections;
public class DetectLeaks : MonoBehaviour {
void OnGUI () {
GUILayout.Label("All " + Resources.FindObjectsOfTypeAll(typeof(UnityEngine.Object)).Length);
GUILayout.Label("Textures " + Resources.FindObjectsOfTypeAll(typeof(Texture)).Length);
GUILayout.Label("AudioClips " + Resources.FindObjectsOfTypeAll(typeof(AudioClip)).Length);
GUILayout.Label("Meshes " + Resources.FindObjectsOfTypeAll(typeof(Mesh)).Length);
GUILayout.Label("Materials " + Resources.FindObjectsOfTypeAll(typeof(Material)).Length);
GUILayout.Label("GameObjects " + Resources.FindObjectsOfTypeAll(typeof(GameObject)).Length);
GUILayout.Label("Components " + Resources.FindObjectsOfTypeAll(typeof(Component)).Length);
if(GUILayout.Button("Unload Unused Assets"))
{
Resources.UnloadUnusedAssets();
print("UNLOADING");
}
if(GUILayout.Button("Print Components"))
{
foreach ( Component component in Resources.FindObjectsOfTypeAll(typeof(Component)) )
{
print("COMPONENT: " + component.name);
}
}
if(GUILayout.Button("Print Textures"))
{
foreach ( Texture text in Resources.FindObjectsOfTypeAll(typeof(Texture)) )
{
print("TEXTURE: " + text.name);
}
}
if(GUILayout.Button("Print AudioClips"))
{
foreach ( AudioClip audio in Resources.FindObjectsOfTypeAll(typeof(AudioClip)) )
{
print("Audio: " + audio.name);
}
}
}
}
As you can see I have 3 buttons that will print out resources that are still loaded in to memory. I am currently having the menu automatically load into an empty scene with just that script on the camera. Once in the empty scene by tapping printing out all of the textures, audio clips and components that are currently loaded, I can see that pretty much everything in the previous scene is still loaded in to memory.
I do not have DontDestroyOnLoad() on any scripts, and any case in which I use a static singleton variable, I set to null in OnDestroy.
I am at a loss for what else could be causing this, but it is absolutely killing my memory usage on devices. Does anyone know why these items would all still be getting left in memory?