using UnityEngine; using System.Collections.Generic; namespace Rokid.UXR.Interaction { /// /// 头手交互驱动器 /// public class HeadHandDriver : MonoBehaviour { [SerializeField] private List drivedObjs = new List(); private HandType holdHandType = HandType.None; private bool isHovering = false; private void Start() { GesEventInput.OnTrackedFailed += OnTrackedFailed; FindComponets(transform); } private void OnDestroy() { GesEventInput.OnTrackedFailed -= OnTrackedFailed; } private void FindComponets(Transform tsf) { IHeadHandDriver[] handDrivers = tsf.GetComponents(); if (handDrivers != null && handDrivers.Length > 0) drivedObjs.AddRange(handDrivers); if (tsf.childCount > 0) { foreach (Transform child in tsf) { FindComponets(child); } } } private void OnTrackedFailed(HandType handType) { if (handType == holdHandType || handType == HandType.None) { holdHandType = HandType.None; ChangeHoldHandType(); } } private void Update() { if (holdHandType == HandType.None) { if (GesEventInput.Instance.GetHandDown(HandType.LeftHand)) { holdHandType = HandType.LeftHand; ChangeHoldHandType(); } else if (GesEventInput.Instance.GetHandDown(HandType.RightHand)) { holdHandType = HandType.RightHand; ChangeHoldHandType(); } if (!GesEventInput.Instance.GetHandPress(holdHandType)) { OnHandRelease(); } } else if (GesEventInput.Instance.GetHandPress(holdHandType)) { OnHandPress(); } if (GesEventInput.Instance.GetHandUp(holdHandType)) { RKLog.Info($"====HeadHandDriver==== GetHandUp: {holdHandType}"); holdHandType = HandType.None; ChangeHoldHandType(); } } private void ChangeHoldHandType() { for (int i = 0; i < drivedObjs.Count; i++) { drivedObjs[i].OnBeforeChangeHoldHandType(holdHandType); } for (int i = 0; i < drivedObjs.Count; i++) { drivedObjs[i].OnChangeHoldHandType(holdHandType); } RKLog.Info($"====HeadHandDriver==== ChangeHoldHandType: {holdHandType}"); } private void OnHandPress() { for (int i = 0; i < drivedObjs.Count; i++) { drivedObjs[i].OnHandPress(holdHandType); } } private void OnHandRelease() { for (int i = 0; i < drivedObjs.Count; i++) { drivedObjs[i].OnHandRelease(); } } } }