Unity高度解耦合性——监听与广播系统

创建系统代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class EventCreat 
{
    //创建字典,第一个参数为事件码 第二个参数为委托
    private static Dictionary<EventType, Delegate> m_EventTable = new Dictionary<EventType, Delegate>();

    /// <summary>
    /// 代码合并
    /// </summary>
    //监听方法的精简
    private static void AddListening(EventType eventType, Delegate callBack)
    {
        //判断事件码是否存在字典中
        if (!m_EventTable.ContainsKey(eventType))//如果不存在
        {
            m_EventTable.Add(eventType, null);//添加到字典中
        }
        //判断事件码对应的委托是否和需要添加的委托对应
        Delegate d = m_EventTable[eventType];//拿到事件码对应的委托
        if (d != null && d.GetType() != callBack.GetType())//如果d不等于null且 两个委托的参数类型等等不一致
        {
            throw new Exception(string.Format("尝试为时间{0}添加不同类型的委托,当前事件对应的委托为{1},要添加的事件委托为{2}", eventType, d.GetType(), callBack.GetType()));
        }
    }
    //移除监听方法的精简
    private static void RemovListening(EventType eventType, Delegate callBack)
    {
        if (m_EventTable.ContainsKey(eventType))
        {
            Delegate d = m_EventTable[eventType];
            if (d == null)
            {
                throw new Exception(string.Format("移除监听错误:事件{0}没有对应的委托", eventType));
            }
            else if (d.GetType() != callBack.GetType())
            {
                throw new Exception(string.Format("移除监听错误:尝试为时间{0}移除不同类型的委托,当前事件委托类型为{1},要移除的事件委托为{2}", eventType, d.GetType(), callBack.GetType()));
            }
        }
        else
        {
            throw new Exception(string.Format("移除监听错误:没有事件码{0}", eventType));
        }
    }
    private static void RemoveListeningTwo(EventType eventType)
    {
        if (m_EventTable[eventType] == null)
        {
            m_EventTable.Remove(eventType);
        }
    }


    //创建无参的监听方法
    public static void AddListener(EventType eventType, CallBack callBack)
    {
        AddListening(eventType,callBack);
        //两个委托一致,进行添加
        m_EventTable[eventType] = (CallBack)m_EventTable[eventType] + callBack;
    }
    //移除监听的方法(无参)
    public static void RemoveListener(EventType eventType,CallBack callBack)
    {
        RemovListening(eventType,callBack);
        m_EventTable[eventType] = (CallBack)m_EventTable[eventType] - callBack;
        RemoveListeningTwo(eventType);
    }
    //广播方法(无参)
    public static void BroadCast(EventType eventType)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventType,out d))
        {
           CallBack callBack=d as CallBack;
            if (callBack!=null)
            {
                callBack();
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应的委托具有不同的类型",eventType));
            }
        }
    }


    //创建一个参数的监听方法
    public static void AddListener<T>(EventType eventType, CallBack<T> callBack)
    {
        AddListening(eventType,callBack);
        //两个委托一致,进行添加
        m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] + callBack;
    }
    //移除监听的方法(一个参数)
    public static void RemoveListener<T>(EventType eventType, CallBack<T> callBack)
    {
        RemovListening(eventType,callBack);
         m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] - callBack;
        RemoveListeningTwo(eventType);
    }
    //广播方法(一个参数)
    public static void BroadCast<T>(EventType eventType,T arg)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventType, out d))
        {
            CallBack<T> callBack = d as CallBack<T>;
            if (callBack != null)
            {
                callBack(arg);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应的委托具有不同的类型", eventType));
            }
        }
    }


    //创建两个参数的监听方法
    public static void AddListener<T,X>(EventType eventType, CallBack<T,X> callBack)
    {
        AddListening(eventType, callBack);
        //两个委托一致,进行添加
        m_EventTable[eventType] = (CallBack<T,X>)m_EventTable[eventType] + callBack;
    }
    //移除监听的方法(两个参数)
    public static void RemoveListener<T,X>(EventType eventType, CallBack<T,X> callBack)
    {
        RemovListening(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T,X>)m_EventTable[eventType] - callBack;
        RemoveListeningTwo(eventType);
    }
    //广播方法(两个参数)
    public static void BroadCast<T, X>(EventType eventType, T arg, X arg1)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventType, out d))
        {
            CallBack<T,X> callBack = d as CallBack<T,X>;
            if (callBack != null)
            {
                callBack(arg,arg1);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应的委托具有不同的类型", eventType));
            }
        }
    }


    //创建三个参数的监听方法
    public static void AddListener<T, X,Y>(EventType eventType, CallBack<T, X,Y> callBack)
    {
        AddListening(eventType, callBack);
        //两个委托一致,进行添加
        m_EventTable[eventType] = (CallBack<T, X,Y>)m_EventTable[eventType] + callBack;
    }
    //移除监听的方法(三个参数)
    public static void RemoveListener<T, X,Y>(EventType eventType, CallBack<T, X,Y> callBack)
    {
        RemovListening(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X,Y>)m_EventTable[eventType] - callBack;
        RemoveListeningTwo(eventType);
    }
    //广播方法(三个参数)
    public static void BroadCast<T, X,Y>(EventType eventType, T arg, X arg1,Y arg2)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventType, out d))
        {
            CallBack<T, X,Y> callBack = d as CallBack<T, X,Y>;
            if (callBack != null)
            {
                callBack(arg, arg1,arg2);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应的委托具有不同的类型", eventType));
            }
        }
    }

    //创建四个参数的监听方法
    public static void AddListener<T, X, Y,Z>(EventType eventType, CallBack<T, X, Y,Z> callBack)
    {
        AddListening(eventType, callBack);
        //两个委托一致,进行添加
        m_EventTable[eventType] = (CallBack<T, X, Y,Z>)m_EventTable[eventType] + callBack;
    }
    //移除监听的方法(四个参数)
    public static void RemoveListener<T, X, Y,Z>(EventType eventType, CallBack<T, X, Y,Z> callBack)
    {
        RemovListening(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X, Y,Z>)m_EventTable[eventType] - callBack;
        RemoveListeningTwo(eventType);
    }
    //广播方法(四个参数)
    public static void BroadCast<T, X, Y,Z>(EventType eventType, T arg, X arg1, Y arg2,Z arg3)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventType, out d))
        {
            CallBack<T, X, Y,Z> callBack = d as CallBack<T, X, Y,Z>;
            if (callBack != null)
            {
                callBack(arg, arg1, arg2,arg3);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应的委托具有不同的类型", eventType));
            }
        }
    }

    //创建五个参数的监听方法
    public static void AddListener<T, X, Y, Z,W>(EventType eventType, CallBack<T, X, Y, Z,W> callBack)
    {
        AddListening(eventType, callBack);
        //两个委托一致,进行添加
        m_EventTable[eventType] = (CallBack<T, X, Y, Z,W>)m_EventTable[eventType] + callBack;
    }
    //移除监听的方法(五个参数)
    public static void RemoveListener<T, X, Y, Z,W>(EventType eventType, CallBack<T, X, Y, Z,W> callBack)
    {
        RemovListening(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X, Y, Z,W>)m_EventTable[eventType] - callBack;
        RemoveListeningTwo(eventType);
    }
    //广播方法(五个参数)
    public static void BroadCast<T, X, Y, Z,W>(EventType eventType, T arg, X arg1, Y arg2, Z arg3,W arg4)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventType, out d))
        {
            CallBack<T, X, Y, Z,W> callBack = d as CallBack<T, X, Y, Z,W>;
            if (callBack != null)
            {
                callBack(arg, arg1, arg2, arg3,arg4);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应的委托具有不同的类型", eventType));
            }
        }
    }
}

创建委托类

public delegate void CallBack();//无参委托
public delegate void CallBack<T>(T arg1);//一个参数委托
public delegate void CallBack<T,X>(T arg1,X arg2);
public delegate void CallBack<T,X,Y>(T arg1, X arg2, Y arg3);
public delegate void CallBack<T,X,Y,Z>(T arg1, X arg2, Y arg3,Z arg4);
public delegate void CallBack<T,X,Y,Z,W>(T arg1, X arg2, Y arg3, Z arg4,W arg5);

创建事件码类

public enum EventType
{
    ShowText,
}

在需要监听的地方加监听方法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ShowText : MonoBehaviour
{

    private void Awake()
    {
        gameObject.SetActive(false);
      //  EventCreat.AddListener<string,>(EventType.ShowText, Show);
        EventCreat.AddListener<string,string,float,int,int>(EventType.ShowText,Show);
    }
    private void OnDestroy()
    {
       // EventCreat.RemoveListener<string>(EventType.ShowText, Show);
        EventCreat.RemoveListener<string,string,float,int,int>(EventType.ShowText, Show);
    }

    //private void Show(string str)
    //{
    //    gameObject.SetActive(true);
    //    GetComponent<Text>().text = str;
    //}
    private void Show(string str,string s,float f,int a,int b)
    {
        gameObject.SetActive(true);
        GetComponent<Text>().text = str+s+f+a+b;
    }
}

广播

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BtnClick : MonoBehaviour
{
    private void Awake()
    {
        GetComponent<Button>().onClick.AddListener(() =>
        {
            // EventCreat.BroadCast(EventType.ShowText, "你好!");
            EventCreat.BroadCast(EventType.ShowText, "你好","呀!",1.0f,125,3);
        }); 
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值