Skip to content

Instantly share code, notes, and snippets.

@SorraTheOrc
Last active April 20, 2025 20:54
Show Gist options
  • Save SorraTheOrc/88c2606390446bbb3848b6eaedec4405 to your computer and use it in GitHub Desktop.
Save SorraTheOrc/88c2606390446bbb3848b6eaedec4405 to your computer and use it in GitHub Desktop.
// MIT License
//
// Copyright (c) Wizards Code
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Detects missing scripts, logging the results to the console and
/// providing a list of invalid objects.
/// </summary>
public static class DetectMissingScriptsWrapper
{
/// <summary>
/// Add a menu item to detects missing scripts in the project folders, logging the results to the console.
/// </summary>
[MenuItem("Tools/Wizards Code/Detect Missing Scripts")]
public static void DetectMissingScripts()
{
string[] folders = new string[] { "Assets/_Dev", "Assets/_Marketing", "Assets/_Rogue Wave" };
List<GameObject> invalidObjects = DetectMissingScripts(folders);
// perform remediation work here
}
/// <summary>
/// Detects missing scripts in the specified folders and logs warnings for each GameObject with missing scripts.
/// </summary>
/// <param name="folders">An array of folder paths to search for prefabs.</param>
/// <returns>A list of GameObjects that have missing scripts.</returns>
public static List<GameObject> DetectMissingScripts(string[] folders)
{
int step = 0;
EditorUtility.DisplayProgressBar("Detecting Missing Scripts", "Searching for prefabs to validate", 0.0f);
step++;
string[] guids = AssetDatabase.FindAssets("t:Prefab", folders);
int numOfSteps = guids.Length;
EditorUtility.DisplayProgressBar("Detecting Missing Scripts", $"Found {guids.Length} Game Objects to validate", step / numOfSteps);
List<GameObject> invalidObjects = new List<GameObject>();
List<GameObject> validObjects = new List<GameObject>();
foreach (string guid in guids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>(path);
Component[] components = go.GetComponents<Component>();
for (int i = 0; i < components.Length; i++)
{
if (components[i] == null)
{
invalidObjects.Add(go);
Debug.LogError($"Missing script found in GameObject: {go.name}", go);
}
else
{
validObjects.Add(go);
}
}
step++;
EditorUtility.DisplayProgressBar("Detecting Missing Scripts", $"{invalidObjects.Count} of {guids.Length} are invalid", step / numOfSteps);
}
if (invalidObjects.Count == 0)
{
EditorUtility.DisplayDialog("Missing Scripts Detection", $"No missing scripts found in {validObjects.Count} Game Objects.", "OK");
Debug.Log("No missing scripts found.");
}
else
{
EditorUtility.DisplayDialog("Missing Scripts Detection", $"Found {invalidObjects.Count} of {validObjects.Count} GameObjects have missing scripts. See details in the console", "OK");
}
EditorUtility.ClearProgressBar();
return invalidObjects;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment