Test123/Assets/RL_DevPlus1/Editor/ProjectAnalyzer/RLUnityEventAnalyzer.cs
2026-06-15 12:56:29 -05:00

77 lines
3.2 KiB
C#

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace RunlevelSystems.DevPlus1.EditorTools
{
public static class RLUnityEventAnalyzer
{
public static List<RLUnityEventInfo> ExtractUnityEvents(GameObject gameObject, string sourcePath, string hierarchyPath)
{
List<RLUnityEventInfo> events = new List<RLUnityEventInfo>();
Component[] components = gameObject.GetComponents<Component>();
foreach (Component component in components)
{
if (component == null)
{
continue;
}
SerializedObject serializedObject = new SerializedObject(component);
SerializedProperty iterator = serializedObject.GetIterator();
bool enterChildren = true;
while (iterator.NextVisible(enterChildren))
{
enterChildren = false;
if (iterator.propertyType == SerializedPropertyType.Generic &&
(iterator.type.Contains("UnityEvent") || iterator.displayName.Contains("On Click") || iterator.name.Contains("m_OnClick")))
{
ExtractPersistentCalls(events, iterator.Copy(), sourcePath, hierarchyPath, component);
}
}
}
return events;
}
private static void ExtractPersistentCalls(List<RLUnityEventInfo> events, SerializedProperty eventProperty, string sourcePath, string hierarchyPath, Component component)
{
SerializedProperty calls = eventProperty.FindPropertyRelative("m_PersistentCalls.m_Calls");
if (calls == null || !calls.isArray)
{
return;
}
for (int i = 0; i < calls.arraySize; i++)
{
SerializedProperty call = calls.GetArrayElementAtIndex(i);
Object target = ReadObject(call, "m_Target");
string method = ReadString(call, "m_MethodName");
RLUnityEventInfo info = new RLUnityEventInfo();
info.sourcePath = sourcePath;
info.gameObjectPath = hierarchyPath;
info.componentName = component.GetType().Name;
info.eventName = eventProperty.displayName;
info.targetObject = target != null ? target.name : "missing or none";
info.targetComponent = target != null ? target.GetType().Name : "not detected";
info.targetMethod = string.IsNullOrEmpty(method) ? "not detected" : method;
events.Add(info);
}
}
private static Object ReadObject(SerializedProperty parent, string relativePath)
{
SerializedProperty property = parent.FindPropertyRelative(relativePath);
return property != null ? property.objectReferenceValue : null;
}
private static string ReadString(SerializedProperty parent, string relativePath)
{
SerializedProperty property = parent.FindPropertyRelative(relativePath);
return property != null ? property.stringValue : string.Empty;
}
}
}