Last active
September 19, 2024 00:06
-
-
Save thelebaron/d2b58da74b8fd186ebb41618824565e8 to your computer and use it in GitHub Desktop.
search for unity's inspector
This file contains hidden or 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.Generic; | |
using System.Linq; | |
using UnityEditor; | |
using UnityEngine; | |
namespace Junk.Utilities | |
{ | |
/// <summary> | |
/// Thanks to Ryiah and the collective stolen knowledge of the internet via chatgpt | |
/// https://forum.unity.com/threads/still-no-search-field-in-the-inspector.1555850/#post-9697247 | |
/// </summary> | |
public static class SearchInspector | |
{ | |
private static string searchString = "Search..."; | |
private static List<SerializedProperty> matchingProperties = new List<SerializedProperty>(); | |
private static Dictionary<string, SerializedObject> serializedObjectsCache = new Dictionary<string, SerializedObject>(); | |
[InitializeOnLoadMethod] | |
static void Init() | |
{ | |
UnityEditor.Editor.finishedDefaultHeaderGUI += OnDisplaySearch; | |
} | |
static void OnDisplaySearch(UnityEditor.Editor editor) | |
{ | |
var selectedGameObject = editor.target as GameObject; | |
if (selectedGameObject == null) | |
return; | |
var target = editor.target; | |
EditorGUI.BeginChangeCheck(); | |
searchString = EditorGUILayout.TextField("", searchString); | |
if (EditorGUI.EndChangeCheck()) | |
{ | |
if (!string.IsNullOrWhiteSpace(searchString)) | |
{ | |
SearchFieldsByDisplayName(target); | |
} | |
else | |
{ | |
matchingProperties.Clear(); // Clear results if search string is cleared | |
} | |
} | |
if (matchingProperties.Any()) | |
{ | |
// indent | |
++EditorGUI.indentLevel; | |
// Store the current label width | |
float currentLabelWidth = EditorGUIUtility.labelWidth; | |
// Set the desired label width | |
EditorGUIUtility.labelWidth = 200; // Adjust this value to your preference | |
GUILayout.Label("Results:", EditorStyles.boldLabel); | |
foreach (SerializedProperty property in matchingProperties) | |
{ | |
SerializedObject serializedObject = serializedObjectsCache[property.propertyPath]; | |
serializedObject.Update(); | |
EditorGUILayout.PropertyField(property, true); | |
serializedObject.ApplyModifiedProperties(); | |
} | |
--EditorGUI.indentLevel; | |
// Restore the label width | |
EditorGUIUtility.labelWidth = currentLabelWidth; | |
} | |
} | |
private static void SearchFieldsByDisplayName(Object searchTarget) | |
{ | |
matchingProperties.Clear(); | |
serializedObjectsCache.Clear(); | |
if (searchTarget == null) return; | |
Component[] components = ((GameObject)searchTarget).GetComponents<Component>(); | |
foreach (Component component in components) | |
{ | |
SerializedObject serializedObject = new SerializedObject(component); | |
SerializedProperty serializedProperty = serializedObject.GetIterator(); | |
bool enterChildren = true; | |
while (serializedProperty.NextVisible(enterChildren)) | |
{ | |
if (serializedProperty.displayName.ToLowerInvariant().Contains(searchString.ToLowerInvariant())) | |
{ | |
matchingProperties.Add(serializedProperty.Copy()); | |
serializedObjectsCache[serializedProperty.propertyPath] = serializedObject; | |
} | |
enterChildren = false; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment