public enum EventType { None, ButtonPress, KeyPress, MouseClick } public class EventManager : MonoBehaviour { public delegate void EventDelegate(EventType eventType); private Dictionary<EventType, EventDelegate> eventDictionary; private static EventManager eventManager; public static EventManager instance { get { if (!eventManager) { eventManager = FindObjectOfType(typeof(EventManager)) as EventManager; if (!eventManager) { Debug.LogError("There needs to be one active EventManager script on a GameObject in your scene."); } else { eventManager.Init(); } } return eventManager; } } void Init() { if (eventDictionary == null) { eventDictionary = new Dictionary<EventType, EventDelegate>(); } } public static void StartListening(EventType eventType, EventDelegate listener) { EventDelegate thisEvent; if (instance.eventDictionary.TryGetValue(eventType, out thisEvent)) { thisEvent += listener; instance.eventDictionary[eventType] = thisEvent; } else { thisEvent += listener; instance.eventDictionary.Add(eventType, thisEvent); } } public static void StopListening(EventType eventType, EventDelegate listener) { if (eventManager == null) return; EventDelegate thisEvent; if (instance.eventDictionary.TryGetValue(eventType, out thisEvent)) { thisEvent -= listener; instance.eventDictionary[eventType] = thisEvent; } } public static void TriggerEvent(EventType eventType) { EventDelegate thisEvent = null; if (instance.eventDictionary.TryGetValue(eventType, out thisEvent)) { thisEvent.Invoke(eventType); } } }添加注释
时间: 2024-02-10 18:12:40 浏览: 35
// 定义事件类型枚举
public enum EventType {
None, // 无事件类型
ButtonPress, // 按钮点击事件
KeyPress, // 按键按下事件
MouseClick // 鼠标点击事件
}
public class EventManager : MonoBehaviour {
// 定义事件委托
public delegate void EventDelegate(EventType eventType);
// 事件类型与事件委托的字典
private Dictionary<EventType, EventDelegate> eventDictionary;
// 静态的事件管理器实例
private static EventManager eventManager;
// 获取事件管理器实例的属性
public static EventManager instance {
get {
// 如果事件管理器实例不存在,则查找场景中是否存在 EventManager 脚本
if (!eventManager) {
eventManager = FindObjectOfType(typeof(EventManager)) as EventManager;
// 如果不存在,则输出错误信息
if (!eventManager) {
Debug.LogError("There needs to be one active EventManager script on a GameObject in your scene.");
}
// 如果存在,则初始化事件字典
else {
eventManager.Init();
}
}
return eventManager;
}
}
// 初始化事件字典
void Init() {
if (eventDictionary == null) {
eventDictionary = new Dictionary<EventType, EventDelegate>();
}
}
// 添加事件监听器
public static void StartListening(EventType eventType, EventDelegate listener) {
EventDelegate thisEvent;
if (instance.eventDictionary.TryGetValue(eventType, out thisEvent)) {
thisEvent += listener;
instance.eventDictionary[eventType] = thisEvent;
} else {
thisEvent += listener;
instance.eventDictionary.Add(eventType, thisEvent);
}
}
// 移除事件监听器
public static void StopListening(EventType eventType, EventDelegate listener) {
if (eventManager == null) return;
EventDelegate thisEvent;
if (instance.eventDictionary.TryGetValue(eventType, out thisEvent)) {
thisEvent -= listener;
instance.eventDictionary[eventType] = thisEvent;
}
}
// 触发事件
public static void TriggerEvent(EventType eventType) {
EventDelegate thisEvent = null;
if (instance.eventDictionary.TryGetValue(eventType, out thisEvent)) {
thisEvent.Invoke(eventType);
}
}
}