Skip to content

Instantly share code, notes, and snippets.

@brainwipe
Created June 24, 2022 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brainwipe/0d56922f26c5acafa7bc797d2bb3a336 to your computer and use it in GitHub Desktop.
Save brainwipe/0d56922f26c5acafa7bc797d2bb3a336 to your computer and use it in GitHub Desktop.
Unity Editor Tool for Removing Missing Behaviours
//
// To use: copy this file into a folder called Assets/Editor, return to Unity. Go to Window->Missing Behaviour Remover and a window will popup; follow instructions below.
//
#if (UNITY_EDITOR)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
namespace Lang.Clomp.EditorExtensions
{
/// <summary>
/// Removed all the missing behaviours (scripts) from the open scene. If you have lots of prefabs, it will not go through those automatically, you have to open each and press the button.
/// </summary>
public class MissingBehaviourRemover : EditorWindow
{
[MenuItem("Window/Missing Behaviour Remover")]
public static void OpenWindow()
{
GetWindow<MissingBehaviourRemover>("Missing Behaviour Remover");
}
private void OnGUI()
{
if (GUILayout.Button("Remove Missing Behaviours")) RemoveMissing();
}
private void RemoveMissing()
{
var allGameObjects = StageUtility.GetCurrentStageHandle().FindComponentsOfType<MonoBehaviour>();
Debug.Log($"Searching {allGameObjects.Length} gameobjects for behaviours to remove");
int removed = 0;
for (int i = 0; i < allGameObjects.Length; i++)
{
removed += RecurseRemoveMissing(allGameObjects[i].transform);
}
Debug.Log($"Removed {removed} behaviours");
}
private int RecurseRemoveMissing(Transform target)
{
var removed = GameObjectUtility.RemoveMonoBehavioursWithMissingScript(target.gameObject);
for (int i = 0; i < target.childCount; i++)
{
removed += RecurseRemoveMissing(target.GetChild(i));
}
return removed;
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment