Last active
December 6, 2023 08:05
-
-
Save beardordie/af823160eba097af2b48af4141d37aa5 to your computer and use it in GitHub Desktop.
Custom Game Object Inspector - filter the Inspector to find a property or component, made by Mattimus in the More Mountains Community Discord (updated to work with Unity 2022.3)
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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor; | |
using System; | |
using System.Reflection; | |
[CustomEditor(typeof(GameObject), true)] | |
[CanEditMultipleObjects] | |
public class CustomGameObjectInspector : Editor | |
{ | |
//Unity's built-in editor | |
Editor defaultEditor; | |
GameObject gameObject; | |
protected string searchText = ""; | |
void OnEnable() | |
{ | |
//When this inspector is created, also create the built-in inspector | |
defaultEditor = Editor.CreateEditor(targets, Type.GetType("UnityEditor.GameObjectInspector, UnityEditor")); | |
gameObject = target as GameObject; | |
defaultEditor.GetType().GetMethod("OnEnable").Invoke(defaultEditor, null); | |
} | |
void OnDisable() | |
{ | |
//When OnDisable is called, the default editor we created should be destroyed to avoid memory leakage. | |
//Also, make sure to call any required methods like OnDisable | |
DestroyImmediate(defaultEditor); | |
} | |
public override void OnInspectorGUI() | |
{ | |
defaultEditor.OnInspectorGUI(); | |
defaultEditor.DrawHeader(); | |
EditorGUI.BeginChangeCheck(); | |
EditorGUILayout.BeginHorizontal(); | |
GUI.SetNextControlName("textField"); | |
EditorGUILayout.LabelField("Find:", GUILayout.Width(50)); | |
searchText = EditorGUILayout.TextField(searchText); | |
//Clear button. | |
GUI.SetNextControlName("Button"); | |
if (GUILayout.Button("X", GUILayout.Width(30))) | |
{ | |
searchText = ""; | |
GUI.FocusControl("Button"); | |
SceneView.RepaintAll(); | |
} | |
EditorGUILayout.EndHorizontal(); | |
if (EditorGUI.EndChangeCheck()) | |
{ | |
FilterProperties(); | |
} | |
} | |
//filters by property | |
void FilterProperties() | |
{ | |
//find all the components on all the game objects | |
foreach (var t in targets) | |
{ | |
foreach (Component component in ((GameObject)t).GetComponents<Component>()) | |
{ | |
//don't filter out Transform or GameObject. | |
if (component.GetType() == typeof(GameObject) || component.GetType() == typeof(Transform)) | |
{ | |
continue; | |
} | |
bool hasProperty = false; | |
FieldInfo[] fields = component.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); //get all the fields on this component | |
foreach (FieldInfo fI in fields) //does this component have a field that matches the query | |
{ | |
if (fI.Name.ToLower().Contains(searchText.ToLower())) | |
{ | |
hasProperty = true; | |
break; | |
} | |
} | |
//hide properties that don't meet the search requirements. | |
if (hasProperty || component.GetType().ToString().ToLower().Contains(searchText.ToLower())) | |
{ | |
component.hideFlags = component.hideFlags & ~HideFlags.HideInInspector; | |
} | |
else | |
{ | |
component.hideFlags = component.hideFlags | HideFlags.HideInInspector; | |
} | |
} | |
} | |
foreach (var t in targets) | |
{ | |
EditorUtility.SetDirty(t); | |
} | |
Repaint(); | |
} | |
} |
Author
beardordie
commented
Oct 8, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment