-
-
Save frarees/5052637 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reflection; | |
public class ComponentLister : EditorWindow { | |
delegate string ToolbarSearchFieldDelegate (string text, string[] searchModes, ref int searchMode, params GUILayoutOption[] options); | |
ToolbarSearchFieldDelegate ToolbarSearchField; | |
Hashtable sets; | |
Vector2 scrollPosition; | |
string searchString; | |
string[] searchFilters; | |
Type[] searchParametersTypes; | |
int searchMode; | |
T ExposeStatic<T> (Type type, string methodName, Type[] types = null) where T : class { | |
MethodInfo method; | |
if (types != null) | |
method = type.GetMethod (methodName, BindingFlags.NonPublic | BindingFlags.Static, null, types, null); | |
else | |
method = type.GetMethod (methodName, BindingFlags.NonPublic | BindingFlags.Static); | |
return Delegate.CreateDelegate (typeof (T), method) as T; | |
} | |
[MenuItem ("Component/Component Lister")] | |
static void Launch () { | |
EditorWindow window = GetWindow (typeof (ComponentLister)); | |
window.title = "Components"; | |
window.Show (); | |
} | |
void UpdateList () { | |
UnityEngine.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); | |
} | |
} | |
void Awake () { | |
searchParametersTypes = new Type[] {typeof (string), typeof (string[]), typeof (int).MakeByRefType (), typeof (GUILayoutOption[])}; | |
ToolbarSearchField = ExposeStatic<ToolbarSearchFieldDelegate> (typeof (EditorGUILayout), "ToolbarSearchField", searchParametersTypes); | |
sets = new Hashtable (); | |
searchString = ""; | |
searchFilters = new string[] { "Component", "GameObject" }; | |
UpdateList (); | |
} | |
void OnEnable () { | |
searchMode = EditorPrefs.GetInt ("ComponentLister.SearchMode", 0); | |
} | |
void OnDisable () { | |
EditorPrefs.SetInt ("ComponentLister.SearchMode", searchMode); | |
} | |
void OnDestroy () { | |
sets = null; | |
searchString = null; | |
searchFilters = null; | |
searchParametersTypes = null; | |
} | |
void OnHierarchyChange () { | |
UpdateList (); | |
Repaint (); | |
} | |
void OnGUI () { | |
GUILayout.BeginHorizontal (EditorStyles.toolbar); | |
GUILayout.FlexibleSpace (); | |
GUI.SetNextControlName ("SearchField"); | |
//searchString = GUILayout.TextField (searchString, EditorStyles.toolbarTextField); | |
searchString = ToolbarSearchField (searchString, searchFilters, ref searchMode, new GUILayoutOption [0]) as string; | |
GUILayout.EndHorizontal (); | |
/* | |
GUILayout.BeginHorizontal (GUI.skin.GetStyle ("Box")); | |
GUILayout.Label ("Components in scene: "); | |
GUILayout.FlexibleSpace (); | |
if (GUILayout.Button ("Refresh")) | |
UpdateList (); | |
GUILayout.EndHorizontal (); | |
*/ | |
scrollPosition = GUILayout.BeginScrollView (scrollPosition); | |
switch (searchMode) { | |
case 1: { | |
foreach (Type type in sets.Keys) { | |
List<GameObject> filtered = ((ArrayList)sets[type]).Cast<GameObject> ().Where (go => go.name.ToLower ().Contains (searchString.ToLower ())).ToList (); | |
if (filtered.Count == 0) | |
continue; | |
GUILayout.Label (type.Name + ":"); | |
foreach (GameObject gameObject in filtered) { | |
if (GUILayout.Button (gameObject.name)) { | |
EditorGUIUtility.PingObject (Selection.activeObject = gameObject); | |
} | |
} | |
} | |
} | |
break; | |
case 0: { | |
foreach (Type type in sets.Keys.Cast<Type> ().Where (t => t.Name.ToLower ().Contains (searchString.ToLower ()))) { | |
GUILayout.Label (type.Name + ":"); | |
foreach (GameObject gameObject in (ArrayList)sets[type]) { | |
if (GUILayout.Button (gameObject.name)) { | |
EditorGUIUtility.PingObject (Selection.activeObject = gameObject); | |
} | |
} | |
} | |
} | |
break; | |
default: { | |
Debug.LogError ("Filtering by " + searchFilters[searchMode] + " isn't implemented"); | |
searchMode = 0; | |
} | |
break; | |
} | |
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