修改事件触发方式

This commit is contained in:
terric 2023-11-10 16:12:37 +08:00
parent 7d8e33cfac
commit 99c4f11d54
6 changed files with 84 additions and 57 deletions

View File

@ -7,12 +7,17 @@ public class RobotController : MonoBehaviour
{
public void CheckAction(AvatarAction action)
{
EventManager.Instance.Dispatch("EstimateAction", action);
EventManager.Instance.Dispatch(YogaEventType.EstimateAction, action);
}
public void FinishCurrentActionCheck()
{
EventManager.Instance.Dispatch("ScoreUpdate");
EventManager.Instance.Dispatch(YogaEventType.ScoreUpdate);
}
public void GetActionBasePoint()
{
EventManager.Instance.Dispatch(YogaEventType.GetActionBasePoint);
}
}

View File

@ -8,9 +8,9 @@ public class EventManager : MonoSingleton<EventManager>
{
public delegate void EventHandler(params object[] args);
private Dictionary<string, Action> _actionDic = new Dictionary<string, Action>();
private Dictionary<string, EventHandler> _actionsDic = new Dictionary<string, EventHandler>();
public void AddEventListener(string eventName, Action handler)
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))
{
@ -23,7 +23,7 @@ public class EventManager : MonoSingleton<EventManager>
}
}
public void RemoveEventListener(string eventName, Action handler)
public void RemoveEventListener(YogaEventType eventName, Action handler)
{
if (!_actionDic.ContainsKey(eventName))
return;
@ -31,7 +31,7 @@ public class EventManager : MonoSingleton<EventManager>
_actionDic[eventName] -= handler;
}
public void Dispatch(string eventName)
public void Dispatch(YogaEventType eventName)
{
if (!_actionDic.ContainsKey(eventName))
return;
@ -40,7 +40,7 @@ public class EventManager : MonoSingleton<EventManager>
}
//´ø²ÎÊýµÄʼþ
public void AddEventListener(string eventName, EventHandler handler)
public void AddEventListener(YogaEventType eventName, EventHandler handler)
{
if (!_actionsDic.ContainsKey(eventName))
{
@ -53,7 +53,7 @@ public class EventManager : MonoSingleton<EventManager>
}
}
public void RemoveEventListener(string eventName, EventHandler handler)
public void RemoveEventListener(YogaEventType eventName, EventHandler handler)
{
if (!_actionsDic.ContainsKey(eventName))
return;
@ -61,7 +61,7 @@ public class EventManager : MonoSingleton<EventManager>
_actionsDic[eventName] -= handler;
}
public void Dispatch(string eventName, params object[] param)
public void Dispatch(YogaEventType eventName, params object[] param)
{
if (!_actionsDic.ContainsKey(eventName))
return;
@ -69,3 +69,10 @@ public class EventManager : MonoSingleton<EventManager>
_actionsDic[eventName]?.Invoke(param);
}
}
public enum YogaEventType
{
StartMotionCapture,
StopMotionCapture,
EstimateAction,
}

View File

@ -1,10 +1,11 @@
using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GudieAnimationManager : MonoBehaviour
{
private Animator _animator;
private string _currName;
private void Awake()
{
@ -14,6 +15,21 @@ public class GudieAnimationManager : MonoBehaviour
public void Play(string name)
{
_animator.CrossFade(name, 0.5f);
_currName = name;
}
private void Update()
{
if (string.IsNullOrEmpty(_currName))
return;
//当未达到指标且动画播放完毕时,重新播放
if (_animator.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1.0f)
{
if (YogaManager.Instance.CurrentActionCount < YogaManager.Instance.MaxActionCount)
{
_animator.Play(_currName, 0, 0.0f);
}
}
}
public void Stop()
@ -23,7 +39,7 @@ public class GudieAnimationManager : MonoBehaviour
public void FrameEstimate()
{
EventManager.Instance.Dispatch("PoseEstimate");
EventManager.Instance.Dispatch(YogaEventType.PoseEstimate);
}
}

View File

@ -10,10 +10,24 @@ public class HeadTurnLeft : PoseBase
public override bool CheckPose(List<Point> points)
{
if (!CheckPoint(points, "Nose"))
if (!CheckPoint(points, "Nose") || CheckPoint(points, "LEar") || CheckPoint(points, "Neck")) // 必须包含 Nose 和 LEar 和 Neck 的点位
return false;
//左转头检测
//当 LShoulder 和 RShoulder 都未检测到时
if (!CheckPoint(points, "LShoulder") || !CheckPoint(points, "RShoulder"))
{
}
//当 LShoulder 和 RShoulder 都检测到时,以 LShoulder 和 RShoulder 为基准
//
if (CheckPoint(points, "LEar") && !CheckPoint(points, "REar"))
{
return true;

View File

@ -56,18 +56,22 @@ namespace Yoga
private void OnEnable()
{
EventManager.Instance.AddEventListener("StartMotionCapture", OnStartMotionCapture);
EventManager.Instance.AddEventListener("StopMotionCapture", OnStopMotionCapture);
EventManager.Instance.AddEventListener("EstimateAction", EstimateAction);
EventManager.Instance.AddEventListener("ScoreUpdate", ScoreUpdate);
EventManager.Instance.AddEventListener(YogaEventType.StartMotionCapture, OnStartMotionCapture);
EventManager.Instance.AddEventListener(YogaEventType.StopMotionCapture, OnStopMotionCapture);
EventManager.Instance.AddEventListener(YogaEventType.EstimateAction, EstimateAction);
EventManager.Instance.AddEventListener(YogaEventType.ScoreUpdate, ScoreUpdate);
EventManager.Instance.AddEventListener(YogaEventType.GetActionBasePoint, GetActionBasePoint);
}
private void OnDisable()
{
EventManager.Instance.RemoveEventListener("StartMotionCapture", OnStartMotionCapture);
EventManager.Instance.RemoveEventListener("StopMotionCapture", OnStopMotionCapture);
EventManager.Instance.RemoveEventListener("EstimateAction", EstimateAction);
EventManager.Instance.RemoveEventListener("ScoreUpdate", ScoreUpdate);
EventManager.Instance.RemoveEventListener(YogaEventType.StartMotionCapture, OnStartMotionCapture);
EventManager.Instance.RemoveEventListener(YogaEventType.StopMotionCapture, OnStopMotionCapture);
EventManager.Instance.RemoveEventListener(YogaEventType.EstimateAction, EstimateAction);
EventManager.Instance.RemoveEventListener(YogaEventType.ScoreUpdate, ScoreUpdate);
EventManager.Instance.RemoveEventListener(YogaEventType.GetActionBasePoint, GetActionBasePoint);
}
private void OnStartMotionCapture()
@ -81,7 +85,10 @@ namespace Yoga
this.enabled = false;
_isOnCamCapture = false;
}
private void GetActionBasePoint()
{
throw new NotImplementedException();
}
public override void Init()
{
base.Init();
@ -378,11 +385,11 @@ namespace Yoga
if (_isCorrectAction)
{
EventManager.Instance.Dispatch("ActionSuccess");
EventManager.Instance.Dispatch(YogaEventType.ActionSuccess);
}
else
{
EventManager.Instance.Dispatch("ActionFailed");
EventManager.Instance.Dispatch(YogaEventType.ActionFailed);
}
_isCorrectAction = false;//重置

View File

@ -27,20 +27,20 @@ public class GuideUI : UIPanelBase
private void OnEnable()
{
EventManager.Instance.AddEventListener("PlayAnimation", PlayAnimation);
EventManager.Instance.AddEventListener("ActionSuccess", ShowSuccessEffect);
EventManager.Instance.AddEventListener("UpdateProgress", UpdateSuccessCount);
EventManager.Instance.AddEventListener("ActionFailed", ShowFailEffect);
EventManager.Instance.AddEventListener(YogaEventType.PlayAnimation, PlayAnimation);
EventManager.Instance.AddEventListener(YogaEventType.ActionSuccess, ShowSuccessEffect);
EventManager.Instance.AddEventListener(YogaEventType.UpdateProgress, UpdateSuccessCount);
EventManager.Instance.AddEventListener(YogaEventType.ActionFailed, ShowFailEffect);
}
private void OnDisable()
{
EventManager.Instance.RemoveEventListener("PlayAnimation", PlayAnimation);
EventManager.Instance.RemoveEventListener("ActionSuccess", ShowSuccessEffect);
EventManager.Instance.RemoveEventListener("UpdateProgress", UpdateSuccessCount);
EventManager.Instance.RemoveEventListener("ActionFailed", ShowFailEffect);
EventManager.Instance.RemoveEventListener(YogaEventType.PlayAnimation, PlayAnimation);
EventManager.Instance.RemoveEventListener(YogaEventType.ActionSuccess, ShowSuccessEffect);
EventManager.Instance.RemoveEventListener(YogaEventType.UpdateProgress, UpdateSuccessCount);
EventManager.Instance.RemoveEventListener(YogaEventType.ActionFailed, ShowFailEffect);
}
#region Event Func
@ -54,28 +54,6 @@ public class GuideUI : UIPanelBase
}
GuideMgr.Play(Enum.GetName(typeof(AvatarAction), animeType));
//switch ((AvatarAction)animeType)
//{
// case AvatarAction.HeadTurnLeft:
// GuideMgr.Play("HeadTurnLeft");
// break;
// case AvatarAction.HeadTurnRight:
// GuideMgr.Play("HeadTurnRight");
// break;
// case AvatarAction.HeadShake:
// GuideMgr.Play("HeadShake");
// break;
// case AvatarAction.Nod:
// GuideMgr.Play("Nod");
// break;
// case AvatarAction.HandsUp:
// GuideMgr.Play("HandsUp");
// break;
// default:
// GuideMgr.Play("Idle");
// break;
//}
}
private void UpdateSuccessCount(params object[] args)
@ -147,8 +125,8 @@ public class GuideUI : UIPanelBase
{
base.OnEnter();
_guide.SetActive(true);
EventManager.Instance.Dispatch("StartMotionCapture");
EventManager.Instance.Dispatch("PlayAnimation", YogaManager.Instance.Action.Action);
EventManager.Instance.Dispatch(YogaEventType.StartMotionCapture);
EventManager.Instance.Dispatch(YogaEventType.PlayAnimation, YogaManager.Instance.Action.Action);
}
public override void OnExit()
@ -156,6 +134,6 @@ public class GuideUI : UIPanelBase
base.OnExit();
_guide.SetActive(false);
GuideMgr.Stop();
EventManager.Instance.Dispatch("StopMotionCapture");
EventManager.Instance.Dispatch(YogaEventType.StopMotionCapture);
}
}