-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindMissingScripts.cs
94 lines (79 loc) · 3.18 KB
/
FindMissingScripts.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using UnityEngine;
using UnityEditor;
using UnityEngine.SceneManagement;
using UnityEditor.SceneManagement;
using System;
public class FindMissingScriptsEditor : EditorWindow
{
[MenuItem("Window/Utilities/Find Missing Scripts")]
public static void FindMissingScripts()
{
EditorWindow.GetWindow(typeof(FindMissingScriptsEditor));
}
[MenuItem("Window/Utilities/Clear Progressbar")]
public static void ClearProgressbar()
{
EditorUtility.ClearProgressBar();
}
static int missingCount = -1;
void OnGUI()
{
EditorGUILayout.BeginHorizontal();
{
EditorGUILayout.LabelField("Missing Scripts:");
EditorGUILayout.LabelField("" + (missingCount == -1 ? "---" : missingCount.ToString()));
}
EditorGUILayout.EndHorizontal();
if (GUILayout.Button("Find missing scripts"))
{
missingCount = 0;
EditorUtility.DisplayProgressBar("Searching Prefabs", "", 0.0f);
string[] files = System.IO.Directory.GetFiles(Application.dataPath, "*.prefab", System.IO.SearchOption.AllDirectories);
EditorUtility.DisplayCancelableProgressBar("Searching Prefabs", "Found " + files.Length + " prefabs", 0.0f);
Scene currentScene = EditorSceneManager.GetActiveScene();
string scenePath = currentScene.path;
EditorSceneManager.NewScene(NewSceneSetup.EmptyScene);
for (int i = 0; i < files.Length; i++)
{
string prefabPath = files[i].Replace(Application.dataPath, "Assets");
if (EditorUtility.DisplayCancelableProgressBar("Processing Prefabs " + i + "/" + files.Length, prefabPath, (float)i/(float)files.Length))
break;
GameObject go = UnityEditor.AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)) as GameObject;
if (go != null)
{
FindInGO(go);
go = null;
EditorUtility.UnloadUnusedAssetsImmediate(true);
}
}
EditorUtility.DisplayProgressBar("Cleanup", "Cleaning up", 1.0f);
EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Single);
EditorUtility.UnloadUnusedAssetsImmediate(true);
GC.Collect();
EditorUtility.ClearProgressBar();
}
}
private static void FindInGO(GameObject go, string prefabName = "")
{
Component[] components = go.GetComponents<Component>();
for (int i = 0; i < components.Length; i++)
{
if (components[i] == null)
{
missingCount++;
Transform t = go.transform;
string componentPath = go.name;
while (t.parent != null)
{
componentPath = t.parent.name + "/" + componentPath;
t = t.parent;
}
Debug.LogWarning("Prefab " + prefabName + " has an empty script attached:\n" + componentPath , go);
}
}
foreach (Transform child in go.transform)
{
FindInGO(child.gameObject, prefabName);
}
}
}