Example #1
0
 protected void UnregisterHandler(Type eventType, Action <ActorEvent> callback)
 {
     actor.UnregisterHandler(new ActorEventHandler
     {
         autoRegister = false,
         callback     = ActorEventDelegate.Create(callback.Method),
         component    = this,
         eventType    = eventType,
         priority     = 0
     });
 }
Example #2
0
        protected void RegisterHandler(Type eventType, Action <ActorEvent> callback, int priority = 0)
        {
            if (!typeof(ActorEvent).IsAssignableFrom(eventType))
            {
                throw new ArgumentException("type is not an ActorEvent");
            }

            actor.RegisterHandler(new ActorEventHandler
            {
                autoRegister = false,
                callback     = ActorEventDelegate.Create(callback.Method),
                component    = this,
                eventType    = eventType,
                priority     = priority
            });
        }
Example #3
0
        protected virtual void OnEnable()
        {
            // Lazy create the handlers array for this handler when first enabled
            var type = GetType();

            if (!infoByType.TryGetValue(type, out var existingInfo))
            {
                tempHandlersList.Clear();
                tempDependencyList.Clear();
                for (var t = type; t != null && t != typeof(ActorComponent); t = t.BaseType)
                {
                    var methods = t.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                    foreach (var method in methods)
                    {
                        var attrs = method.GetCustomAttributes(typeof(ActorEventHandlerAttribute), true);
                        if (attrs.Length == 0)
                        {
                            continue;
                        }

                        var attr = attrs[0] as ActorEventHandlerAttribute;
                        if (null == attr)
                        {
                            continue;
                        }

                        var parameters = method.GetParameters();
                        if (parameters.Length != 1)
                        {
                            Debug.LogError($"{t.Name}.{method.Name}: ActorEvent callbacks must take a single parameter derived from ActorEvent");
                            continue;
                        }

                        var eventType = parameters[0].ParameterType;
                        if (!typeof(ActorEvent).IsAssignableFrom(eventType))
                        {
                            Debug.LogError($"{t.Name}.{method.Name}: ActorEvent callbacks must take a single parameter derived from ActorEvent");
                            continue;
                        }

                        tempHandlersList.Add(new ActorEventHandler
                        {
                            priority     = attr.priority,
                            autoRegister = attr.autoRegister,
                            callback     = ActorEventDelegate.Create(method),
                            eventType    = eventType
                        });
                    }

                    var fields = t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                    foreach (var field in fields)
                    {
                        var attrs = field.GetCustomAttributes(typeof(ActorDependencyAttribute), true);
                        if (attrs.Length == 0)
                        {
                            continue;
                        }

                        var attr = attrs[0] as ActorDependencyAttribute;
                        if (null == attr)
                        {
                            continue;
                        }

                        tempDependencyList.Add(new ActorComponentInfo.Dependency
                        {
                            field         = field,
                            componentType = field.FieldType
                        });
                    }
                }

                infoByType[type] = info = new ActorComponentInfo(tempHandlersList.ToArray(), tempDependencyList.ToArray());
                tempHandlersList.Clear();
            }
            else
            {
                info = existingInfo;
            }

            actor.RegisterComponent(this);

            foreach (var dependency in info.injection)
            {
                dependency.field.SetValue(this, GetActorComponent(dependency.componentType));
            }
        }