|
| 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 | +} |
0 commit comments