Unity | 通过C#的委托方法实现的Unity事件系统

0 前言

在最初对于Unity的学习中,常用的数据交互和通讯方式往往是通过GameObject与GameObjet,Component与Component之间的交互进行的,这样的方式在程序结构上耦合性很高,代码的复用性很差,相对的,我们可以利用类似Unity中自带的事件系统的方法作为数据传递的手段来对程序结构进行解耦。

1 程序结构

  • EventSystem
    • CallBack.cs : Delegate Class
    • EventCenter.cs : Class
    • EventType.cs : Enum

2 CallBack类

首先需要构建一个CallBack类建立几种回调方法的容器:
CallBack.cs

public delegate void CallBack();
public delegate void CallBack<T>(T arg);
public delegate void CallBack<T, X>(T args1, X args2);
public delegate void CallBack<T, X, Y>(T args1, X args2, Y args3);
public delegate void CallBack<T, X, Y, Z>(T args1, X args2, Y args3, Z args4);
public delegate void CallBack<T, X, Y, Z, W>(T args1, X args2, Y args3, Z args4, W args5);

这里声明了可以传递0-5个任意类型参数的泛型委托方法作为事件传输参数的容器,这里对标Unity的Action类,Action类中有0-4个参数的委托方法,而游戏中常用的参数数量一般最多在5个,在这里对Action方法进行扩展,可以使用5个参数的委托方法。

3 EventCenter类

EventCenter类在这个结构中的角色是委托方法传递的分发中心,作为消息传递过程的中转站,将委托传递到Listener所在的实例中。

这个类在本质上是一个存储了委托方法和相应的事件类型的字典,其余方法用来将委托存入字典或取出字典,并执行相应的委托方法。

EventCenter类同样对于每一种CallBack类型做了方法重载,对于每一种参数类型需要一个AddListener方法,RemoveListener方法和Broadcast方法,对存入字典中的CallBack委托方法进行删除或调用。

  • AddListener方法用来将EventType类的CallBack加入到同类型的Delegate中,形成组播。
  • RemoveListener方法在逻辑上与AddListener相反,将与字典中Delegate同类型的CallBack移除出组播,移除之后移除相应的EventType,完成移除。
  • Broadcast方法用来从字典中取出并执行Eventtype对应的委托方法。
  • 对应的Adding,Removing,Removed方法都是在添加和执行过程中进行存在性和冲突检测的,将其整理变为方法,在使用时调用方法以节省代码行数。

EventCenter.cs

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

public class EventCenter
{
    private static Dictionary<EventType, Delegate> m_EventTable = new Dictionary<EventType, Delegate>();

    private static void OnListenerAdding(EventType eventType, Delegate callBack)
    {
        if (!m_EventTable.ContainsKey(eventType))
        {
            m_EventTable.Add(eventType, null);
        }

        // 在这里对比一下callBack的类型是否与[eventType]对应的类型相同
        var @delegate = m_EventTable[eventType];
        if (@delegate != null && @delegate.GetType() != callBack.GetType())
        {
            throw new Exception($"尝试为事件{eventType}添加不同类型的委托,当前事件对应的委托是{@delegate.GetType()},要添加的委托类型是{callBack.GetType()}");
        }
    }

    private static void OnListenerRemoving(EventType eventType, Delegate callBack)
    {
        // 判断对应EventType是否存在,如果不存在或者类型错误则无法移除
        if (m_EventTable.ContainsKey(eventType))
        {
            var @delegate = m_EventTable[eventType];
            if (@delegate == null)
            {
                throw new Exception($"移除监听错误:事件{eventType}没有对应的委托");
            }
            else if (@delegate.GetType() != callBack.GetType())
            {
                throw new Exception(
                    $"尝试为事件移除{eventType}不同类型的委托,当前委托类型为{@delegate.GetType()},要移除的委托类型为{callBack.GetType()}");
            }
        }
        else
        {
            throw new Exception($"移除监听错误,没有事件码{eventType}");
        }
    }

    private static void OnListenerRemoved(EventType eventType)
    {
        if (m_EventTable[eventType] == null)
        {
            m_EventTable.Remove(eventType);
        }
    }

    public static void AddListener(EventType eventType, CallBack callBack)
    {
        OnListenerAdding(eventType, callBack);
        // 为当前[eventType]类型的Delegate添加一个CallBack,因为本质上CallBack就是一个delegate,所以将m_EventTable转换成CallBack类型用来进行“+”操作
        m_EventTable[eventType] = (CallBack)m_EventTable[eventType] + callBack;
    }
    
    public static void AddListener<T>(EventType eventType, CallBack<T> callBack)
    {
        OnListenerAdding(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] + callBack;
    }

    public static void AddListener<T, X>(EventType eventType, CallBack<T, X> callBack)
    {
        OnListenerAdding(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X>)m_EventTable[eventType] + callBack;
    }
    public static void AddListener<T, X, Y>(EventType eventType, CallBack<T, X, Y> callBack)
    {
        OnListenerAdding(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X, Y>)m_EventTable[eventType] + callBack;
    }
    
    public static void AddListener<T, X, Y, Z>(EventType eventType, CallBack<T, X, Y, Z> callBack)
    {
        OnListenerAdding(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X, Y, Z>)m_EventTable[eventType] + callBack;
    }
    
    public static void AddListener<T, X, Y, Z, W>(EventType eventType, CallBack<T, X, Y, Z, W> callBack)
    {
        OnListenerAdding(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X, Y, Z, W>)m_EventTable[eventType] + callBack;
    }

    // 移除监听器的方法
    public static void RemoveListener(EventType eventType, CallBack callBack)
    {
        OnListenerRemoving(eventType, callBack);
        // 移除对应类型的callback,通过-callback实现
        m_EventTable[eventType] = (CallBack)m_EventTable[eventType] - callBack;
        OnListenerRemoved(eventType);
    }
    
    public static void RemoveListener<T>(EventType eventType, CallBack<T> callBack)
    {
        OnListenerRemoving(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] - callBack;
        OnListenerRemoved(eventType);
    }
    
    public static void RemoveListener<T, X>(EventType eventType, CallBack<T, X> callBack)
    {
        OnListenerRemoving(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X>)m_EventTable[eventType] - callBack;
        OnListenerRemoved(eventType);
    }
    
    public static void RemoveListener<T, X, Y>(EventType eventType, CallBack<T, X, Y> callBack)
    {
        OnListenerRemoving(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X, Y>)m_EventTable[eventType] - callBack;
        OnListenerRemoved(eventType);
    }
    
    public static void RemoveListener<T, X, Y, Z>(EventType eventType, CallBack<T, X, Y, Z> callBack)
    {
        OnListenerRemoving(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X, Y, Z>)m_EventTable[eventType] - callBack;
        OnListenerRemoved(eventType);
    }
    
    public static void RemoveListener<T, X, Y, Z, W>(EventType eventType, CallBack<T, X, Y, Z, W> callBack)
    {
        OnListenerRemoving(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X, Y, Z, W>)m_EventTable[eventType] - callBack;
        OnListenerRemoved(eventType);
    }

    public static void Broadcast(EventType eventType)
    {
        Delegate @delegate;
        if (m_EventTable.TryGetValue(eventType, out @delegate))
        {
            var callBack = @delegate as CallBack;
            if (callBack != null)
            {
                callBack();
            }
            else
            {
                throw new Exception($"广播事件错误,事件{eventType}对应的委托有不同的类型");
            }
        }
    }
    
    public static void Broadcast<T>(EventType eventType, T arg)
    {
        Delegate @delegate;
        if (m_EventTable.TryGetValue(eventType, out @delegate))
        {
            var callBack = @delegate as CallBack<T>;
            if (callBack != null)
            {
                callBack(arg);
            }
            else
            {
                throw new Exception($"广播事件错误,事件{eventType}对应的委托有不同的类型");
            }
        }
    }
    
    public static void Broadcast<T, X>(EventType eventType, T args1, X args2)
    {
        Delegate @delegate;
        if (m_EventTable.TryGetValue(eventType, out @delegate))
        {
            var callBack = @delegate as CallBack<T, X>;
            if (callBack != null)
            {
                callBack(args1, args2);
            }
            else
            {
                throw new Exception($"广播事件错误,事件{eventType}对应的委托有不同的类型");
            }
        }
    }
    
    public static void Broadcast<T, X, Y>(EventType eventType, T args1, X args2, Y args3)
    {
        Delegate @delegate;
        if (m_EventTable.TryGetValue(eventType, out @delegate))
        {
            var callBack = @delegate as CallBack<T, X, Y>;
            if (callBack != null)
            {
                callBack(args1, args2, args3);
            }
            else
            {
                throw new Exception($"广播事件错误,事件{eventType}对应的委托有不同的类型");
            }
        }
    }
    
    public static void Broadcast<T, X, Y, Z>(EventType eventType, T args1, X args2, Y args3, Z args4)
    {
        Delegate @delegate;
        if (m_EventTable.TryGetValue(eventType, out @delegate))
        {
            var callBack = @delegate as CallBack<T, X, Y, Z>;
            if (callBack != null)
            {
                callBack(args1, args2, args3, args4);
            }
            else
            {
                throw new Exception($"广播事件错误,事件{eventType}对应的委托有不同的类型");
            }
        }
    }
    
    public static void Broadcast<T, X, Y, Z, W>(EventType eventType, T args1, X args2, Y args3, Z args4, W args5)
    {
        Delegate @delegate;
        if (m_EventTable.TryGetValue(eventType, out @delegate))
        {
            var callBack = @delegate as CallBack<T, X, Y, Z, W>;
            if (callBack != null)
            {
                callBack(args1, args2, args3, args4, args5);
            }
            else
            {
                throw new Exception($"广播事件错误,事件{eventType}对应的委托有不同的类型");
            }
        }
    }
}

3 EventType枚举

该枚举类作为EventDictionary中每个委托的的唯一key存在。
EventType.cs

public enum EventType
{
	EventType_1,
	EventType_2,
	...
}

4 使用方法

  1. 在EventType中添加一个不重复的Key,作为判断委托事件的标识。
public enum EventType
{
	EventType_1
}
  1. 为需要调用事件的实例中添加一个响应事件的Listener,以及相应的委托方法。
EventCenter.AddListener<Type>(EventType.EventType_1, Function);

void Function(Type elem){
	...
}
  1. 在希望使用委托方法的代码段中调用EventCenter的Broadcast方法进行广播,以此在Listener中调用对应的委托方法。
EventCenter.Broadcast(EventType.EventType_1);
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

catalpa_ovis

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

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

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

打赏作者

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

抵扣说明:

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

余额充值