Skip to content

Instantly share code, notes, and snippets.

@romainPechot
Created October 9, 2015 13:05
Show Gist options
  • Save romainPechot/1a5939e5d657554d53cf to your computer and use it in GitHub Desktop.
Save romainPechot/1a5939e5d657554d53cf to your computer and use it in GitHub Desktop.
Unity EditorWindow to show how many components there is inside a gameObject hierarchy (put the script inside an "Editor" folder)
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
public class ComponentsViewer : EditorWindow
{
[MenuItem("Window/Help/GameObject/Components Viewer")]
private static void Init()
{
ComponentsViewer _window = GetWindow<ComponentsViewer>();
_window.title = "CViewer";
_window.Show();
}// Init()
private const string gameObjectTypeName = "GameObject";
private const string transformTypeName = "Transform";
private Transform root;
private bool includeInactive = false;
private bool removeGameObjectTransform = true;
private bool removeNamespace = true;
private Dictionary<string, List<Component>> components = new Dictionary<string, List<Component>>();
private List<string> keys = new List<string>();
private int nbComps = 0;
private Vector2 scrollHeight = Vector2.zero;
private void OnGUI()
{
// root
root = (Transform)EditorGUILayout.ObjectField("Root", root, typeof(Transform));
if(root == null)
{
EditorGUILayout.HelpBox("No root means it will scan ALL the current scene !", MessageType.Warning, true);
}
// inactive ?
includeInactive = EditorGUILayout.Toggle("Include inactive", includeInactive);
// remove gameobject and transform inside list
removeGameObjectTransform = EditorGUILayout.Toggle("Don't list gameobject and transform", removeGameObjectTransform);
// remove namespace
removeNamespace = EditorGUILayout.Toggle("Remove namespace", removeNamespace);
if(GUILayout.Button("Refresh Components List"))
{
FindComponents(root, includeInactive, removeGameObjectTransform, removeNamespace);
return;
}
// nb comps
EditorGUILayout.LabelField("Components : " + nbComps.ToString());
scrollHeight = EditorGUILayout.BeginScrollView(scrollHeight);
for(int i = 0; i < keys.Count; i++)
{
// key
EditorGUILayout.LabelField(" - " + keys[i] + " : " + components[keys[i]].Count.ToString());
}
EditorGUILayout.EndScrollView();
}// OnGUI()
private void FindComponents(Transform root, bool includeInactive, bool removeGameObjectTransform, bool removeNamespace)
{
Component[] comps;
if(root != null)
{
comps = (Component[])root.GetComponentsInChildren<Component>(includeInactive);
}
else
{
comps = (Component[])GameObject.FindObjectsOfType<Component>();
}
// clear list
components.Clear();
if(removeGameObjectTransform)
{
for(int i = 0; i < comps.Length; i++)
{
if(comps[i] != null)
{
string type = GetComponentName(comps[i], removeNamespace);
// NOT transform or gameobject
if(type != transformTypeName && type != gameObjectTypeName)
{
AddComponent(type, comps[i]);
}
}
}
}
else
{
for(int i = 0; i < comps.Length; i++)
{
if(comps[i] != null)
{
string type = GetComponentName(comps[i], removeNamespace);
AddComponent(type, comps[i]);
}
}
}
keys.Clear();
keys.AddRange(components.Keys);
}// FindComponents()
private string GetComponentName(Component comp, bool removeNamespace)
{
string compName = string.Empty;
if(comp != null)
{
compName = comp.GetType().ToString();
if(removeNamespace)
{
int _i_nameSpace = compName.LastIndexOf('.');
// namespace >> remove
if(_i_nameSpace >= 0)
{
compName = compName.Substring(_i_nameSpace + 1);
}
}
}
return compName;
}// GetComponentName()
private void AddComponent(string key, Component comp)
{
// key
if(!components.ContainsKey(key))
{
components.Add(key, new List<Component>(32));
}
// add entry
components[key].Add(comp);
nbComps++;
}// AddComponent()
}// ComponentsViewer : EditorWindow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment