From 5813670e2c999c45fc9f480835d1b43d8836eb59 Mon Sep 17 00:00:00 2001 From: exposerain <1328540878@qq.com> Date: Thu, 31 Aug 2023 22:37:54 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BD=BF=E6=9E=B6=E6=9E=84=20Architecture?= =?UTF-8?q?=20=E5=86=85=E9=83=A8=E4=BA=8B=E4=BB=B6=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=99=A8=E4=B9=9F=E5=8F=AF=E9=80=9A=E8=BF=87=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=EF=BC=8C=E5=B9=B6=E8=83=BD=E4=B8=80=E9=94=AE?= =?UTF-8?q?=E6=B3=A8=E5=86=8C/=E6=B3=A8=E9=94=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Framework/Scripts/QFramework.cs | 74 ++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) diff --git a/QFramework.Unity2018+/Assets/QFramework/Framework/Scripts/QFramework.cs b/QFramework.Unity2018+/Assets/QFramework/Framework/Scripts/QFramework.cs index 8e926f1d8..14db919ea 100755 --- a/QFramework.Unity2018+/Assets/QFramework/Framework/Scripts/QFramework.cs +++ b/QFramework.Unity2018+/Assets/QFramework/Framework/Scripts/QFramework.cs @@ -237,6 +237,11 @@ public static void UnRegisterEvent(this IOnEvent self) where T : struct } } + public interface IOnEventInArchitecture + { + void OnEventInArchitecture(T e); + } + #endregion #region Controller @@ -467,6 +472,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 +483,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 +804,7 @@ public BindableProperty(T defaultValue = default) { mValue = defaultValue; } - + protected T mValue; public T Value From f374772fa4fee880962f4147cc9892f9f5b63464 Mon Sep 17 00:00:00 2001 From: exposerain <1328540878@qq.com> Date: Thu, 31 Aug 2023 23:01:49 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=A1=A5=E4=BA=A4=E4=B8=8B=E5=8F=8D?= =?UTF-8?q?=E5=B0=84=E5=91=BD=E5=90=8D=E7=A9=BA=E9=97=B4=E7=9A=84=E5=BC=95?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Assets/QFramework/Framework/Scripts/QFramework.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/QFramework.Unity2018+/Assets/QFramework/Framework/Scripts/QFramework.cs b/QFramework.Unity2018+/Assets/QFramework/Framework/Scripts/QFramework.cs index 14db919ea..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