SpatialObject.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Events;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.Serialization;
  8. namespace EZXR.Glass.Inputs
  9. {
  10. public class SpatialObject : MonoBehaviour
  11. {
  12. public bool isStaticObject;
  13. public bool freezePosition_X, freezePosition_Y, freezePosition_Z;
  14. public bool freezeRotation;
  15. public bool freezeScale_X, freezeScale_Y, freezeScale_Z;
  16. private static Dictionary<Collider, SpatialObject> All = new Dictionary<Collider, SpatialObject>();
  17. [Serializable]
  18. public class SpatialObjectEvent : UnityEvent
  19. { }
  20. [Serializable]
  21. public class Entry
  22. {
  23. public SpatialObjectEventType eventID = SpatialObjectEventType.OnEnter;
  24. public SpatialObjectEvent callback = new SpatialObjectEvent();
  25. }
  26. [FormerlySerializedAs("delegates")]
  27. [SerializeField]
  28. private List<Entry> m_Delegates;
  29. public List<Entry> triggers
  30. {
  31. get
  32. {
  33. if (m_Delegates == null)
  34. m_Delegates = new List<Entry>();
  35. return m_Delegates;
  36. }
  37. set { m_Delegates = value; }
  38. }
  39. private void Execute(SpatialObjectEventType id)
  40. {
  41. var triggerCount = triggers.Count;
  42. for (int i = 0, imax = triggers.Count; i < imax; ++i)
  43. {
  44. var ent = triggers[i];
  45. if (ent.eventID == id && ent.callback != null)
  46. ent.callback.Invoke();
  47. }
  48. }
  49. ////TODO: 改为EventTrigger "Add Event"形式
  50. //[SerializeField]
  51. //private UnityEvent OnHandEnter;
  52. //[SerializeField]
  53. //private UnityEvent OnHandStay;
  54. //[SerializeField]
  55. //private UnityEvent OnHandExit;
  56. //[SerializeField]
  57. //private UnityEvent OnHandGrab;
  58. //[SerializeField]
  59. //private UnityEvent OnHandRelease;
  60. //[SerializeField]
  61. //private UnityEvent OnHandTriggerEnter;
  62. //[SerializeField]
  63. //private UnityEvent OnHandTriggerStay;
  64. //[SerializeField]
  65. //private UnityEvent OnHandTriggerExit;
  66. //[SerializeField]
  67. //private UnityEvent OnHandTriggerGrab;
  68. //[SerializeField]
  69. //private UnityEvent OnHandTriggerRelease;
  70. //[SerializeField]
  71. //private UnityEvent OnHandRayEnter;
  72. //[SerializeField]
  73. //private UnityEvent OnHandRayStay;
  74. //[SerializeField]
  75. //private UnityEvent OnHandRayExit;
  76. //[SerializeField]
  77. //private UnityEvent OnHandRayGrab;
  78. //[SerializeField]
  79. //private UnityEvent OnHandRayRelease;
  80. private void Awake()
  81. {
  82. gameObject.tag = "SpatialObject";
  83. All.Add(GetComponent<Collider>(), this);
  84. }
  85. // Start is called before the first frame update
  86. void Start()
  87. {
  88. }
  89. // Update is called once per frame
  90. void LateUpdate()
  91. {
  92. }
  93. Vector3 newPos;
  94. public void SetPosition(Vector3 pos)
  95. {
  96. if (!isStaticObject)
  97. {
  98. newPos.Set(freezePosition_X ? transform.position.x : pos.x, freezePosition_Y ? transform.position.y : pos.y, freezePosition_Z ? transform.position.z : pos.z);
  99. transform.position = newPos;
  100. }
  101. }
  102. Quaternion newQ;
  103. public void SetRotation(Quaternion rot)
  104. {
  105. if (!isStaticObject && !freezeRotation)
  106. {
  107. newQ = rot;
  108. transform.rotation = newQ;
  109. }
  110. }
  111. Vector3 newScale;
  112. public void SetScale(Vector3 scale)
  113. {
  114. if (!isStaticObject)
  115. {
  116. newScale.Set(freezeScale_X ? transform.localScale.x : scale.x, freezeScale_Y ? transform.localScale.y : scale.y, freezeScale_Z ? transform.localScale.z : scale.z);
  117. transform.localScale = newScale;
  118. }
  119. }
  120. public void SetTransform(Vector3 pos, Quaternion rot, Vector3 scale)
  121. {
  122. if (!isStaticObject)
  123. {
  124. transform.position = pos;
  125. transform.rotation = rot;
  126. transform.localScale = scale;
  127. }
  128. }
  129. #region HandTriggerEvent
  130. public static void PerformOnHandTriggerEnter(Collider other)
  131. {
  132. Debug.Log("SpatialObject--> PerformOnHandTriggerEnter: " + other.name);
  133. if (All.ContainsKey(other))
  134. {
  135. //All[other].OnHandTriggerEnter.Invoke();
  136. //All[other].OnHandEnter.Invoke();
  137. All[other].Execute(SpatialObjectEventType.OnHandEnter);
  138. All[other].Execute(SpatialObjectEventType.OnEnter);
  139. }
  140. }
  141. public static void PerformOnHandTriggerStay(Collider other)
  142. {
  143. //Debug.Log("SpatialObject--> PerformOnHandTriggerStay: " + other.name);
  144. if (All.ContainsKey(other))
  145. {
  146. //All[other].OnHandTriggerStay.Invoke();
  147. //All[other].OnHandStay.Invoke();
  148. All[other].Execute(SpatialObjectEventType.OnHandStay);
  149. All[other].Execute(SpatialObjectEventType.OnStay);
  150. }
  151. }
  152. public static void PerformOnHandTriggerExit(Collider other)
  153. {
  154. Debug.Log("SpatialObject--> PerformOnHandTriggerExit: " + other.name);
  155. if (All.ContainsKey(other))
  156. {
  157. //All[other].OnHandTriggerExit.Invoke();
  158. //All[other].OnHandExit.Invoke();
  159. All[other].Execute(SpatialObjectEventType.OnHandExit);
  160. All[other].Execute(SpatialObjectEventType.OnExit);
  161. }
  162. }
  163. public static void PerformOnHandTriggerGrab(Collider other)
  164. {
  165. Debug.Log("SpatialObject--> PerformOnHandTriggerGrab: " + other.name);
  166. if (All.ContainsKey(other))
  167. {
  168. //All[other].OnHandTriggerGrab.Invoke();
  169. //All[other].OnHandGrab.Invoke();
  170. All[other].Execute(SpatialObjectEventType.OnHandGrab);
  171. All[other].Execute(SpatialObjectEventType.OnGrab);
  172. }
  173. }
  174. public static void PerformOnHandTriggerRelease(Collider other)
  175. {
  176. Debug.Log("SpatialObject--> PerformOnHandTriggerRelease: " + other.name);
  177. if (All.ContainsKey(other))
  178. {
  179. //All[other].OnHandTriggerRelease.Invoke();
  180. //All[other].OnHandRelease.Invoke();
  181. All[other].Execute(SpatialObjectEventType.OnHandRelease);
  182. All[other].Execute(SpatialObjectEventType.OnRelease);
  183. }
  184. }
  185. #endregion
  186. #region HandRayEvent
  187. public static void PerformOnHandRayEnter(Collider other)
  188. {
  189. Debug.Log("SpatialObject--> PerformOnHandRayEnter: " + other.name);
  190. if (All.ContainsKey(other))
  191. {
  192. //All[other].OnHandRayEnter.Invoke();
  193. //All[other].OnHandEnter.Invoke();
  194. All[other].Execute(SpatialObjectEventType.OnRayEnter);
  195. All[other].Execute(SpatialObjectEventType.OnEnter);
  196. }
  197. }
  198. public static void PerformOnHandRayStay(Collider other)
  199. {
  200. //Debug.Log("SpatialObject--> PerformOnHandRayStay: " + other.name);
  201. if (All.ContainsKey(other))
  202. {
  203. //All[other].OnHandRayStay.Invoke();
  204. //All[other].OnHandStay.Invoke();
  205. All[other].Execute(SpatialObjectEventType.OnRayStay);
  206. All[other].Execute(SpatialObjectEventType.OnStay);
  207. }
  208. }
  209. public static void PerformOnHandRayExit(Collider other)
  210. {
  211. Debug.Log("SpatialObject--> PerformOnHandRayExit: " + other.name);
  212. if (All.ContainsKey(other))
  213. {
  214. //All[other].OnHandRayExit.Invoke();
  215. //All[other].OnHandExit.Invoke();
  216. All[other].Execute(SpatialObjectEventType.OnRayExit);
  217. All[other].Execute(SpatialObjectEventType.OnExit);
  218. }
  219. }
  220. public static void PerformOnHandRayGrab(Collider other)
  221. {
  222. Debug.Log("SpatialObject--> PerformOnHandRayGrab: " + other.name);
  223. if (All.ContainsKey(other))
  224. {
  225. //All[other].OnHandRayGrab.Invoke();
  226. //All[other].OnHandGrab.Invoke();
  227. All[other].Execute(SpatialObjectEventType.OnRayGrab);
  228. All[other].Execute(SpatialObjectEventType.OnGrab);
  229. }
  230. }
  231. public static void PerformOnHandRayRelease(Collider other)
  232. {
  233. Debug.Log("SpatialObject--> PerformOnHandRayRelease: " + other.name);
  234. if (All.ContainsKey(other))
  235. {
  236. //All[other].OnHandRayRelease.Invoke();
  237. //All[other].OnHandRelease.Invoke();
  238. All[other].Execute(SpatialObjectEventType.OnRayRelease);
  239. All[other].Execute(SpatialObjectEventType.OnRelease);
  240. }
  241. }
  242. #endregion
  243. }
  244. }