diff --git a/QFramework.Unity2018+/Assets/QFramework/Framework/Scripts/QFramework.cs b/QFramework.Unity2018+/Assets/QFramework/Framework/Scripts/QFramework.cs index 8e926f1d8..2eba43f88 100755 --- a/QFramework.Unity2018+/Assets/QFramework/Framework/Scripts/QFramework.cs +++ b/QFramework.Unity2018+/Assets/QFramework/Framework/Scripts/QFramework.cs @@ -23,6 +23,7 @@ using System; using System.Collections.Generic; +using System.Reflection; using UnityEngine; namespace QFramework @@ -237,6 +238,11 @@ public static void UnRegisterEvent(this IOnEvent self) where T : struct } } + public interface IOnEventInArchitecture + { + void OnEventInArchitecture(T e); + } + #endregion #region Controller @@ -467,6 +473,8 @@ public interface ICanRegisterEvent : IBelongToArchitecture public static class CanRegisterEventExtension { + private static readonly string mHandlerInArchitectureMethodName = "OnEventInArchitecture"; + public static IUnRegister RegisterEvent(this ICanRegisterEvent self, Action onEvent) { return self.GetArchitecture().RegisterEvent(onEvent); @@ -476,6 +484,71 @@ public static void UnRegisterEvent(this ICanRegisterEvent self, Action onE { self.GetArchitecture().UnRegisterEvent(onEvent); } + + private static void ForEachSpecificMethod(object obj, string methodName, Action callback) + { + if (obj == null) return; + + Type type = obj.GetType(); + MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance); + foreach (MethodInfo method in methods) + { + if (method.Name == methodName) + { + Type paramType = method.GetParameters()[0].ParameterType; + Type delegateType = typeof(Action<>).MakeGenericType(paramType); + var handler = method.CreateDelegate(delegateType, obj); + + callback?.Invoke(paramType, handler); + } + } + } + + /// + /// 注册所有通过 IOnEventInArchitecture 接口实现的事件处理器 + /// + /// + /// + /// 是否在 gameObject 销毁后自动注销 + public static void RegisterAllEventInArchitecture(this ICanRegisterEvent self, object obj, bool isAutoUnregister = true) + { + if (obj == null) return; + + GameObject gameObject = obj is Component ? (obj as Component).gameObject : null; + Type architectureType = self.GetArchitecture().GetType(); + + ForEachSpecificMethod(obj, mHandlerInArchitectureMethodName, (paramType, handler) => + { + MethodInfo registerMethod = architectureType + .GetMethod("RegisterEvent", BindingFlags.Public | BindingFlags.Instance) + .MakeGenericMethod(paramType); + var unregister = registerMethod.Invoke(self.GetArchitecture(), new object[] { handler }) as IUnRegister; + if (isAutoUnregister && gameObject is not null) + { + unregister.UnRegisterWhenGameObjectDestroyed(gameObject); + } + }); + } + + /// + /// 注销所有通过 IOnEventInArchitecture 接口实现的事件处理器 + /// + /// + /// + public static void UnRegisterAllEventInArchitecture(this ICanRegisterEvent self, object obj) + { + if (obj == null) return; + + Type architectureType = self.GetArchitecture().GetType(); + + ForEachSpecificMethod(obj, mHandlerInArchitectureMethodName, (paramType, handler) => + { + MethodInfo unregisterMethod = architectureType + .GetMethod("UnRegisterEvent", BindingFlags.Public | BindingFlags.Instance) + .MakeGenericMethod(paramType); + unregisterMethod.Invoke(self.GetArchitecture(), new object[] { handler }); + }); + } } public interface ICanSendCommand : IBelongToArchitecture @@ -732,7 +805,7 @@ public BindableProperty(T defaultValue = default) { mValue = defaultValue; } - + protected T mValue; public T Value