From d41abe2cf57762a35eaf34155887bc3a7485f191 Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Fri, 28 Feb 2025 20:06:09 +0800 Subject: [PATCH] rework drawgizmo callback --- Runtime/DebugX.cs | 73 ++++++++++++++----- Runtime/Utils/DebugXDrawGizmoAttribute.cs | 14 ---- .../Utils/DebugXDrawGizmoAttribute.cs.meta | 2 - 3 files changed, 55 insertions(+), 34 deletions(-) delete mode 100644 Runtime/Utils/DebugXDrawGizmoAttribute.cs delete mode 100644 Runtime/Utils/DebugXDrawGizmoAttribute.cs.meta diff --git a/Runtime/DebugX.cs b/Runtime/DebugX.cs index f1e3ef5..1204eab 100644 --- a/Runtime/DebugX.cs +++ b/Runtime/DebugX.cs @@ -14,7 +14,6 @@ using Unity.Collections.LowLevel.Unsafe; using DCFApixels.DebugXCore.Internal; #if UNITY_EDITOR using UnityEditor; -using System.Reflection; using System.Linq; #endif @@ -22,6 +21,53 @@ namespace DCFApixels { using static DebugXConsts; using IN = System.Runtime.CompilerServices.MethodImplAttribute; + + public static class DebugXEvents + { + public delegate void OnDrawGizmoHandler(Camera camera); + private static event OnDrawGizmoHandler _onDrawGizmo = delegate { }; + public static event OnDrawGizmoHandler OnDrawGizmo + { + add + { + _onDrawGizmo -= value; + _onDrawGizmo += value; + } + remove + { + _onDrawGizmo -= value; + } + } + + private static void CleanupEvent(ref OnDrawGizmoHandler eventToClean) + { + var invocationList = eventToClean.GetInvocationList(); + + var validDelegates = invocationList + .Where(d => d.Target as UnityEngine.Object == null || d.Target as UnityEngine.Object != null) + .Where(d => !(d.Target is UnityEngine.Object targetObj) || targetObj != null) + .ToArray(); + + if (validDelegates.Length != invocationList.Length) + { + eventToClean = null; + foreach (var delegateItem in validDelegates) + { + eventToClean += (OnDrawGizmoHandler)delegateItem; + } + } + } + + internal static void CleanupOnDrawGizmo() + { + CleanupEvent(ref _onDrawGizmo); + } + internal static void InvokeOnDrawGizmo(Camera camera) + { + _onDrawGizmo(camera); + } + } + public static unsafe partial class DebugX { private static DebugXPauseState _pauseState = DebugXPauseState.Unpaused; @@ -36,7 +82,7 @@ namespace DCFApixels private static ulong _timeTicks = 0; #if UNITY_EDITOR - private static (MethodInfo method, DebugXDrawGizmoAttribute attribute)[] _drawGizmosMethods; + private static readonly Unity.Profiling.ProfilerMarker _onDrawGizmoCalllback = new Unity.Profiling.ProfilerMarker($"{nameof(DebugX)}.{nameof(DebugXEvents.OnDrawGizmo)}"); #endif public static ulong RenderTicks @@ -48,7 +94,6 @@ namespace DCFApixels get { return _timeTicks; } } - #region Other public static void ClearAllGizmos() { @@ -121,7 +166,6 @@ namespace DCFApixels #if UNITY_EDITOR - _drawGizmosMethods = TypeCache.GetMethodsWithAttribute().Select(o => (o, o.GetCustomAttribute())).ToArray(); EditorApplication.pauseStateChanged -= EditorApplication_pauseStateChanged; EditorApplication.pauseStateChanged += EditorApplication_pauseStateChanged; EditorApplication.update -= EditorApplication_update; @@ -304,28 +348,21 @@ namespace DCFApixels { _renderTicks++; _lastEditorToRenderTicks = _editorTicks; + + + DebugXEvents.CleanupOnDrawGizmo(); } if (DebugXUtility.IsGizmosRender()) { - if(_drawGizmosMethods.Length > 0) +#if UNITY_EDITOR + using (_onDrawGizmoCalllback.Auto()) +#endif { - object[] oneObjParams = new object[1]; - for (int i = 0; i < _drawGizmosMethods.Length; i++) - { - var pair = _drawGizmosMethods[i]; - var objects = UnityEngine.Object.FindObjectsByType(pair.attribute.Type, FindObjectsInactive.Exclude, FindObjectsSortMode.None); - for (int j = 0; j < objects.Length; j++) - { - oneObjParams[0] = objects[j]; - pair.method.Invoke(null, oneObjParams); - } - } + DebugXEvents.InvokeOnDrawGizmo(camera); } - - RenderContextController.StaicContextController.Prepare(); RenderContextController.StaicContextController.Render(cbExecutor); cbExecutor.Submit(); diff --git a/Runtime/Utils/DebugXDrawGizmoAttribute.cs b/Runtime/Utils/DebugXDrawGizmoAttribute.cs deleted file mode 100644 index 02278b1..0000000 --- a/Runtime/Utils/DebugXDrawGizmoAttribute.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; - -namespace DCFApixels.DebugXCore -{ - [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)] - public sealed class DebugXDrawGizmoAttribute : Attribute - { - public Type Type; - public DebugXDrawGizmoAttribute(Type type) - { - Type = type; - } - } -} \ No newline at end of file diff --git a/Runtime/Utils/DebugXDrawGizmoAttribute.cs.meta b/Runtime/Utils/DebugXDrawGizmoAttribute.cs.meta deleted file mode 100644 index 6642528..0000000 --- a/Runtime/Utils/DebugXDrawGizmoAttribute.cs.meta +++ /dev/null @@ -1,2 +0,0 @@ -fileFormatVersion: 2 -guid: 884ed13700ff7c44e86f92c859f02dc5 \ No newline at end of file