unity事件处理

该文章介绍了一个Unity工具类EventCenterUtil,用于事件的广播和监听管理。它包含对无参和有参事件的处理,使用委托进行回调,并提供了添加、移除监听以及广播事件的方法。在添加和移除监听时,会检查委托类型的一致性,确保事件处理的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法调用

//发送事件 【发送事件码,发送消息内容】 
EventCenterUtil.Broadcast(EventCenterUtil.EventType.Joystick, ui);

//监听无参事件
EventCenterUtil.AddListener(EventCenterUtil.EventType.Joystick, show);
public void show(){}




 //发送事件 有参事件
EventCenterUtil.Broadcast(EventCenterUtil.EventType.Joystick, ui, "你好");

EventCenterUtil.AddListener<int>(EventCenterUtil.EventType.Joystick, show);

    public void show(string i)
    { }

unity工具类

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static Unity.VisualScripting.FlowStateWidget;
public class EventCenterUtil
{

    public enum EventType
    {
        Joystick
    }

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

    private static Dictionary<EventType, Delegate> m_EventTable = new Dictionary<EventType, Delegate>();

    /**
     * 添加监听
     * **/
    public static void AddListener(EventType eventType, CallBack callBack)
    {
        if (!m_EventTable.ContainsKey(eventType))
        {
            m_EventTable.Add(eventType, null);
        }
        Delegate d = m_EventTable[eventType];
        if (d != null && d.GetType() != callBack.GetType())
        {
            throw new Exception(string.Format("尝试为事件{0}添加不同类型的委托,当前事件所对应的委托是{1}," +
                "要添加的委托类型为{2}", eventType, d.GetType(), callBack.GetType()));
        }
        m_EventTable[eventType] = (CallBack)m_EventTable[eventType] + callBack;
    }

    public static void AddListener<T>(EventType eventType, CallBack<T> callBack)
    {
        if (!m_EventTable.ContainsKey(eventType))
        {
            m_EventTable.Add(eventType, null);
        }
        Delegate d = m_EventTable[eventType];
        if (d != null && d.GetType() != callBack.GetType())
        {
            throw new Exception(string.Format("尝试为事件{0}添加不同类型的委托,当前事件所对应的委托是{1}," +
                "要添加的委托类型为{2}", eventType, d.GetType(), callBack.GetType()));
        }
        m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] + callBack;
    }
    public static void AddListener<T,X>(EventType eventType, CallBack<T, X> callBack)
    {
        if (!m_EventTable.ContainsKey(eventType))
        {
            m_EventTable.Add(eventType, null);
        }
        Delegate d = m_EventTable[eventType];
        if (d != null && d.GetType() != callBack.GetType())
        {
            throw new Exception(string.Format("尝试为事件{0}添加不同类型的委托,当前事件所对应的委托是{1}," +
                "要添加的委托类型为{2}", eventType, d.GetType(), callBack.GetType()));
        }
        m_EventTable[eventType] = (CallBack<T, X>)m_EventTable[eventType] + callBack;
    }




    /**
     * 移除监听
     * **/
    public static void RemoveListener(EventType eventType, CallBack 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));
        }
        m_EventTable[eventType] = (CallBack)m_EventTable[eventType] - callBack;
    }

    public static void RemoveListener<T,X>(EventType eventType, CallBack<T, X> 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));
        }
        m_EventTable[eventType] = (CallBack<T,X>)m_EventTable[eventType] - callBack;
    }
        public static void RemoveListener<T, X>(EventType eventType, CallBack<T> 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));
        }
        m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] - callBack;
    }



    /**
   * 广播监听
   * **/
    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 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 Broadcast<T,X>(EventType eventType, T arg1, X arg2)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventType, out d))
        {
            CallBack<T,X> callBack = d as CallBack<T,X>;
            if (callBack != null)
            {
                callBack(arg1, arg2);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误: 事件{0}对应委托具有不同的类型", eventType));
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MM-BD

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值