Health/Assets/Scripts/EventManager.cs

88 lines
2.2 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//消息管理器
public class EventManager : MonoSingleton<EventManager>
{
public delegate void EventHandler(params object[] args);
private Dictionary<YogaEventType, Action> _actionDic = new Dictionary<YogaEventType, Action>();
private Dictionary<YogaEventType, EventHandler> _actionsDic = new Dictionary<YogaEventType, EventHandler>();
public void AddEventListener(YogaEventType eventName, Action handler)
{
if (!_actionDic.ContainsKey(eventName))
{
_actionDic.Add(eventName, handler);
}
else
{
_actionDic[eventName] -= handler;
_actionDic[eventName] += handler;
}
}
public void RemoveEventListener(YogaEventType eventName, Action handler)
{
if (!_actionDic.ContainsKey(eventName))
return;
_actionDic[eventName] -= handler;
}
public void Dispatch(YogaEventType eventName)
{
if (!_actionDic.ContainsKey(eventName))
return;
_actionDic[eventName]?.Invoke();
}
//带参数的事件
public void AddEventListener(YogaEventType eventName, EventHandler handler)
{
if (!_actionsDic.ContainsKey(eventName))
{
_actionsDic.Add(eventName, handler);
}
else
{
_actionsDic[eventName] -= handler;
_actionsDic[eventName] += handler;
}
}
public void RemoveEventListener(YogaEventType eventName, EventHandler handler)
{
if (!_actionsDic.ContainsKey(eventName))
return;
_actionsDic[eventName] -= handler;
}
public void Dispatch(YogaEventType eventName, params object[] param)
{
if (!_actionsDic.ContainsKey(eventName))
return;
_actionsDic[eventName]?.Invoke(param);
}
}
public enum YogaEventType
{
StartMotionCapture,
StopMotionCapture,
EstimateAction,
ScoreUpdate,
GetActionBasePoint,
PoseEstimate,
ActionSuccess,
ActionFailed,
PlayAnimation,
UpdateProgress,
ShowHint,
ShowMessage,
ChangeCaptureCameraDevice,
}