Skip to content

Commit ec8e46f

Browse files
committed
Create MissingScriptsFinder.cs
1 parent 5e9bcb1 commit ec8e46f

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

Editor/MissingScriptsFinder.cs

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// ReSharper disable all
2+
3+
// **************************************************************** //
4+
//
5+
// Copyright (c) RimuruDev. All rights reserved.
6+
// Contact me:
7+
// - Gmail: [email protected]
8+
// - GitHub: https://github.com/RimuruDev
9+
// - LinkedIn: https://www.linkedin.com/in/rimuru/
10+
//
11+
// **************************************************************** //
12+
13+
using UnityEditor;
14+
using UnityEngine;
15+
using UnityEditor.SceneManagement;
16+
using System.Runtime.Remoting.Activation;
17+
18+
namespace AbyssMoth
19+
{
20+
[Url("https://github.com/RimuruDev/Unity-MissingScriptsFinder")]
21+
public sealed class MissingScriptsFinder : EditorWindow
22+
{
23+
[MenuItem("RimuruDev Tools/Find Missing Scripts")]
24+
public static void ShowWindow() =>
25+
GetWindow<MissingScriptsFinder>("Find Missing Scripts");
26+
27+
private void OnGUI()
28+
{
29+
if (GUILayout.Button("Find Missing Scripts in Scene"))
30+
{
31+
FindMissingScripts();
32+
}
33+
34+
if (GUILayout.Button("Delete All Missing Scripts"))
35+
{
36+
DeleteAllMissingScripts();
37+
}
38+
}
39+
40+
private static void FindMissingScripts()
41+
{
42+
var objects = FindObjectsOfType<GameObject>(true);
43+
var missingCount = 0;
44+
45+
foreach (var go in objects)
46+
{
47+
var components = go.GetComponents<Component>();
48+
49+
foreach (var component in components)
50+
{
51+
if (component == null)
52+
{
53+
missingCount++;
54+
Debug.Log($"<color=yellow>Missing script found in GameObject: {GetFullPath(go)}</color>", go);
55+
}
56+
}
57+
}
58+
59+
Debug.Log(missingCount == 0
60+
? "No missing scripts found in the scene."
61+
: $"<color=magenta>Found {missingCount} GameObjects with missing scripts.</color>");
62+
}
63+
64+
private static void DeleteAllMissingScripts()
65+
{
66+
var objects = GameObject.FindObjectsOfType<GameObject>(true);
67+
var removedCount = 0;
68+
69+
foreach (var go in objects)
70+
{
71+
var components = go.GetComponents<Component>();
72+
73+
foreach (var component in components)
74+
{
75+
if (component == null)
76+
{
77+
Undo.RegisterCompleteObjectUndo(go, "Remove Missing Scripts");
78+
GameObjectUtility.RemoveMonoBehavioursWithMissingScript(go);
79+
removedCount++;
80+
}
81+
}
82+
}
83+
84+
Debug.Log(removedCount == 0
85+
? "No missing scripts found to delete."
86+
: $"<color=magenta>Deleted {removedCount} missing scripts.</color>");
87+
88+
EditorSceneManager.MarkAllScenesDirty();
89+
}
90+
91+
private static string GetFullPath(GameObject go)
92+
{
93+
var path = "/" + go.name;
94+
95+
while (go.transform.parent != null)
96+
{
97+
go = go.transform.parent.gameObject;
98+
path = "/" + go.name + path;
99+
}
100+
101+
return path;
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)