-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// ReSharper disable all | ||
|
||
// **************************************************************** // | ||
// | ||
// Copyright (c) RimuruDev. All rights reserved. | ||
// Contact me: | ||
// - Gmail: [email protected] | ||
// - 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<MissingScriptsFinder>("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<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.</color>"); | ||
} | ||
|
||
private static void DeleteAllMissingScripts() | ||
{ | ||
var objects = GameObject.FindObjectsOfType<GameObject>(true); | ||
var removedCount = 0; | ||
|
||
foreach (var go in objects) | ||
{ | ||
var components = go.GetComponents<Component>(); | ||
|
||
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." | ||
: $"<color=magenta>Deleted {removedCount} missing scripts.</color>"); | ||
|
||
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; | ||
} | ||
} | ||
} |