Skip to content

Instantly share code, notes, and snippets.

@AkhmadMax
Created July 1, 2014 11:28
Show Gist options
  • Save AkhmadMax/4967663c71de674bb8a1 to your computer and use it in GitHub Desktop.
Save AkhmadMax/4967663c71de674bb8a1 to your computer and use it in GitHub Desktop.
Script helps to find if Unity leaves any assets in memory, what causes overflow. Usually it happens after creating assets in runtime.
// Script helps to find if Unity leaves any assets in memory, what causes overflow.
// Usually it happens after creating assets in runtime.
using UnityEngine;
public class DetectLeaks : MonoBehaviour
{
void OnGUI()
{
if (GUILayout.Button("Unload Unused Assets"))
{
Resources.UnloadUnusedAssets();
}
if (GUILayout.Button("Mono Garbage Collect"))
{
System.GC.Collect();
}
if (GUILayout.Button("List Loaded Textures"))
{
ListLoadedTextures();
}
//if (GUILayout.Button("List Loaded Skins"))
//{
// ListLoadedSkins();
//}
//if (GUILayout.Button("List Loaded Styles"))
//{
// ListLoadedStyles();
//}
if (GUILayout.Button("List Loaded Sounds"))
{
ListLoadedAudio();
}
if (GUILayout.Button("List Loaded GameObjects"))
{
ListLoadedGameObjects();
}
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);
}
public void Init()
{
}
void Start()
{
DontDestroyOnLoad(gameObject);
}
private void ListLoadedTextures()
{
Object[] textures = Resources.FindObjectsOfTypeAll(typeof(Texture));
string list = string.Empty;
for (int i = 0; i < textures.Length; i++)
{
if (textures[i].name == string.Empty)
{
continue;
}
list += (i.ToString() + ". " + textures[i].name + "\n");
if (i == 500)
{
Debug.Log(list);
list = string.Empty;
}
}
Debug.Log(list);
}
private void ListLoadedSkins()
{
Object[] skins = Resources.FindObjectsOfTypeAll(typeof(GUISkin));
string list = string.Empty;
for (int i = 0; i < skins.Length; i++)
{
if (skins[i].name == string.Empty)
{
continue;
}
list += (i.ToString() + ". " + skins[i].name + "\n");
if (i == 500)
{
Debug.Log(list);
list = string.Empty;
}
}
Debug.Log(list);
}
private void ListLoadedStyles()
{
Object[] styles = Resources.FindObjectsOfTypeAll(typeof(GUIStyle));
string list = string.Empty;
for (int i = 0; i < styles.Length; i++)
{
if (styles[i].name == string.Empty)
{
continue;
}
list += (i.ToString() + ". " + styles[i].name + "\n");
if (i == 500)
{
Debug.Log(list);
list = string.Empty;
}
}
Debug.Log(list);
}
private void ListLoadedAudio()
{
Object[] sounds = Resources.FindObjectsOfTypeAll(typeof(AudioClip));
string list = string.Empty;
for (int i = 0; i < sounds.Length; i++)
{
if (sounds[i].name == string.Empty)
{
continue;
}
list += (i.ToString() + ". " + sounds[i].name + "\n");
}
Debug.Log(list);
}
private void ListLoadedGameObjects()
{
Object[] gos = Resources.FindObjectsOfTypeAll(typeof(GameObject));
string list = string.Empty;
for (int i = 0; i < gos.Length; i++)
{
if (gos[i].name == string.Empty)
{
continue;
}
list += (i.ToString() + ". " + gos[i].name + "\n");
}
Debug.Log(list);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment