监听通知事件

1.Messager

通知类,使用Messenger.AddListener()来进行事件的注册。Messenger.Broadcast()进行事件的通知Messenger.RemoveListener()来移除事件。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using UnityEngine;

public enum MessengerMode
{
    DONT_REQUIRE_LISTENER,
    REQUIRE_LISTENER,
}

static internal class MessengerInternal
{
    readonly public static Dictionary<string, Delegate> eventTable = new Dictionary<string, Delegate>();
    static public MessengerMode DEFAULT_MODE = MessengerMode.REQUIRE_LISTENER;

    static public void AddListener(string eventType, Delegate callback)
    {
        MessengerInternal.OnListenerAdding(eventType, callback);
        eventTable[eventType] = Delegate.Combine(eventTable[eventType], callback);
    }

    static public void RemoveListener(string eventType, Delegate handler)
    {
        MessengerInternal.OnListenerRemoving(eventType, handler);
        eventTable[eventType] = Delegate.Remove(eventTable[eventType], handler);
        MessengerInternal.OnListenerRemoved(eventType);
    }

    static public T[] GetInvocationList<T>(string eventType)
    {
        Delegate d;
        if (eventTable.TryGetValue(eventType, out d))
        {
            try
            {
                return d.GetInvocationList().Cast<T>().ToArray();
            }
            catch
            {
                throw MessengerInternal.CreateBroadcastSignatureException(eventType);
            }
        }
        return new T[0];
    }

    static public void OnListenerAdding(string eventType, Delegate listenerBeingAdded)
    {
        if (!eventTable.ContainsKey(eventType))
        {
            eventTable.Add(eventType, null);
        }

        var d = eventTable[eventType];
        if (d != null && d.GetType() != listenerBeingAdded.GetType())
        {
            throw new ListenerException(string.Format("Attempting to add listener with inconsistent signature for event type {0}. Current listeners have type {1} and listener being added has type {2}", eventType, d.GetType().Name, listenerBeingAdded.GetType().Name));
        }
    }

    static public void OnListenerRemoving(string eventType, Delegate listenerBeingRemoved)
    {
        if (eventTable.ContainsKey(eventType))
        {
            var d = eventTable[eventType];

            if (d == null)
            {
                throw new ListenerException(string.Format("Attempting to remove listener with for event type {0} but current listener is null.", eventType));
            }
            else if (d.GetType() != listenerBeingRemoved.GetType())
            {
                throw new ListenerException(string.Format("Attempting to remove listener with inconsistent signature for event type {0}. Current listeners have type {1} and listener being removed has type {2}", eventType, d.GetType().Name, listenerBeingRemoved.GetType().Name));
            }
        }
        else
        {
            throw new ListenerException(string.Format("Attempting to remove listener for type {0} but Messenger doesn't know about this event type.", eventType));
        }
    }

    static public void OnListenerRemoved(string eventType)
    {
        if (eventTable[eventType] == null)
        {
            eventTable.Remove(eventType);
        }
    }

    static public void OnBroadcasting(string eventType, MessengerMode mode)
    {
        if (mode == MessengerMode.REQUIRE_LISTENER && !eventTable.ContainsKey(eventType))
        {
            UnityEngine.Debugger.LogWarning(string.Format("Broadcasting message {0} but no listener found.", eventType));
            //throw new MessengerInternal.BroadcastException(string.Format("Broadcasting message {0} but no listener found.", eventType));
        }
    }

    static public BroadcastException CreateBroadcastSignatureException(string eventType)
    {
        return new BroadcastException(string.Format("Broadcasting message {0} but listeners have a different signature than the broadcaster.", eventType));
    }

    public class BroadcastException : Exception
    {
        public BroadcastException(string msg)
            : base(msg)
        {
        }
    }

    public class ListenerException : Exception
    {
        public ListenerException(string msg)
            : base(msg)
        {
        }
    }
}

/// <summary>
/// 事件注册和反注册必须成对出现 否则会有漏的问题
/// </summary>
static public class Messenger
{
    static public void AddListener(string eventType, Action handler)
    {
        MessengerInternal.AddListener(eventType, handler);
    }

    static public void AddListener<TReturn>(string eventType, Func<TReturn> handler)
    {
        MessengerInternal.AddListener(eventType, handler);
    }

    static public void RemoveListener(string eventType, Action handler)
    {
        MessengerInternal.RemoveListener(eventType, handler);
    }

    static public void RemoveListener<TReturn>(string eventType, Func<TReturn> handler)
    {
        MessengerInternal.RemoveListener(eventType, handler);
    }

    static public void Broadcast(string eventType)
    {
        Broadcast(eventType, MessengerInternal.DEFAULT_MODE);
    }

    static public void Broadcast<TReturn>(string eventType, Action<TReturn> returnCall)
    {
        Broadcast(eventType, returnCall, MessengerInternal.DEFAULT_MODE);
    }

    static public void Broadcast(string eventType, MessengerMode mode)
    {
        MessengerInternal.OnBroadcasting(eventType, mode);
        var invocationList = MessengerInternal.GetInvocationList<Action>(eventType);

        foreach (var callback in invocationList)
            callback.Invoke();
    }

    static public void Broadcast<TReturn>(string eventType, Action<TReturn> returnCall, MessengerMode mode)
    {
        MessengerInternal.OnBroadcasting(eventType, mode);
        var invocationList = MessengerInternal.GetInvocationList<Func<TReturn>>(eventType);

        foreach (var result in invocationList.Select(del => del.Invoke()).Cast<TReturn>())
        {
            returnCall.Invoke(result);
        }
    }
}

// One parameter
static public class Messenger<T>
{
    static public void AddListener(string eventType, Action<T> handler)
    {
        MessengerInternal.AddListener(eventType, handler);
    }

    static public void AddListener<TReturn>(string eventType, Func<T, TReturn> handler)
    {
        MessengerInternal.AddListener(eventType, handler);
    }

    static public void RemoveListener(string eventType, Action<T> handler)
    {
        MessengerInternal.RemoveListener(eventType, handler);
    }

    static public void RemoveListener<TReturn>(string eventType, Func<T, TReturn> handler)
    {
        MessengerInternal.RemoveListener(eventType, handler);
    }

    static public void Broadcast(string eventType, T arg1)
    {
        Broadcast(eventType, arg1, MessengerInternal.DEFAULT_MODE);
    }

    static public void Broadcast<TReturn>(string eventType, T arg1, Action<TReturn> returnCall)
    {
        Broadcast(eventType, arg1, returnCall, MessengerInternal.DEFAULT_MODE);
    }

    static public void Broadcast(string eventType, T arg1, MessengerMode mode)
    {
        MessengerInternal.OnBroadcasting(eventType, mode);
        var invocationList = MessengerInternal.GetInvocationList<Action<T>>(eventType);

        foreach (var callback in invocationList)
            callback.Invoke(arg1);
    }

    static public void Broadcast<TReturn>(string eventType, T arg1, Action<TReturn> returnCall, MessengerMode mode)
    {
        MessengerInternal.OnBroadcasting(eventType, mode);
        var invocationList = MessengerInternal.GetInvocationList<Func<T, TReturn>>(eventType);

        foreach (var result in invocationList.Select(del => del.Invoke(arg1)).Cast<TReturn>())
        {
            returnCall.Invoke(result);
        }
    }
}


// Two parameters
static public class Messenger<T, U>
{
    static public void AddListener(string eventType, Action<T, U> handler)
    {
        MessengerInternal.AddListener(eventType, handler);
    }

    static public void AddListener<TReturn>(string eventType, Func<T, U, TReturn> handler)
    {
        MessengerInternal.AddListener(eventType, handler);
    }

    static public void RemoveListener(string eventType, Action<T, U> handler)
    {
        MessengerInternal.RemoveListener(eventType, handler);
    }

    static public void RemoveListener<TReturn>(string eventType, Func<T, U, TReturn> handler)
    {
        MessengerInternal.RemoveListener(eventType, handler);
    }

    static public void Broadcast(string eventType, T arg1, U arg2)
    {
        Broadcast(eventType, arg1, arg2, MessengerInternal.DEFAULT_MODE);
    }

    static public void Broadcast<TReturn>(string eventType, T arg1, U arg2, Action<TReturn> returnCall)
    {
        Broadcast(eventType, arg1, arg2, returnCall, MessengerInternal.DEFAULT_MODE);
    }

    static public void Broadcast(string eventType, T arg1, U arg2, MessengerMode mode)
    {
        MessengerInternal.OnBroadcasting(eventType, mode);
        var invocationList = MessengerInternal.GetInvocationList<Action<T, U>>(eventType);

        foreach (var callback in invocationList)
            callback.Invoke(arg1, arg2);
    }

    static public void Broadcast<TReturn>(string eventType, T arg1, U arg2, Action<TReturn> returnCall, MessengerMode mode)
    {
        MessengerInternal.OnBroadcasting(eventType, mode);
        var invocationList = MessengerInternal.GetInvocationList<Func<T, U, TReturn>>(eventType);

        foreach (var result in invocationList.Select(del => del.Invoke(arg1, arg2)).Cast<TReturn>())
        {
            returnCall.Invoke(result);
        }
    }
}


// Three parameters
static public class Messenger<T, U, V>
{
    static public void AddListener(string eventType, Action<T, U, V> handler)
    {
        MessengerInternal.AddListener(eventType, handler);
    }

    static public void AddListener<TReturn>(string eventType, Func<T, U, V, TReturn> handler)
    {
        MessengerInternal.AddListener(eventType, handler);
    }

    static public void RemoveListener(string eventType, Action<T, U, V> handler)
    {
        MessengerInternal.RemoveListener(eventType, handler);
    }

    static public void RemoveListener<TReturn>(string eventType, Func<T, U, V, TReturn> handler)
    {
        MessengerInternal.RemoveListener(eventType, handler);
    }

    static public void Broadcast(string eventType, T arg1, U arg2, V arg3)
    {
        Broadcast(eventType, arg1, arg2, arg3, MessengerInternal.DEFAULT_MODE);
    }

    static public void Broadcast<TReturn>(string eventType, T arg1, U arg2, V arg3, Action<TReturn> returnCall)
    {
        Broadcast(eventType, arg1, arg2, arg3, returnCall, MessengerInternal.DEFAULT_MODE);
    }

    static public void Broadcast(string eventType, T arg1, U arg2, V arg3, MessengerMode mode)
    {
        MessengerInternal.OnBroadcasting(eventType, mode);
        var invocationList = MessengerInternal.GetInvocationList<Action<T, U, V>>(eventType);

        foreach (var callback in invocationList)
            callback.Invoke(arg1, arg2, arg3);
    }

    static public void Broadcast<TReturn>(string eventType, T arg1, U arg2, V arg3, Action<TReturn> returnCall, MessengerMode mode)
    {
        MessengerInternal.OnBroadcasting(eventType, mode);
        var invocationList = MessengerInternal.GetInvocationList<Func<T, U, V, TReturn>>(eventType);

        foreach (var result in invocationList.Select(del => del.Invoke(arg1, arg2, arg3)).Cast<TReturn>())
        {
            returnCall.Invoke(result);
        }
    }
}

2.MessageEvent类

用来添加事件的类型

/// <summary>
/// 事件定义
/// </summary>
public class MessageEvent
{}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值