Skip to content

Instantly share code, notes, and snippets.

@mmj-the-fighter
Forked from AngryAnt/ComponentLister.cs
Last active August 5, 2017 14:27
Show Gist options
  • Save mmj-the-fighter/f4f666143c3f792dc720 to your computer and use it in GitHub Desktop.
Save mmj-the-fighter/f4f666143c3f792dc720 to your computer and use it in GitHub Desktop.
An extension to example code for "Where did that component go?" from AngryAnt
/*
* Displays gameobjects linked to each script present in a scene, useful for development and debugging.
* Added features:
* You can search for a particular component, and turn off UnityEngine componets from displaying.
* While searching you don't have to type the entire name.
*/
using UnityEngine;
using UnityEditor;
using System.Collections;
public class ComponentLister : EditorWindow
{
private Hashtable sets = new Hashtable();
private Vector2 scrollPosition;
private string searchText = "";
private bool showEngineClasses = false;
private bool showOnlySearched = false;
[ MenuItem( "Tools/ComponentLister" ) ]
public static void Launch() {
EditorWindow window = GetWindow( typeof( ComponentLister ) );
window.title = "Components";
window.Show();
}
void OnHierarchyChange () {
UpdateList ();
Repaint ();
}
public void UpdateList()
{
Object[] objects;
sets.Clear();
objects = FindObjectsOfType( typeof( Component ) );
foreach( Component component in objects ) {
if( !sets.ContainsKey(component.GetType()) ) {
sets[ component.GetType() ] = new ArrayList();
}
(( ArrayList )sets[ component.GetType() ]).Add(component.gameObject);
}
}
public void OnGUI()
{
GUILayout.BeginHorizontal( GUI.skin.GetStyle( "Box" ) );
showEngineClasses = GUILayout.Toggle(showEngineClasses, "showEngineClasses");
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal( GUI.skin.GetStyle( "Box" ) );
showOnlySearched = GUILayout.Toggle(showOnlySearched, "showOnlySearched");
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label( "Components in scene:" );
GUILayout.FlexibleSpace();
if( GUILayout.Button("Refresh") ) {
UpdateList();
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Search");
GUI.SetNextControlName("SearchField");
searchText = GUILayout.TextField(searchText, 25);
GUILayout.EndHorizontal();
scrollPosition = GUILayout.BeginScrollView(scrollPosition);
if(!showOnlySearched) {
foreach( System.Type type in sets.Keys ) {
if(!showEngineClasses) {
string str = type.ToString();
if (str.StartsWith("UnityEngine.")) {
continue;
}
}
GUILayout.Label( type.Name + ":" );
foreach( GameObject gameObject in ( ArrayList )sets[ type ] ) {
if( GUILayout.Button( gameObject.name ) ) {
Selection.activeObject = gameObject;
}
}
}
} else {
foreach (System.Type type in sets.Keys) {
string lsearchText = searchText.ToLower();
string s = type.Name.ToString().ToLower();
if (!s.StartsWith(lsearchText))
{
continue;
}
GUILayout.Label(type.Name + ":");
foreach (GameObject gameObject in (ArrayList)sets[type]) {
if (GUILayout.Button(gameObject.name)) {
Selection.activeObject = gameObject;
}
}
}
}
GUILayout.EndScrollView();
if (Event.current.isKey) {
GUI.FocusControl ("SearchField");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment