Пример #1
0
        private void Unregister(Type type, object eventType, InvokeInfo invokeInfo)
        {
            if (m_eventDict.TryGetValue(type, out var eventDict))
            {
                if (eventDict.TryGetValue(eventType, out List <InvokeInfo> invokeList))
                {
                    for (int i = 0; i < invokeList.Count; i++)
                    {
                        if (invokeList[i].target == invokeInfo.target &&
                            invokeList[i].method == invokeInfo.method)
                        {
                            invokeList.RemoveAt(i);
                            break;
                        }
                    }
                    if (invokeList.Count == 0)
                    {
                        eventDict.Remove(eventType);
                    }

                    if (m_eventDict.Count == 0)
                    {
                        m_eventDict.Remove(type);
                    }
                }
            }
        }
Пример #2
0
        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}");
                        }
                    }
                }
            }
        }
Пример #3
0
 private void Broadcast(Type type, object eventType, params object[] param)
 {
     if (m_eventDict.TryGetValue(type, out var eventDict))
     {
         if (eventDict.TryGetValue(eventType, out var invokeList))
         {
             for (int i = 0; i < invokeList.Count; i++)
             {
                 InvokeInfo invokeInfo = invokeList[i];
                 invokeInfo.method.Invoke(invokeInfo.target, new object[] { param });
             }
         }
     }
 }
Пример #4
0
        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);
                    }
                }
            }
        }
Пример #5
0
 private void Register(Type type, object eventType, InvokeInfo invokeInfo)
 {
     if (m_eventDict.TryGetValue(type, out var eventDict))
     {
         if (eventDict.TryGetValue(eventType, out var invokeList))
         {
             invokeList.Add(invokeInfo);
         }
         else
         {
             invokeList = new List <InvokeInfo>();
             invokeList.Add(invokeInfo);
             eventDict.Add(eventType, invokeList);
         }
     }
     else
     {
         List <InvokeInfo> invokeList = new List <InvokeInfo>();
         invokeList.Add(invokeInfo);
         m_eventDict.Add(type, new Dictionary <object, List <InvokeInfo> >());
         m_eventDict[type].Add(eventType, invokeList);
     }
 }