unity事件系统(广播系统)的学习笔记

最近在研究广播系统,写下来记录一下,好记性不如烂笔头。简单写了无参数广播事件系统

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


public delegate void CallBack();//需要有参数的直接在这加上需要的参数就可以实现参数广播。
public enum EventTypee//广播事件
{
    test
}
public class EventCenter : MonoBehaviour
{
    private static Dictionary<EventTypee, Delegate> dic = new Dictionary<EventTypee, Delegate>();
    /// <summary>
    /// 添加事件监听
    /// </summary>
    /// <param name="eventType"></param>
    /// <param name="callBack"></param>
    public static void AddListener(EventTypee eventTypee,CallBack callBack)
    {
        if (!dic.ContainsKey(eventTypee))
        {
            dic.Add(eventTypee, null);
        }
        //异常抛出
        Delegate m = dic[eventTypee];
        if (m != null && m.GetType() != callBack.GetType())
        {
            throw new Exception(string.Format("事件{0}为不同委托,对应为{1},添加类型为{2}", eventTypee, m.GetType(), callBack.GetType()));
        }
        dic[eventTypee] = (CallBack)dic[eventTypee] + callBack;
        
    }
    /// <summary>
    /// 移除监听事件
    /// </summary>
    /// <param name="eventTypee"></param>
    /// <param name="callBack"></param>
    public static void RemoveListener(EventTypee eventTypee,CallBack callBack)
    {
        //异常抛出
        if (dic.ContainsKey(eventTypee))
        {
            Delegate m = dic[eventTypee];
            if(m == null)
            {
                throw new Exception(string.Format("错误;事件{0}没有添加", eventTypee));
            }
            else if (m.GetType()!= callBack.GetType())
            {
                throw new Exception(string.Format("移除错误,事件{0}于事件{1}为不同委托", m.GetType(), callBack.GetType()));
            }
            
        }
        else
        {
            throw new Exception(string.Format("错误,没有事件{0}", eventTypee));
        }
        dic[eventTypee] = (CallBack)dic[eventTypee] - callBack;
        if (dic[eventTypee] == null)
        {
            dic.Remove(eventTypee);
        }
    }
    /// <summary>
    /// 事件的广播
    /// </summary>
    /// <param name="eventTypee"></param>
    public static void Broadcast(EventTypee eventTypee)
    {
        Delegate m;
        if (dic.TryGetValue(eventTypee, out m))
        {
            CallBack callBack = m as CallBack;
            if(callBack != null)
            {
                callBack();
            }
            else
            {
                throw new Exception(string.Format("错误,事件已存在", eventTypee));
            }
        }
    }
}

测试一下

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

public class NewBehaviourScript : MonoBehaviour
{
        
    private void Awake()
    {
        EventCenter.AddListener(EventTypee.test, Show1);
    }
   
    private void Show1()
    {
        Debug.Log("广播成功");
    }
   
    private void OnDestroy()
    {
        EventCenter.RemoveListener(EventTypee.test, Show1);
    }
    public void OnShowBtnClick()
    {
        EventCenter.Broadcast(EventTypee.test);
    }
}

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45072334/article/details/106328316