|
| 1 | +using System.Collections; |
| 2 | +using System.Collections.Generic; |
| 3 | +using UnityEngine; |
| 4 | +using UnityEditor; |
| 5 | +using UnityEngine.SceneManagement; |
| 6 | + |
| 7 | +public class FindMissingScripts: EditorWindow |
| 8 | +{ |
| 9 | + [MenuItem("Content Extensions/Find Missing Component")] |
| 10 | + static public void FindMissing() |
| 11 | + { |
| 12 | + GetWindow<FindMissingScripts>(); |
| 13 | + } |
| 14 | + |
| 15 | + protected List<GameObject> _objectWithMissingScripts; |
| 16 | + protected Vector2 _scrollPosition; |
| 17 | + |
| 18 | + private void OnEnable() |
| 19 | + { |
| 20 | + _objectWithMissingScripts = new List<GameObject>(); |
| 21 | + _scrollPosition = Vector2.zero; |
| 22 | + } |
| 23 | + |
| 24 | + private void OnGUI() |
| 25 | + { |
| 26 | + EditorGUILayout.BeginHorizontal(); |
| 27 | + if (GUILayout.Button("Find in Assets")) |
| 28 | + FindInAssets(); |
| 29 | + if (GUILayout.Button("Find in Current Scene")) |
| 30 | + FindInScenes(); |
| 31 | + EditorGUILayout.EndHorizontal(); |
| 32 | + |
| 33 | + _scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition); |
| 34 | + |
| 35 | + for (int i = 0; i < _objectWithMissingScripts.Count; ++i) |
| 36 | + { |
| 37 | + if (GUILayout.Button(_objectWithMissingScripts[i].name)) |
| 38 | + { |
| 39 | + EditorGUIUtility.PingObject(_objectWithMissingScripts[i]); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + EditorGUILayout.EndScrollView(); |
| 44 | + } |
| 45 | + |
| 46 | + void FindInAssets() |
| 47 | + { |
| 48 | + var assetGUIDs = AssetDatabase.FindAssets("t:GameObject"); |
| 49 | + _objectWithMissingScripts.Clear(); |
| 50 | + |
| 51 | + Debug.Log("Testing " + assetGUIDs.Length + " GameObject in Assets"); |
| 52 | + |
| 53 | + foreach (string assetGuiD in assetGUIDs) |
| 54 | + { |
| 55 | + GameObject obj = AssetDatabase.LoadAssetAtPath<GameObject>(AssetDatabase.GUIDToAssetPath(assetGuiD)); |
| 56 | + |
| 57 | + RecursiveDepthSearch(obj); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + void RecursiveDepthSearch(GameObject root) |
| 62 | + { |
| 63 | + Component[] components = root.GetComponents<Component>(); |
| 64 | + foreach (Component c in components) |
| 65 | + { |
| 66 | + if (c == null) |
| 67 | + { |
| 68 | + if (!_objectWithMissingScripts.Contains(root)) |
| 69 | + _objectWithMissingScripts.Add(root); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + foreach (Transform t in root.transform) |
| 74 | + { |
| 75 | + RecursiveDepthSearch(t.gameObject); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + void FindInScenes() |
| 80 | + { |
| 81 | + _objectWithMissingScripts.Clear(); |
| 82 | + |
| 83 | + for(int i= 0; i < SceneManager.sceneCount; ++i) |
| 84 | + { |
| 85 | + var rootGOs = SceneManager.GetSceneAt(i).GetRootGameObjects(); |
| 86 | + |
| 87 | + Debug.Log("Testing " + rootGOs.Length + " Gameobjects in scene " + i); |
| 88 | + |
| 89 | + foreach (GameObject obj in rootGOs) |
| 90 | + { |
| 91 | + RecursiveDepthSearch(obj); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments