123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Events;
- using UnityEngine.EventSystems;
- using UnityEngine.Serialization;
- namespace EZXR.Glass.Inputs
- {
- public class SpatialObject : MonoBehaviour
- {
- public bool isStaticObject;
- public bool freezePosition_X, freezePosition_Y, freezePosition_Z;
- public bool freezeRotation;
- public bool freezeScale_X, freezeScale_Y, freezeScale_Z;
- private static Dictionary<Collider, SpatialObject> All = new Dictionary<Collider, SpatialObject>();
- [Serializable]
- public class SpatialObjectEvent : UnityEvent
- { }
- [Serializable]
- public class Entry
- {
- public SpatialObjectEventType eventID = SpatialObjectEventType.OnEnter;
- public SpatialObjectEvent callback = new SpatialObjectEvent();
- }
- [FormerlySerializedAs("delegates")]
- [SerializeField]
- private List<Entry> m_Delegates;
- public List<Entry> triggers
- {
- get
- {
- if (m_Delegates == null)
- m_Delegates = new List<Entry>();
- return m_Delegates;
- }
- set { m_Delegates = value; }
- }
- private void Execute(SpatialObjectEventType id)
- {
- var triggerCount = triggers.Count;
- for (int i = 0, imax = triggers.Count; i < imax; ++i)
- {
- var ent = triggers[i];
- if (ent.eventID == id && ent.callback != null)
- ent.callback.Invoke();
- }
- }
- ////TODO: 改为EventTrigger "Add Event"形式
- //[SerializeField]
- //private UnityEvent OnHandEnter;
- //[SerializeField]
- //private UnityEvent OnHandStay;
- //[SerializeField]
- //private UnityEvent OnHandExit;
- //[SerializeField]
- //private UnityEvent OnHandGrab;
- //[SerializeField]
- //private UnityEvent OnHandRelease;
- //[SerializeField]
- //private UnityEvent OnHandTriggerEnter;
- //[SerializeField]
- //private UnityEvent OnHandTriggerStay;
- //[SerializeField]
- //private UnityEvent OnHandTriggerExit;
- //[SerializeField]
- //private UnityEvent OnHandTriggerGrab;
- //[SerializeField]
- //private UnityEvent OnHandTriggerRelease;
- //[SerializeField]
- //private UnityEvent OnHandRayEnter;
- //[SerializeField]
- //private UnityEvent OnHandRayStay;
- //[SerializeField]
- //private UnityEvent OnHandRayExit;
- //[SerializeField]
- //private UnityEvent OnHandRayGrab;
- //[SerializeField]
- //private UnityEvent OnHandRayRelease;
- private void Awake()
- {
- gameObject.tag = "SpatialObject";
- All.Add(GetComponent<Collider>(), this);
- }
- // Start is called before the first frame update
- void Start()
- {
- }
- // Update is called once per frame
- void LateUpdate()
- {
- }
- Vector3 newPos;
- public void SetPosition(Vector3 pos)
- {
- if (!isStaticObject)
- {
- newPos.Set(freezePosition_X ? transform.position.x : pos.x, freezePosition_Y ? transform.position.y : pos.y, freezePosition_Z ? transform.position.z : pos.z);
- transform.position = newPos;
- }
- }
- Quaternion newQ;
- public void SetRotation(Quaternion rot)
- {
- if (!isStaticObject && !freezeRotation)
- {
- newQ = rot;
- transform.rotation = newQ;
- }
- }
- Vector3 newScale;
- public void SetScale(Vector3 scale)
- {
- if (!isStaticObject)
- {
- newScale.Set(freezeScale_X ? transform.localScale.x : scale.x, freezeScale_Y ? transform.localScale.y : scale.y, freezeScale_Z ? transform.localScale.z : scale.z);
- transform.localScale = newScale;
- }
- }
- public void SetTransform(Vector3 pos, Quaternion rot, Vector3 scale)
- {
- if (!isStaticObject)
- {
- transform.position = pos;
- transform.rotation = rot;
- transform.localScale = scale;
- }
- }
- #region HandTriggerEvent
- public static void PerformOnHandTriggerEnter(Collider other)
- {
- Debug.Log("SpatialObject--> PerformOnHandTriggerEnter: " + other.name);
- if (All.ContainsKey(other))
- {
- //All[other].OnHandTriggerEnter.Invoke();
- //All[other].OnHandEnter.Invoke();
- All[other].Execute(SpatialObjectEventType.OnHandEnter);
- All[other].Execute(SpatialObjectEventType.OnEnter);
- }
- }
- public static void PerformOnHandTriggerStay(Collider other)
- {
- //Debug.Log("SpatialObject--> PerformOnHandTriggerStay: " + other.name);
- if (All.ContainsKey(other))
- {
- //All[other].OnHandTriggerStay.Invoke();
- //All[other].OnHandStay.Invoke();
- All[other].Execute(SpatialObjectEventType.OnHandStay);
- All[other].Execute(SpatialObjectEventType.OnStay);
- }
- }
- public static void PerformOnHandTriggerExit(Collider other)
- {
- Debug.Log("SpatialObject--> PerformOnHandTriggerExit: " + other.name);
- if (All.ContainsKey(other))
- {
- //All[other].OnHandTriggerExit.Invoke();
- //All[other].OnHandExit.Invoke();
- All[other].Execute(SpatialObjectEventType.OnHandExit);
- All[other].Execute(SpatialObjectEventType.OnExit);
- }
- }
- public static void PerformOnHandTriggerGrab(Collider other)
- {
- Debug.Log("SpatialObject--> PerformOnHandTriggerGrab: " + other.name);
- if (All.ContainsKey(other))
- {
- //All[other].OnHandTriggerGrab.Invoke();
- //All[other].OnHandGrab.Invoke();
- All[other].Execute(SpatialObjectEventType.OnHandGrab);
- All[other].Execute(SpatialObjectEventType.OnGrab);
- }
- }
- public static void PerformOnHandTriggerRelease(Collider other)
- {
- Debug.Log("SpatialObject--> PerformOnHandTriggerRelease: " + other.name);
- if (All.ContainsKey(other))
- {
- //All[other].OnHandTriggerRelease.Invoke();
- //All[other].OnHandRelease.Invoke();
- All[other].Execute(SpatialObjectEventType.OnHandRelease);
- All[other].Execute(SpatialObjectEventType.OnRelease);
- }
- }
- #endregion
- #region HandRayEvent
- public static void PerformOnHandRayEnter(Collider other)
- {
- Debug.Log("SpatialObject--> PerformOnHandRayEnter: " + other.name);
- if (All.ContainsKey(other))
- {
- //All[other].OnHandRayEnter.Invoke();
- //All[other].OnHandEnter.Invoke();
- All[other].Execute(SpatialObjectEventType.OnRayEnter);
- All[other].Execute(SpatialObjectEventType.OnEnter);
- }
- }
- public static void PerformOnHandRayStay(Collider other)
- {
- //Debug.Log("SpatialObject--> PerformOnHandRayStay: " + other.name);
- if (All.ContainsKey(other))
- {
- //All[other].OnHandRayStay.Invoke();
- //All[other].OnHandStay.Invoke();
- All[other].Execute(SpatialObjectEventType.OnRayStay);
- All[other].Execute(SpatialObjectEventType.OnStay);
- }
- }
- public static void PerformOnHandRayExit(Collider other)
- {
- Debug.Log("SpatialObject--> PerformOnHandRayExit: " + other.name);
- if (All.ContainsKey(other))
- {
- //All[other].OnHandRayExit.Invoke();
- //All[other].OnHandExit.Invoke();
- All[other].Execute(SpatialObjectEventType.OnRayExit);
- All[other].Execute(SpatialObjectEventType.OnExit);
- }
- }
- public static void PerformOnHandRayGrab(Collider other)
- {
- Debug.Log("SpatialObject--> PerformOnHandRayGrab: " + other.name);
- if (All.ContainsKey(other))
- {
- //All[other].OnHandRayGrab.Invoke();
- //All[other].OnHandGrab.Invoke();
- All[other].Execute(SpatialObjectEventType.OnRayGrab);
- All[other].Execute(SpatialObjectEventType.OnGrab);
- }
- }
- public static void PerformOnHandRayRelease(Collider other)
- {
- Debug.Log("SpatialObject--> PerformOnHandRayRelease: " + other.name);
- if (All.ContainsKey(other))
- {
- //All[other].OnHandRayRelease.Invoke();
- //All[other].OnHandRelease.Invoke();
- All[other].Execute(SpatialObjectEventType.OnRayRelease);
- All[other].Execute(SpatialObjectEventType.OnRelease);
- }
- }
- #endregion
- }
- }
|