示例#1
0
        public void AddSubscriber(MessageSubscriber subscriber)
        {
            string[] messageTypes = subscriber.MessageTypes;
            for (int i = 0; i < messageTypes.Length; i++)
            {
                AddSubscriberToMessage(messageTypes[i], subscriber);
            }

            //UnityEngine.Debug.Log("AddSubscriber: " + subscriber.MessageTypes.ToString());
            //UnityEngine.Debug.Log("queue.Count: " + queue.Count.ToString());

            for (int i = 0; i < queue.Count; i++)
            {
                bool finded = false;
                foreach (string type in subscriber.MessageTypes)
                {
                    if (queue[i].Type == type)
                    {
                        subscriber.action(queue[i]);


                        finded = true;
                        break;
                    }
                }

                //if(finded)
                //{
                //    queue.Remove(queue[i]);
                //}
            }
        }
示例#2
0
        void AddSubscriberToMessage(string messageType,
                                    MessageSubscriber subscriber)
        {
            if (!subscriberLists.ContainsKey(messageType))
            {
                subscriberLists[messageType] =
                    new List <MessageSubscriber>();
            }

            subscriberLists[messageType].Add(subscriber);
        }
示例#3
0
        void SendMessageToSubscriber(Message message,
                                     MessageSubscriber subscriber)
        {
            if (disposable_subscibers.ContainsKey(message.Type))
            {
                foreach (var m in disposable_subscibers[message.Type])
                {
                    SendMessage(m);
                }

                disposable_subscibers[message.Type].Clear();
            }
            //UnityEngine.Debug.Log(message.Type);
            subscriber.action(message);
        }
示例#4
0
        public void Start()
        {
            var methods = obj.GetType().GetMethods().Where(
                prop => Attribute.IsDefined(prop, typeof(Subscribe)));

            foreach (System.Reflection.MethodInfo m in methods)
            {
                SubscriberAction method = (SubscriberAction)Delegate.CreateDelegate(typeof(SubscriberAction), obj, m.Name);
                Subscribe        attr   = (Subscribe)m.GetCustomAttributes(typeof(Subscribe), true)[0];

                MessageBus.MessageSubscriber subs = new MessageBus.MessageSubscriber();
                subs.action       = method;
                subs.MessageTypes = attr.subscribed_types;

                MessageBus.MessageBus.Instance.AddSubscriber(subs);
            }
        }