public void Register(Component comp) { Type type = comp.GetType(); MethodInfo[] methods = type.GetMethods(); for (int i = 0; i < methods.Length; i++) { Attribute[] attrs = Attribute.GetCustomAttributes(methods[i]); for (int j = 0; j < attrs.Length; j++) { if (attrs[j] is RegistEventAttribute) { try { RegistEventAttribute attr = attrs[j] as RegistEventAttribute; InvokeInfo invokeInfo = new InvokeInfo(); invokeInfo.method = methods[i]; invokeInfo.target = comp; Register(attr.eventType.GetType(), attr.eventType, invokeInfo); } catch (System.Exception) { Debug.LogError($"方法注册失败:{type.Name}.{methods[i].Name}"); } } } } }
public void Unregister(Component comp) { Type type = comp.GetType(); MethodInfo[] methods = type.GetMethods(); for (int i = 0; i < methods.Length; i++) { Attribute[] attrs = Attribute.GetCustomAttributes(methods[i]); for (int j = 0; j < attrs.Length; j++) { if (attrs[j] is RegistEventAttribute) { RegistEventAttribute attr = attrs[j] as RegistEventAttribute; InvokeInfo invokeInfo = new InvokeInfo(); invokeInfo.method = methods[i]; invokeInfo.target = comp; Unregister(attr.eventType.GetType(), attr.eventType, invokeInfo); } } } }