Ejemplo n.º 1
0
        private static void SubscribeMethod(object eventObject, MethodToEventSubscribeContainer methodToEventSubscriberField, MethodNameAttribute methodNameAttribute)
        {
            if (methodNameAttribute == null)
            {
                Debug.LogError("Method Name Attribute is not found");
                return;
            }

            var methodInfo = eventObject.GetType().GetMethod(methodNameAttribute.MethodName);

            if (methodInfo == null)
            {
                Debug.LogError($"ERROR! Method {methodNameAttribute.MethodName} doesn't exsist on a object");
                return;
            }

            if (methodToEventSubscriberField.EventsTemplateData.Count == 0)
            {
                Debug.Log($"Nothing to subscribe in {methodNameAttribute.MethodName} method");
            }

            foreach (var item in methodToEventSubscriberField.EventsTemplateData)
            {
                if (item.eventObject == null)
                {
                    continue;
                }

                if (item.IsGlobalEvent)
                {
                    var globalAction = (Action <EventParameter>)Delegate.CreateDelegate(
                        type: typeof(Action <EventParameter>),
                        target: eventObject,
                        method: methodInfo.Name);

                    GlobalEventsRouter.StartListeningGlobalEvent(item.GlobalEventName, globalAction);
                }
                else
                {
                    var eventInfo        = item.MonobehaviourReference.GetType().GetEvent(item.MonobehaviourEventName);
                    var newEventDelegate = (Action <EventParameter>)Delegate.CreateDelegate(
                        type: eventInfo.EventHandlerType,
                        target: eventObject,
                        method: methodInfo.Name);

                    //eventInfo.RemoveEventHandler(item.MonobehaviourReference, newEventDelegate);
                    eventInfo.AddEventHandler(item.MonobehaviourReference, newEventDelegate);
                }
            }
        }
Ejemplo n.º 2
0
        private static void SubscribeEvent(object eventObject, EventToMethodSubscribeСontainer eventToMethodSubscriberField, EventNameAttribute eventNameAttribute)
        {
            if (eventNameAttribute == null)
            {
                Debug.LogError("Event Name Attribute is not found");
                return;
            }

            var eventInfo = eventObject.GetType().GetEvent(eventNameAttribute.EventName);

            if (eventInfo == null)
            {
                Debug.LogError($"ERROR! Event {eventNameAttribute.EventName} doesn't exsist on a object");
                return;
            }

            foreach (var item in eventToMethodSubscriberField.MethodsTemplateData)
            {
                if (item.eventObject == null)
                {
                    continue;
                }

                if (item.IsGlobalEvent)
                {
                    Action <EventParameter> globalEventAction = null;

                    globalEventAction += eventParameter => GlobalEventsRouter.RaiseGlobalEvent(item.GlobalEventName, eventParameter);
                    eventInfo.AddEventHandler(eventObject, globalEventAction);
                }
                else
                {
                    if (item.MonobehaviourReference == null)
                    {
                        Debug.LogError($"Monobehaviour reference is null. Can't subscribe it");
                        continue;
                    }


                    var newEventDelegate = (Action <EventParameter>)Delegate.CreateDelegate(
                        type: typeof(Action <EventParameter>),
                        target: item.MonobehaviourReference,
                        method: item.MonobehaviourMethodName);

                    eventInfo.RemoveEventHandler(eventObject, newEventDelegate);
                    eventInfo.AddEventHandler(eventObject, newEventDelegate);
                }
            }
        }