Unity_内部消息机制

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using GameType;

public class Messenger{

    private static Dictionary<ClickType, Delegate> mDict = new Dictionary<ClickType, Delegate>();

    private static bool OnAddListener(ClickType type,Delegate handler)
    {
        if (!mDict.ContainsKey(type))
        {
            mDict.Add(type, null);
        }
        Delegate d = mDict[type];
        if (d != null && d.GetType()!=handler.GetType())
        {
            return false;
        }
        return true;
    }

    private static bool OnRemoveListener(ClickType type,Delegate handler)
    {
        if (mDict.ContainsKey(type))
        {
            Delegate d = mDict[type];
            if (d==null)
            {
                return false;
            }
            else if (d.GetType()!=handler.GetType())
            {
                return false;
            }
        }
        else
        {
            return false;
        }
        return true;
    }

    public static void AddListener(ClickType type,Callback handler)
    {
        if (!OnAddListener(type,handler))
        {
            return;
        }
        mDict[type] = (Callback)mDict[type] + handler;
    }

    public static void AddListener<T>(ClickType type,Callback<T> handler)
    {
        if (!OnAddListener(type,handler))
        {
            return;
        }
        mDict[type] = (Callback<T>)mDict[type] + handler;
    }

    public static void RemoveListener(ClickType type,Callback handler)
    {
        if (!OnRemoveListener(type,handler))
        {
            return;
        }
        mDict[type] = (Callback)mDict[type] - handler;
        if (mDict[type]==null)
        {
            mDict.Remove(type);
        }
    }

    public static void RemoveListener<T>(ClickType type,Callback<T> handler)
    {
        if (!OnRemoveListener(type, handler))
        {
            return;
        }
        mDict[type] = (Callback<T>)mDict[type] - handler;
        if (mDict[type] == null)
        {
            mDict.Remove(type);
        }
    }

    public static void Broadcast(ClickType type)
    {
        if (!mDict.ContainsKey(type))
        {
            return;
        }
        Delegate d;
        if(mDict.TryGetValue(type, out d))
        {
            Callback callback = d as Callback;
            if (callback != null)
                callback();
            else
                return;
        }
    }

    public static void Broadcast<T>(ClickType type,T arg)
    {
        if (!mDict.ContainsKey(type))
        {
            return;
        }
        Delegate d;
        if (mDict.TryGetValue(type, out d))
        {
            Callback<T> callback = d as Callback<T>;
            if (callback != null)
                callback(arg);
            else
                return;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值