VRTestInputModule.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. public class VRTestInputModule : BaseInputModule
  6. {
  7. public static VRTestInputModule Instance
  8. {
  9. get
  10. {
  11. if (VRTestInputModule.m_Instance == null)
  12. {
  13. }
  14. return VRTestInputModule.m_Instance;
  15. }
  16. }
  17. public void AddController(VRControllerLaserCast controller)
  18. {
  19. if (this.m_ControllerData.ContainsKey(controller))
  20. {
  21. return;
  22. }
  23. this.m_ControllerData.Add(controller, new VRTestInputModule.ControllerData());
  24. Debug.Log("コントローラを追加しました。");
  25. }
  26. public void RemoveController(VRControllerLaserCast controller)
  27. {
  28. this.m_ControllerData.Remove(controller);
  29. }
  30. private void ChangeCanvasWorldCamera(Camera worldCamera)
  31. {
  32. Canvas[] array = Resources.FindObjectsOfTypeAll<Canvas>();
  33. for (int i = 0; i < array.Length; i++)
  34. {
  35. array[i].worldCamera = worldCamera;
  36. }
  37. }
  38. protected override void OnDisable()
  39. {
  40. base.OnDisable();
  41. this.ChangeCanvasWorldCamera(null);
  42. }
  43. protected override void OnEnable()
  44. {
  45. base.OnEnable();
  46. this.ChangeCanvasWorldCamera(this.m_UIRaycastCamera);
  47. }
  48. protected override void Awake()
  49. {
  50. base.Awake();
  51. if (VRTestInputModule.m_Instance != null)
  52. {
  53. Debug.LogWarning(string.Concat(new object[]
  54. {
  55. "シングルトン「",
  56. typeof(VRTestInputModule),
  57. "」が既に作成されていました。\nゲームオブジェクト「",
  58. base.gameObject.name,
  59. "」を削除します。"
  60. }));
  61. UnityEngine.Object.DestroyImmediate(base.gameObject);
  62. return;
  63. }
  64. VRTestInputModule.m_Instance = this;
  65. Debug.Log("シングルトン「" + typeof(VRTestInputModule) + "」を作成しました。");
  66. this.m_UIRaycastCamera = new GameObject("VR Controller Raycast Camera").AddComponent<Camera>();
  67. this.m_UIRaycastCamera.clearFlags = CameraClearFlags.Nothing;
  68. this.m_UIRaycastCamera.orthographic = true;
  69. this.m_UIRaycastCamera.fieldOfView = 60f;
  70. this.m_UIRaycastCamera.nearClipPlane = 0f;
  71. this.m_UIRaycastCamera.farClipPlane = 100f;
  72. this.m_UIRaycastCamera.depth = -100f;
  73. this.m_UIRaycastCamera.orthographicSize = 1E-09f;
  74. this.m_UIRaycastCamera.transform.SetParent(base.gameObject.transform);
  75. this.m_UIRaycastCamera.stereoTargetEye = StereoTargetEyeMask.None;
  76. this.m_UIRaycastCamera.gameObject.AddComponent<PhysicsRaycaster>();
  77. this.ChangeCanvasWorldCamera(this.m_UIRaycastCamera);
  78. this.m_UIRaycastCamera.enabled = false;
  79. }
  80. public override void Process()
  81. {
  82. foreach (KeyValuePair<VRControllerLaserCast, VRTestInputModule.ControllerData> keyValuePair in this.m_ControllerData)
  83. {
  84. VRControllerLaserCast key = keyValuePair.Key;
  85. VRTestInputModule.ControllerData value = keyValuePair.Value;
  86. if (key.gameObject.activeInHierarchy)
  87. {
  88. if (key.EnableUIRay())
  89. {
  90. if (this.m_UIRaycastCamera)
  91. {
  92. this.m_UIRaycastCamera.transform.position = key.transform.position;
  93. this.m_UIRaycastCamera.transform.rotation = key.transform.rotation;
  94. if (value.pointerData == null)
  95. {
  96. value.pointerData = new PointerEventData(base.eventSystem);
  97. }
  98. else
  99. {
  100. value.pointerData.Reset();
  101. }
  102. PointerEventData pointerData = value.pointerData;
  103. pointerData.position = new Vector2(key.PosScreenX * (float)this.m_UIRaycastCamera.pixelWidth, key.PosScreenY * (float)this.m_UIRaycastCamera.pixelHeight);
  104. pointerData.delta = Vector2.zero;
  105. pointerData.scrollDelta = key.GetScrollDelta();
  106. base.eventSystem.RaycastAll(pointerData, this.m_RaycastResultCache);
  107. RaycastResult pointerCurrentRaycast = BaseInputModule.FindFirstRaycast(this.m_RaycastResultCache);
  108. GameObject gameObject = pointerCurrentRaycast.gameObject;
  109. pointerData.pointerCurrentRaycast = pointerCurrentRaycast;
  110. this.m_RaycastResultCache.Clear();
  111. if (pointerCurrentRaycast.distance > key.raycastMaxLength)
  112. {
  113. gameObject = null;
  114. }
  115. key.SetRaycastHitPoint(pointerCurrentRaycast.worldPosition, pointerCurrentRaycast.worldNormal, pointerCurrentRaycast.distance, gameObject);
  116. if (value.pointObject != gameObject)
  117. {
  118. if (value.pointObject)
  119. {
  120. ExecuteEvents.ExecuteHierarchy<IPointerExitHandler>(value.pointObject, pointerData, ExecuteEvents.pointerExitHandler);
  121. key.OnEventRayHitExit(value.pointObject);
  122. }
  123. if (gameObject)
  124. {
  125. ExecuteEvents.ExecuteHierarchy<IPointerEnterHandler>(gameObject, pointerData, ExecuteEvents.pointerEnterHandler);
  126. key.OnEventRayHitEnter(gameObject);
  127. }
  128. }
  129. value.pointObject = gameObject;
  130. if (pointerData.scrollDelta.sqrMagnitude > 0f)
  131. {
  132. ExecuteEvents.ExecuteHierarchy<IScrollHandler>(value.pointObject, pointerData, ExecuteEvents.scrollHandler);
  133. }
  134. if (key.GetButtonDown())
  135. {
  136. if (base.eventSystem.currentSelectedGameObject)
  137. {
  138. base.eventSystem.SetSelectedGameObject(null);
  139. }
  140. pointerData.pressPosition = pointerData.position;
  141. pointerData.pointerPressRaycast = pointerData.pointerCurrentRaycast;
  142. pointerData.pointerPress = null;
  143. if (value.pointObject)
  144. {
  145. value.pressedObject = value.pointObject;
  146. GameObject gameObject2 = ExecuteEvents.ExecuteHierarchy<IPointerDownHandler>(value.pressedObject, pointerData, ExecuteEvents.pointerDownHandler);
  147. if (!gameObject2)
  148. {
  149. gameObject2 = ExecuteEvents.ExecuteHierarchy<IPointerClickHandler>(value.pressedObject, pointerData, ExecuteEvents.pointerClickHandler);
  150. if (gameObject2 != null)
  151. {
  152. value.pressedObject = gameObject2;
  153. }
  154. }
  155. else
  156. {
  157. pointerData.pointerPress = gameObject2;
  158. value.pressedObject = gameObject2;
  159. base.eventSystem.SetSelectedGameObject(gameObject2);
  160. }
  161. ExecuteEvents.ExecuteHierarchy<IBeginDragHandler>(value.pressedObject, pointerData, ExecuteEvents.beginDragHandler);
  162. GameObject gameObject3 = ExecuteEvents.ExecuteHierarchy<IDragHandler>(value.pressedObject, pointerData, ExecuteEvents.dragHandler);
  163. pointerData.pointerDrag = gameObject3;
  164. value.draggingObject = gameObject3;
  165. }
  166. }
  167. if (key.GetButtonUp())
  168. {
  169. if (value.draggingObject)
  170. {
  171. ExecuteEvents.ExecuteHierarchy<IEndDragHandler>(value.draggingObject, pointerData, ExecuteEvents.endDragHandler);
  172. pointerData.pointerDrag = null;
  173. value.draggingObject = null;
  174. }
  175. if (value.pressedObject)
  176. {
  177. GameObject eventHandler = ExecuteEvents.GetEventHandler<IPointerClickHandler>(value.pointObject);
  178. if (value.pressedObject == eventHandler)
  179. {
  180. key.OnEventClick(value.pressedObject);
  181. if (!ExecuteEvents.Execute<ISubmitHandler>(value.pressedObject, pointerData, ExecuteEvents.submitHandler))
  182. {
  183. ExecuteEvents.Execute<IPointerClickHandler>(value.pressedObject, pointerData, ExecuteEvents.pointerClickHandler);
  184. }
  185. }
  186. ExecuteEvents.Execute<IPointerUpHandler>(value.pressedObject, pointerData, ExecuteEvents.pointerUpHandler);
  187. pointerData.pointerPress = null;
  188. value.pressedObject = null;
  189. }
  190. if (base.eventSystem.currentSelectedGameObject)
  191. {
  192. base.eventSystem.SetSelectedGameObject(null);
  193. }
  194. }
  195. if (value.draggingObject)
  196. {
  197. ExecuteEvents.ExecuteHierarchy<IDragHandler>(value.draggingObject, pointerData, ExecuteEvents.dragHandler);
  198. }
  199. }
  200. }
  201. }
  202. }
  203. }
  204. private static VRTestInputModule m_Instance;
  205. private Camera m_UIRaycastCamera;
  206. private Dictionary<VRControllerLaserCast, VRTestInputModule.ControllerData> m_ControllerData = new Dictionary<VRControllerLaserCast, VRTestInputModule.ControllerData>();
  207. private class ControllerData
  208. {
  209. public PointerEventData pointerData;
  210. public GameObject pointObject;
  211. public GameObject pressedObject;
  212. public GameObject draggingObject;
  213. }
  214. }