unity消息管理

GameEvent.cs

public delegate void CallBack();
public delegate void CallBack<T>(T arg1);
public delegate void CallBack<T, K>(T arg1, K arg2);

public enum GameEvent
{
    GameBegin,
    GameEnd
}

Message.cs

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
internal static class Messager
{    //消息派发管理中心
    public static Dictionary<GameEvent, Delegate> mEventTable = new Dictionary<GameEvent, Delegate>();
    //检测是否已经有了监听的类型, 是否符合监听的类型
    private static bool OnAddListener(GameEvent eventType, Delegate handler)
    {
        if (!mEventTable.ContainsKey(eventType))
        {   //如果不包含传入的监听
            mEventTable.Add(eventType, null);
        }
        //GetType 方法返回 Type 对象,该对象表示由其完全限定名指定的类型。
        Delegate d = mEventTable[eventType];  //当前字典中已经存在的
        if (d != null && d.GetType() != handler.GetType())
        {
            Debug.LogError("a");
            return false;
        }

        return true;
    }
    public static void ClearAllMessage()
    {
        mEventTable.Clear();
    }
    private static bool OnRemoveListener(GameEvent eventType, Delegate hangler)
    {
        if (mEventTable.ContainsKey(eventType))
        {
            Delegate d = mEventTable[eventType];
            if (d == null)
            {
                Debug.LogError("null");
                return false;
            }
            else if (d.GetType() != hangler.GetType())
            {
                Debug.LogError("类型不符合");
                return false;
            }
        }
        else
        {
            //Debug.LogError("不包含要移除的对象");
            return false;
        }
        return true;
    }
    //添加监听
    public static void AddListener(GameEvent eventType, CallBack handler)
    {
        if (!OnAddListener(eventType, handler))
        {
            return;
        }
        mEventTable[eventType] = (CallBack)mEventTable[eventType] + handler; 
    }

    public static void AddListener<T>(GameEvent eventType, CallBack<T> handler)
    {
        if (!OnAddListener(eventType, handler))
        {
            return;
        }
        mEventTable[eventType] = (CallBack<T>)mEventTable[eventType] + handler;
    }

    public static void AddListener<T,K>(GameEvent eventType,CallBack<T,K> handler)
    {
        if(!OnAddListener(eventType,handler))
        {
            return;
        }
        mEventTable[eventType] = (CallBack<T, K>)mEventTable[eventType] + handler;
    }

    public static void RemoveListener(GameEvent eventType, CallBack handler)
    {
        if (!OnRemoveListener(eventType, handler))
        {
            return;
        }
        mEventTable[eventType] = (CallBack)mEventTable[eventType] - handler;
        if (mEventTable[eventType] == null)
        {
            mEventTable.Remove(eventType);
        }
    }
    public static void RemoveListener<T>(GameEvent eventType, CallBack<T> handler)
    {
        if (!OnRemoveListener(eventType, handler))
        {
            return;
        }
        mEventTable[eventType] = (CallBack<T>)mEventTable[eventType] - handler;
        if (mEventTable[eventType] == null)
        {
            mEventTable.Remove(eventType);
        }
    }

    /// <summary>
    /// 移除两个参数的事件
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="K"></typeparam>
    /// <param name="eventType"></param>
    /// <param name="handler"></param>
    public static void RemoveListener<T,K>(GameEvent eventType, CallBack<T,K> handler)
    {
        if (!OnRemoveListener(eventType, handler))
        {
            return;
        }
        mEventTable[eventType] = (CallBack<T,K>)mEventTable[eventType] - handler;
        if (mEventTable[eventType] == null)
        {
            mEventTable.Remove(eventType);
        }
    }

    //广播监听
    public static void Broadcast(GameEvent eventType)
    {
        if (!mEventTable.ContainsKey(eventType))
        {  //不包含要广播的监听
            return;
        }
        Delegate d;
        if (mEventTable.TryGetValue(eventType, out d))
        {   //d为一个函数
            CallBack callback = d as CallBack;
            if (callback != null)
            {
                callback();
            }
            else
            {
                Debug.LogError("广播" + eventType + "为空");
            }
        }
    }
    public static void Broadcast<T>(GameEvent eventType, T arg1)
    {
        if (!mEventTable.ContainsKey(eventType))
        {
            return;
        }
        Delegate d;
        if (mEventTable.TryGetValue(eventType, out d))
        {
            CallBack<T> callback = d as CallBack<T>;
            if (callback != null)
            {
                callback(arg1);
            }
            else
            {
                Debug.LogError("广播" + eventType + "为空");
            }
        }
    }
    public static void Broadcast<T,K>(GameEvent eventType, T arg1,K arg2)
    {
        if (!mEventTable.ContainsKey(eventType))
        {
            return;
        }
        Delegate d;
        if (mEventTable.TryGetValue(eventType, out d))
        {
            CallBack<T, K> callback = d as CallBack<T, K>;
            if (callback != null)
            {
                callback(arg1,arg2);
            }
            else
            {
                Debug.LogError("广播" + eventType + "为空");
            }
        }
    }
}

测试脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test1 : MonoBehaviour
{
    void Start()
    {
        Messager.AddListener(GameEvent.GameBegin, OnGameBegin);
        Messager.AddListener<string>(GameEvent.GameEnd, OnGameEnd);
    }
    private void OnGameBegin()
    {
        Debug.Log("GameBegin");
    }
    private void OnGameEnd(string str)
    {
        Debug.Log(str);
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.B))
        {
            Messager.Broadcast(GameEvent.GameBegin);
        }
        if (Input.GetKeyDown(KeyCode.E))
        {
            Messager.Broadcast(GameEvent.GameEnd,"GameEnd");
        }
    }
}

猜你喜欢

转载自blog.csdn.net/LuffyZ/article/details/106278818
今日推荐