Skip to content

Commit 834b066

Browse files
committed
feat: Add remove missing scripts command
1 parent 60574a5 commit 834b066

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Copyright (c) Jen-Chieh Shen. All rights reserved.
3+
*
4+
5+
*/
6+
using System.Collections.Generic;
7+
using UnityEditor;
8+
using UnityEngine;
9+
using UnityEngine.SceneManagement;
10+
11+
namespace Mx
12+
{
13+
public class MxCScript : Mx
14+
{
15+
/* Variables */
16+
17+
/* Setter & Getter */
18+
19+
/* Functions */
20+
21+
public override bool Enable() { return true; }
22+
23+
/// <summary>
24+
/// Remove missing scripts from a game object and its children.
25+
/// </summary>
26+
private static void RemoveMissingScripts(GameObject obj)
27+
{
28+
if (obj == null)
29+
return;
30+
31+
int count = GameObjectUtility.GetMonoBehavioursWithMissingScriptCount(obj);
32+
33+
if (count > 0)
34+
{
35+
Undo.RegisterCompleteObjectUndo(obj, "Remove missing scripts");
36+
GameObjectUtility.RemoveMonoBehavioursWithMissingScript(obj);
37+
}
38+
39+
// Apply to children as well.
40+
for (int index = 0; index < obj.transform.childCount; ++index)
41+
{
42+
Transform child = obj.transform.GetChild(index);
43+
44+
RemoveMissingScripts(child.gameObject);
45+
}
46+
}
47+
48+
[Interactive(
49+
icon: "d_cs Script Icon",
50+
summary: "Remove all missing scripts in the scene and prefabs")]
51+
public static void RemoveMissingScripts()
52+
{
53+
Scene scene = SceneManager.GetActiveScene();
54+
55+
GameObject[] objs = scene.GetRootGameObjects();
56+
57+
foreach (GameObject obj in objs)
58+
{
59+
RemoveMissingScripts(obj);
60+
}
61+
62+
List<string> files = MxEditorUtil.DefaultFiles("*.prefab");
63+
64+
foreach (string file in files)
65+
{
66+
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(file);
67+
68+
RemoveMissingScripts(prefab);
69+
}
70+
71+
AssetDatabase.SaveAssets();
72+
}
73+
}
74+
}

Assets/Mx/Editor/Commands/MxCScript.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)