DetectLeaks
From Unify Community Wiki
Author: Joachim Ante
Description
This script will displays the number of alloctated unity objects by type. This is useful for finding leaks. Knowing the type of object (mesh, texture, sound clip, game object) that is getting leaked is the first step. You could then print the names of all leaked assets of that type.
C# - DetectLeaks
The script _must_ be named DetectLeaks.cs
using UnityEngine; using System.Collections; public class DetectLeaks : MonoBehaviour { void OnGUI () { GUILayout.Label("All " + FindObjectsOfTypeAll(typeof(UnityEngine.Object)).Length); GUILayout.Label("Textures " + FindObjectsOfTypeAll(typeof(Texture)).Length); GUILayout.Label("AudioClips " + FindObjectsOfTypeAll(typeof(AudioClip)).Length); GUILayout.Label("Meshes " + FindObjectsOfTypeAll(typeof(Mesh)).Length); GUILayout.Label("Materials " + FindObjectsOfTypeAll(typeof(Material)).Length); GUILayout.Label("GameObjects " + FindObjectsOfTypeAll(typeof(GameObject)).Length); GUILayout.Label("Components " + FindObjectsOfTypeAll(typeof(Component)).Length); } }
FindObjectsOfTypeAll is obsolete. New version from Berenger on 07/03/2012 (Unity 3.5)
using UnityEngine; using System.Collections; public class DetectLeaks : MonoBehaviour { void OnGUI () { GUILayout.Label("All " + FindObjectsOfType(typeof(UnityEngine.Object)).Length); GUILayout.Label("Textures " + FindObjectsOfType(typeof(Texture)).Length); GUILayout.Label("AudioClips " + FindObjectsOfType(typeof(AudioClip)).Length); GUILayout.Label("Meshes " + FindObjectsOfType(typeof(Mesh)).Length); GUILayout.Label("Materials " + FindObjectsOfType(typeof(Material)).Length); GUILayout.Label("GameObjects " + FindObjectsOfType(typeof(GameObject)).Length); GUILayout.Label("Components " + FindObjectsOfType(typeof(Component)).Length); } }
The upper one isnt working correctly... Here is the actual Solution -> http://unity3d.com/support/documentation/ScriptReference/Resources.FindObjectsOfTypeAll
using UnityEngine; using System.Collections; using System.Collections.Generic; public class DetectLeaks : MonoBehaviour { void OnGUI() { Object[] objects = FindObjectsOfType(typeof (UnityEngine.Object)); Dictionary<string, int> dictionary = new Dictionary<string, int>(); foreach(Object obj in objects) { string key = obj.GetType().ToString(); if(dictionary.ContainsKey(key)) { dictionary[key]++; } else { dictionary[key] = 1; } } List<KeyValuePair<string, int>> myList = new List<KeyValuePair<string, int>>(dictionary); myList.Sort( delegate(KeyValuePair<string, int> firstPair, KeyValuePair<string, int> nextPair) { return nextPair.Value.CompareTo((firstPair.Value)); } ); foreach (KeyValuePair<string, int> entry in myList) { GUILayout.Label(entry.Key + ": " + entry.Value); } } }
Slightly modified version to show every type of object and ordered (descending) by count. -Juha