From ec8e46fab1e94588ae8a04f13befa7d60fafc886 Mon Sep 17 00:00:00 2001 From: Rimuru Date: Sat, 15 Jun 2024 15:59:04 +0400 Subject: [PATCH] Create MissingScriptsFinder.cs --- Editor/MissingScriptsFinder.cs | 104 +++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 Editor/MissingScriptsFinder.cs diff --git a/Editor/MissingScriptsFinder.cs b/Editor/MissingScriptsFinder.cs new file mode 100644 index 0000000..e40f8f3 --- /dev/null +++ b/Editor/MissingScriptsFinder.cs @@ -0,0 +1,104 @@ +// 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; +using System.Runtime.Remoting.Activation; + +namespace AbyssMoth +{ + [Url("https://github.com/RimuruDev/Unity-MissingScriptsFinder")] + public sealed class MissingScriptsFinder : EditorWindow + { + [MenuItem("RimuruDev Tools/Find Missing Scripts")] + public static void ShowWindow() => + GetWindow("Find Missing Scripts"); + + private void OnGUI() + { + if (GUILayout.Button("Find Missing Scripts in Scene")) + { + FindMissingScripts(); + } + + if (GUILayout.Button("Delete All Missing Scripts")) + { + DeleteAllMissingScripts(); + } + } + + private static void FindMissingScripts() + { + var objects = FindObjectsOfType(true); + var missingCount = 0; + + foreach (var go in objects) + { + var components = go.GetComponents(); + + foreach (var component in components) + { + if (component == null) + { + missingCount++; + Debug.Log($"Missing script found in GameObject: {GetFullPath(go)}", go); + } + } + } + + Debug.Log(missingCount == 0 + ? "No missing scripts found in the scene." + : $"Found {missingCount} GameObjects with missing scripts."); + } + + private static void DeleteAllMissingScripts() + { + var objects = GameObject.FindObjectsOfType(true); + var removedCount = 0; + + foreach (var go in objects) + { + var components = go.GetComponents(); + + foreach (var component in components) + { + if (component == null) + { + Undo.RegisterCompleteObjectUndo(go, "Remove Missing Scripts"); + GameObjectUtility.RemoveMonoBehavioursWithMissingScript(go); + removedCount++; + } + } + } + + Debug.Log(removedCount == 0 + ? "No missing scripts found to delete." + : $"Deleted {removedCount} missing scripts."); + + EditorSceneManager.MarkAllScenesDirty(); + } + + 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; + } + } +} \ No newline at end of file