Skip to content

Commit 3e2d55b

Browse files
authored
Create FindWhatButtonCallsMyMethod.cs
1 parent 4532dd6 commit 3e2d55b

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// prints out which buttons in current scene are referencing your given method
2+
3+
using UnityEngine;
4+
using UnityEngine.UI;
5+
using UnityEditor;
6+
using UnityEngine.Events;
7+
using System.Reflection;
8+
9+
namespace UnityLibrary
10+
{
11+
public class FindWhatButtonCallsMyMethod : EditorWindow
12+
{
13+
private string methodName = "MyMethodHere";
14+
15+
[MenuItem("Tools/Find Buttons with Method")]
16+
public static void ShowWindow()
17+
{
18+
GetWindow<FindWhatButtonCallsMyMethod>("FindWhatButtonCallsMyMethod");
19+
}
20+
21+
private void OnGUI()
22+
{
23+
GUILayout.Label("Find Buttons that call Method", EditorStyles.boldLabel);
24+
methodName = EditorGUILayout.TextField("Method Name:", methodName);
25+
26+
if (GUILayout.Button("Find Buttons"))
27+
{
28+
FindButtonsWithMethod();
29+
}
30+
}
31+
32+
private void FindButtonsWithMethod()
33+
{
34+
Button[] allButtons = FindObjectsOfType<Button>();
35+
36+
foreach (var button in allButtons)
37+
{
38+
UnityEvent clickEvent = button.onClick;
39+
40+
for (int i = 0; i < clickEvent.GetPersistentEventCount(); i++)
41+
{
42+
Object target = clickEvent.GetPersistentTarget(i);
43+
string method = clickEvent.GetPersistentMethodName(i);
44+
45+
if (method == methodName)
46+
{
47+
MethodInfo methodInfo = target.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
48+
if (methodInfo != null)
49+
{
50+
ParameterInfo[] parameters = methodInfo.GetParameters();
51+
52+
// Check if the method has arguments
53+
if (parameters.Length > 0)
54+
{
55+
Debug.Log($"Button '{button.gameObject.name}' calls method '{methodName}' with parameters on object '{target.name}'", button.gameObject);
56+
}
57+
else
58+
{
59+
Debug.Log($"Button '{button.gameObject.name}' calls method '{methodName}' with no parameters on object '{target.name}'", button.gameObject);
60+
}
61+
}
62+
}
63+
}
64+
}
65+
66+
Debug.Log("Search Complete");
67+
}
68+
} // class
69+
} // namespace

0 commit comments

Comments
 (0)