Unity3D–委托事件的封装使用
1:CallBack.cs //委托类
public delegate void CallBack();
public delegate void CallBack<T>(T arg);
2. EventType.cs //枚举类型
public enum EventType
{
ShowText,
Printf
}
3.EventCenter.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class EventCenter
{
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}添加不同类型的委托,当前事件