-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScriptSearcher.cs
75 lines (65 loc) · 2.1 KB
/
ScriptSearcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
public class ScriptSearcher : EditorWindow
{
private MonoScript scriptToSearch;
private List<string> searchResults = new List<string>();
private int selectedIndex = -1; // -1 indicates no selection
[MenuItem("Tools/Taqi/Find Script On Object")]
public static void ShowWindow()
{
GetWindow<ScriptSearcher>("Script Searcher");
}
void OnGUI()
{
GUILayout.Label("Search for Script in Hierarchy", EditorStyles.boldLabel);
scriptToSearch = EditorGUILayout.ObjectField("Script", scriptToSearch, typeof(MonoScript), false) as MonoScript;
if (GUILayout.Button("Search"))
{
SearchForScriptInHierarchy();
}
if (GUILayout.Button("Clear"))
{
ClearSearchResults();
}
EditorGUI.BeginDisabledGroup(selectedIndex == -1); // Disable if no selection
if (GUILayout.Button("Copy Name"))
{
EditorGUIUtility.systemCopyBuffer = searchResults[selectedIndex];
}
EditorGUI.EndDisabledGroup();
GUILayout.Label("Search Results:");
for (int i = 0; i < searchResults.Count; i++)
{
var style = (i == selectedIndex) ? EditorStyles.boldLabel : EditorStyles.label;
if (GUILayout.Button(searchResults[i], style))
{
selectedIndex = i;
}
}
}
void SearchForScriptInHierarchy()
{
searchResults.Clear();
selectedIndex = -1; // Reset selection
if (scriptToSearch != null)
{
var scriptType = scriptToSearch.GetClass();
GameObject[] allObjects = GameObject.FindObjectsOfType<GameObject>();
foreach (GameObject obj in allObjects)
{
if (obj.GetComponent(scriptType) != null)
{
searchResults.Add(obj.name);
}
}
}
}
void ClearSearchResults()
{
searchResults.Clear();
selectedIndex = -1; // Reset selection
Repaint();
}
}