|
| 1 | +// finds what scripts reference a given GameObject in the scene (in events, public fields..) |
| 2 | + |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Reflection; |
| 5 | +using UnityEditor; |
| 6 | +using UnityEngine; |
| 7 | +using UnityEngine.Events; |
| 8 | + |
| 9 | +namespace UnityLibrary.Editor |
| 10 | +{ |
| 11 | + public class FindWhoReferencesThisGameObject : EditorWindow |
| 12 | + { |
| 13 | + private GameObject target; |
| 14 | + private Vector2 scroll; |
| 15 | + |
| 16 | + private class ReferenceResult |
| 17 | + { |
| 18 | + public string message; |
| 19 | + public GameObject owner; |
| 20 | + } |
| 21 | + |
| 22 | + private List<ReferenceResult> results = new List<ReferenceResult>(); |
| 23 | + |
| 24 | + [MenuItem("Tools/UnityLibrary/Find References To GameObject")] |
| 25 | + public static void ShowWindow() |
| 26 | + { |
| 27 | + var win = GetWindow<FindWhoReferencesThisGameObject>("Find References"); |
| 28 | + win.minSize = new Vector2(500, 300); |
| 29 | + } |
| 30 | + |
| 31 | + private void OnGUI() |
| 32 | + { |
| 33 | + GUILayout.Label("Find scripts that reference this GameObject", EditorStyles.boldLabel); |
| 34 | + target = EditorGUILayout.ObjectField("Target GameObject", target, typeof(GameObject), true) as GameObject; |
| 35 | + |
| 36 | + if (GUILayout.Button("Find References")) |
| 37 | + { |
| 38 | + results.Clear(); |
| 39 | + if (target != null) |
| 40 | + { |
| 41 | + FindReferences(target); |
| 42 | + } |
| 43 | + else |
| 44 | + { |
| 45 | + Debug.LogWarning("Please assign a GameObject."); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + if (results.Count > 0) |
| 50 | + { |
| 51 | + GUILayout.Label("Results:", EditorStyles.boldLabel); |
| 52 | + scroll = GUILayout.BeginScrollView(scroll, GUILayout.Height(400)); |
| 53 | + foreach (var res in results) |
| 54 | + { |
| 55 | + if (GUILayout.Button(res.message, GUILayout.ExpandWidth(true))) |
| 56 | + { |
| 57 | + EditorGUIUtility.PingObject(res.owner); |
| 58 | + Selection.activeGameObject = res.owner; |
| 59 | + } |
| 60 | + } |
| 61 | + GUILayout.EndScrollView(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private void FindReferences(GameObject target) |
| 66 | + { |
| 67 | + var allObjects = UnityEngine.Object.FindObjectsOfType<MonoBehaviour>(true); |
| 68 | + |
| 69 | + foreach (var mono in allObjects) |
| 70 | + { |
| 71 | + if (mono == null || mono.gameObject == target) continue; |
| 72 | + |
| 73 | + var type = mono.GetType(); |
| 74 | + var fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); |
| 75 | + |
| 76 | + foreach (var field in fields) |
| 77 | + { |
| 78 | + if (typeof(UnityEventBase).IsAssignableFrom(field.FieldType)) |
| 79 | + { |
| 80 | + var unityEvent = field.GetValue(mono) as UnityEventBase; |
| 81 | + if (unityEvent != null) |
| 82 | + { |
| 83 | + int count = unityEvent.GetPersistentEventCount(); |
| 84 | + for (int i = 0; i < count; i++) |
| 85 | + { |
| 86 | + var listener = unityEvent.GetPersistentTarget(i); |
| 87 | + if (listener == target) |
| 88 | + { |
| 89 | + results.Add(new ReferenceResult |
| 90 | + { |
| 91 | + message = $"{mono.name} ({type.Name}) -> UnityEvent '{field.Name}'", |
| 92 | + owner = mono.gameObject |
| 93 | + }); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + else if (typeof(UnityEngine.Object).IsAssignableFrom(field.FieldType)) |
| 99 | + { |
| 100 | + var value = field.GetValue(mono) as UnityEngine.Object; |
| 101 | + if (value == target) |
| 102 | + { |
| 103 | + results.Add(new ReferenceResult |
| 104 | + { |
| 105 | + message = $"{mono.name} ({type.Name}) -> Field '{field.Name}'", |
| 106 | + owner = mono.gameObject |
| 107 | + }); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if (results.Count == 0) |
| 114 | + { |
| 115 | + results.Add(new ReferenceResult |
| 116 | + { |
| 117 | + message = "No references found.", |
| 118 | + owner = null |
| 119 | + }); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments