VRControllerLaserCast.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class VRControllerLaserCast : MonoBehaviour
  6. {
  7. public float raycastMaxLength
  8. {
  9. get
  10. {
  11. return this.m_RaycastMaxLength;
  12. }
  13. set
  14. {
  15. this.m_RaycastMaxLength = value;
  16. }
  17. }
  18. public float PosScreenX
  19. {
  20. get
  21. {
  22. return this.m_PosScreenX;
  23. }
  24. }
  25. public float PosScreenY
  26. {
  27. get
  28. {
  29. return this.m_PosScreenY;
  30. }
  31. }
  32. private void Start()
  33. {
  34. GameMain.VRDeviceType vrdeviceTypeID = GameMain.Instance.VRDeviceTypeID;
  35. if (vrdeviceTypeID == GameMain.VRDeviceType.NON || vrdeviceTypeID == GameMain.VRDeviceType.RIFT || vrdeviceTypeID == GameMain.VRDeviceType.FOVE)
  36. {
  37. base.gameObject.SetActive(false);
  38. return;
  39. }
  40. base.StartCoroutine(this.Coroutine_Debug_FindParent());
  41. }
  42. private IEnumerator Coroutine_Debug_FindParent()
  43. {
  44. for (int i = 0; i < 4; i++)
  45. {
  46. yield return null;
  47. }
  48. while (this.m_Controller == null)
  49. {
  50. this.m_Controller = ((!this.m_IsLeftHand) ? GameMain.Instance.OvrMgr.ovr_obj.right_controller.controller : GameMain.Instance.OvrMgr.ovr_obj.left_controller.controller);
  51. yield return null;
  52. }
  53. GameObject hand = this.m_Controller.gameObject;
  54. VRTestInputModule.Instance.AddController(this);
  55. yield break;
  56. }
  57. private void Update()
  58. {
  59. if (!this.m_Controller)
  60. {
  61. if (GameMain.Instance == null)
  62. {
  63. return;
  64. }
  65. this.m_Controller = ((!this.m_IsLeftHand) ? GameMain.Instance.OvrMgr.ovr_obj.right_controller.controller : GameMain.Instance.OvrMgr.ovr_obj.left_controller.controller);
  66. if (!this.m_Controller)
  67. {
  68. return;
  69. }
  70. VRTestInputModule.Instance.AddController(this);
  71. }
  72. AVRController controller = this.m_Controller;
  73. if (!controller.UIRayEnable)
  74. {
  75. if (this.m_RaycastLineRenderer)
  76. {
  77. this.m_RaycastLineRenderer.gameObject.SetActive(false);
  78. }
  79. if (this.m_RaycastPointEffect)
  80. {
  81. this.m_RaycastPointEffect.SetActive(false);
  82. }
  83. return;
  84. }
  85. this.UpdateOffSet(controller.UIRayTrans);
  86. }
  87. public void UpdateOffSet(Transform trans)
  88. {
  89. base.transform.position = trans.position;
  90. base.transform.rotation = trans.rotation;
  91. base.transform.localScale = trans.lossyScale;
  92. }
  93. private void OnDestroy()
  94. {
  95. if (VRTestInputModule.Instance == null)
  96. {
  97. return;
  98. }
  99. VRTestInputModule.Instance.RemoveController(this);
  100. }
  101. public virtual bool GetButtonDown()
  102. {
  103. return this.m_Controller.VRControllerButtons.GetPressDown(AVRControllerButtons.BTN.VIRTUAL_L_CLICK);
  104. }
  105. public virtual bool GetButtonUp()
  106. {
  107. return this.m_Controller.VRControllerButtons.GetPressUp(AVRControllerButtons.BTN.VIRTUAL_L_CLICK);
  108. }
  109. public virtual Vector2 GetScrollDelta()
  110. {
  111. return Vector2.zero;
  112. }
  113. public virtual bool EnableUIRay()
  114. {
  115. return this.m_Controller && this.m_Controller.UIRayEnable;
  116. }
  117. public virtual void SetRaycastHitPoint(Vector3 WorldPos, Vector3 WorldNormal, float rayLength, GameObject HitObject)
  118. {
  119. bool active = false;
  120. if (HitObject != null && HitObject.GetComponent<RectTransform>() != null)
  121. {
  122. active = true;
  123. }
  124. if (!this.m_Controller.UIRayEnable)
  125. {
  126. return;
  127. }
  128. rayLength += this.m_RayLength;
  129. if (HitObject == null)
  130. {
  131. rayLength = this.m_RaycastMaxLength;
  132. }
  133. if (this.m_RaycastLineRenderer)
  134. {
  135. if (HitObject)
  136. {
  137. this.m_RaycastLineRenderer.SetColors(this.m_LineColorHighlighted, Color.white);
  138. }
  139. else
  140. {
  141. this.m_RaycastLineRenderer.SetColors(this.m_LineColorNormal, Color.white);
  142. }
  143. this.m_RaycastLineRenderer.SetPosition(0, base.transform.position);
  144. this.m_RaycastLineRenderer.SetPosition(1, base.transform.position + base.transform.forward * rayLength);
  145. this.m_RaycastLineRenderer.gameObject.SetActive(true);
  146. }
  147. if (this.m_RaycastPointEffect)
  148. {
  149. this.m_RaycastPointEffect.transform.position = base.transform.position + base.transform.forward * rayLength;
  150. if (HitObject)
  151. {
  152. this.m_RaycastPointEffect.transform.rotation = HitObject.transform.rotation;
  153. }
  154. this.m_RaycastPointEffect.SetActive(active);
  155. }
  156. }
  157. public void OnEventRayHitEnter(GameObject Obj)
  158. {
  159. bool flag = Obj.GetComponent<RectTransform>() != null;
  160. bool flag2 = false;
  161. Selectable component = Obj.GetComponent<Selectable>();
  162. if (component)
  163. {
  164. flag2 = component.interactable;
  165. }
  166. if (flag && flag2)
  167. {
  168. GameMain.Instance.SoundMgr.PlaySystem(SoundMgr.SeType.Hover);
  169. }
  170. }
  171. public void OnEventRayHitExit(GameObject Obj)
  172. {
  173. }
  174. public void OnEventClick(GameObject Obj)
  175. {
  176. bool flag = Obj.GetComponent<RectTransform>();
  177. bool flag2 = false;
  178. Selectable component = Obj.GetComponent<Selectable>();
  179. if (component)
  180. {
  181. flag2 = component.interactable;
  182. }
  183. if (flag && flag2)
  184. {
  185. GameMain.Instance.SoundMgr.PlaySystem(SoundMgr.SeType.Click);
  186. }
  187. }
  188. [SerializeField]
  189. private AVRController m_Controller;
  190. [SerializeField]
  191. private bool m_IsLeftHand;
  192. [SerializeField]
  193. private float m_RaycastMaxLength = 3f;
  194. [SerializeField]
  195. private LineRenderer m_RaycastLineRenderer;
  196. [SerializeField]
  197. protected GameObject m_RaycastPointEffect;
  198. [SerializeField]
  199. private Color m_LineColorNormal = Color.red;
  200. [SerializeField]
  201. private Color m_LineColorHighlighted = Color.green;
  202. [SerializeField]
  203. private float m_RayLength = 0.01f;
  204. protected float m_PosScreenX = 0.5f;
  205. protected float m_PosScreenY = 0.5f;
  206. }