-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMissingScriptsFinder.cs
132 lines (110 loc) · 4.19 KB
/
MissingScriptsFinder.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// ReSharper disable all
// **************************************************************** //
//
// Copyright (c) RimuruDev. All rights reserved.
// Contact me:
// - Gmail: rimuru.dev@gmail.com
// - GitHub: https://github.com/RimuruDev
// - LinkedIn: https://www.linkedin.com/in/rimuru/
//
// **************************************************************** //
using UnityEditor;
using UnityEngine;
using UnityEditor.SceneManagement;
namespace AbyssMoth
{
public sealed class MissingScriptsFinder : EditorWindow
{
[MenuItem("RimuruDev Tools/Find Missing Scripts")]
public static void ShowWindow() =>
GetWindow<MissingScriptsFinder>("Find Missing Scripts");
private void OnGUI()
{
if (GUILayout.Button("Find Missing Scripts in Scene"))
{
FindMissingScriptsInScene();
}
if (GUILayout.Button("Delete All Missing Scripts in Scene"))
{
DeleteAllMissingScriptsInScene();
}
if (GUILayout.Button("Find Missing Scripts in Prefabs"))
{
FindMissingScriptsInPrefabs();
}
}
private static void FindMissingScriptsInScene()
{
var objects = FindObjectsOfType<GameObject>(true);
var missingCount = 0;
foreach (var go in objects)
{
var components = go.GetComponents<Component>();
foreach (var component in components)
{
if (component == null)
{
missingCount++;
Debug.Log($"<color=yellow>Missing script found in GameObject: {GetFullPath(go)}</color>", go);
}
}
}
Debug.Log(missingCount == 0
? "No missing scripts found in the scene."
: $"<color=magenta>Found {missingCount} GameObjects with missing scripts in the scene.</color>");
}
private static void DeleteAllMissingScriptsInScene()
{
var objects = FindObjectsOfType<GameObject>(true);
var removedCount = 0;
foreach (var go in objects)
{
var components = go.GetComponents<Component>();
foreach (var component in components)
{
if (component == null)
{
GameObjectUtility.RemoveMonoBehavioursWithMissingScript(go);
removedCount++;
}
}
}
Debug.Log(removedCount == 0
? "No missing scripts found to delete in the scene."
: $"<color=magenta>Deleted {removedCount} missing scripts in the scene.</color>");
EditorSceneManager.MarkAllScenesDirty();
}
private static void FindMissingScriptsInPrefabs()
{
var prefabGUIDs = AssetDatabase.FindAssets("t:Prefab");
var missingCount = 0;
foreach (var guid in prefabGUIDs)
{
var path = AssetDatabase.GUIDToAssetPath(guid);
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(path);
var components = prefab.GetComponentsInChildren<Component>(true);
foreach (var component in components)
{
if (component == null)
{
missingCount++;
Debug.Log($"<color=yellow>Missing script found in Prefab: {path}</color>", prefab);
}
}
}
Debug.Log(missingCount == 0
? "No missing scripts found in any prefabs."
: $"<color=magenta>Found {missingCount} prefabs with missing scripts.</color>");
}
private static string GetFullPath(GameObject go)
{
var path = "/" + go.name;
while (go.transform.parent != null)
{
go = go.transform.parent.gameObject;
path = "/" + go.name + path;
}
return path;
}
}
}