Unity-Customer-Template/Assets/RL_DevPlus1/Editor/ProjectAnalyzer/RLPrefabAnalyzer.cs
Frank Harris 28c15035a4
All checks were successful
Code Documentation / code-docs (push) Successful in 6s
Project Report / project-report (push) Successful in 4s
Validate Code / validate (push) Successful in 3s
added unity files
2026-06-15 12:03:33 -05:00

98 lines
3.3 KiB
C#

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace RunlevelSystems.DevPlus1.EditorTools
{
public static class RLPrefabAnalyzer
{
public static List<RLPrefabInfo> AnalyzePrefabs()
{
List<RLPrefabInfo> results = new List<RLPrefabInfo>();
string[] guids = AssetDatabase.FindAssets("t:Prefab");
foreach (string guid in guids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(path);
if (prefab == null)
{
continue;
}
RLPrefabInfo info = new RLPrefabInfo();
info.prefabPath = path;
TraversePrefab(prefab, info);
results.Add(info);
}
return results;
}
private static void TraversePrefab(GameObject gameObject, RLPrefabInfo info)
{
info.missingScriptCount += GameObjectUtility.GetMonoBehavioursWithMissingScriptCount(gameObject);
Component[] components = gameObject.GetComponents<Component>();
foreach (Component component in components)
{
if (component == null)
{
AddUnique(info.components, "Missing Script");
continue;
}
AddUnique(info.components, component.GetType().Name);
if (component is MonoBehaviour)
{
AddUnique(info.attachedScripts, component.GetType().Name);
}
CollectReferences(component, info);
}
string prefabSource = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(gameObject);
if (!string.IsNullOrEmpty(prefabSource) && prefabSource != info.prefabPath)
{
AddUnique(info.nestedPrefabs, prefabSource);
}
foreach (Transform child in gameObject.transform)
{
TraversePrefab(child.gameObject, info);
}
}
private static void CollectReferences(Component component, RLPrefabInfo info)
{
SerializedObject serializedObject = new SerializedObject(component);
SerializedProperty property = serializedObject.GetIterator();
bool enterChildren = true;
while (property.NextVisible(enterChildren))
{
enterChildren = false;
if (property.propertyType != SerializedPropertyType.ObjectReference)
{
continue;
}
if (property.objectReferenceValue != null)
{
AddUnique(info.importantReferences, component.GetType().Name + "." + property.displayName + " -> " + property.objectReferenceValue.name);
}
else if (property.objectReferenceInstanceIDValue != 0)
{
info.missingReferenceCount++;
}
}
}
private static void AddUnique(List<string> values, string value)
{
if (!values.Contains(value))
{
values.Add(value);
}
}
}
}