MouseEventInput.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using UnityEngine;
  2. using System;
  3. using Rokid.UXR;
  4. using UnityEngine.EventSystems;
  5. using Rokid.UXR.Module;
  6. namespace Rokid.UXR.Interaction
  7. {
  8. public class MouseEventInput : MonoSingleton<MouseEventInput>, IEventInput
  9. {
  10. public static Action<Vector2> OnMouseMove;
  11. public static Action OnActiveMouseModule;
  12. public static Action OnReleaseMouseModule;
  13. private Vector2 mouseDelta = Vector2.zero;
  14. private Vector2 scrollDelta = Vector2.zero;
  15. private bool scrollChange;
  16. private bool mouseChange;
  17. private bool scrollInverse = false;
  18. private float elapsedTime = 0;
  19. public Transform Interactor { get; set; }
  20. private bool initialize = false;
  21. public void Initialize(Transform parent)
  22. {
  23. if (Interactor == null)
  24. {
  25. GameObject go = GameObject.Find("MouseRayInteractor");
  26. if (go == null)
  27. {
  28. go = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Interactor/MouseRayInteractor"));
  29. }
  30. Interactor = go.transform;
  31. Interactor.name = "MouseRayInteractor";
  32. Interactor.SetParent(transform);
  33. }
  34. Interactor.SetParent(transform);
  35. this.transform.SetParent(parent);
  36. initialize = true;
  37. }
  38. public void Release()
  39. {
  40. OnReleaseMouseModule?.Invoke();
  41. Destroy(this.gameObject);
  42. }
  43. private void Start()
  44. {
  45. RKLog.Info("====MouseEventInput==== 注册android回调用");
  46. CallBridge.CallAndroid(Request.Build()
  47. .Name("VirtualController.setOnHoverListener").
  48. AndroidCallback(CallBridge.CreateCallback(this.gameObject.name, "OnHoverEvent")));
  49. CallBridge.CallAndroid(Request.Build()
  50. .Name("VirtualController.setOnTouchListener").
  51. AndroidCallback(CallBridge.CreateCallback(this.gameObject.name, "OnTouchEvent")));
  52. CallBridge.CallAndroid(Request.Build()
  53. .Name("VirtualController.setOnScrollListener").
  54. AndroidCallback(CallBridge.CreateCallback(this.gameObject.name, "OnScrollEvent")));
  55. }
  56. /// <summary>
  57. /// 鼠标滚轮输入事件
  58. /// Mouse Scroll Input Event
  59. /// </summary>
  60. /// <param name="delta"></param>
  61. private void OnScrollEvent(string delta)
  62. {
  63. float x = Convert.ToSingle(delta.Split(',')[1]);
  64. float y = Convert.ToSingle(delta.Split(',')[0]);
  65. scrollDelta[0] = scrollInverse ? -x : x;
  66. scrollDelta[1] = scrollInverse ? -y : y;
  67. scrollChange = true;
  68. elapsedTime = 0;
  69. }
  70. /// <summary>
  71. /// 获取鼠标滚轮事件
  72. /// Get Mouse Scroll Delta
  73. /// </summary>
  74. public Vector2 GetMouseScrollDelta()
  75. {
  76. return scrollDelta;
  77. }
  78. /// <summary>
  79. /// Whether the mouse is inverted
  80. /// </summary>
  81. /// <param name="inverse"></param>
  82. public void SetScorllInverse(bool inverse)
  83. {
  84. scrollInverse = inverse;
  85. }
  86. /// <summary>
  87. /// Get the mouse down event
  88. /// </summary>
  89. /// <param name="button">0: left mouse button 1: right mouse button 2: middle mouse button</param>
  90. /// <returns></returns>
  91. public bool GetMouseButtonDown(int button)
  92. {
  93. if (button == 0)
  94. {
  95. return Input.GetMouseButtonDown(0);
  96. }
  97. if (button == 1)
  98. {
  99. #if UNITY_EDITOR
  100. return Input.GetMouseButtonDown(1);
  101. #else
  102. return RKNativeInput.Instance.GetKeyDown(RKKeyEvent.KEY_MOUSE_SECONDARY);
  103. #endif
  104. }
  105. if (button == 2)
  106. {
  107. #if UNITY_EDITOR
  108. return Input.GetMouseButtonDown(2);
  109. #else
  110. return RKNativeInput.Instance.GetKeyDown(RKKeyEvent.KEY_MOUSE_THERIARY);
  111. #endif
  112. }
  113. return false;
  114. }
  115. /// <summary>
  116. /// Get the Up event of the mouse
  117. /// </summary>
  118. /// <param name="button">0: left mouse button 1: right mouse button 2: middle mouse button</param>
  119. /// <returns></returns>
  120. public bool GetMouseButtonUp(int button)
  121. {
  122. if (button == 0)
  123. {
  124. return Input.GetMouseButtonUp(0);
  125. }
  126. if (button == 1)
  127. {
  128. #if UNITY_EDITOR
  129. return Input.GetMouseButtonUp(1);
  130. #else
  131. return RKNativeInput.Instance.GetKeyUp(RKKeyEvent.KEY_MOUSE_SECONDARY);
  132. #endif
  133. }
  134. if (button == 2)
  135. {
  136. #if UNITY_EDITOR
  137. return Input.GetMouseButtonUp(2);
  138. #else
  139. return RKNativeInput.Instance.GetKeyUp(RKKeyEvent.KEY_MOUSE_THERIARY);
  140. #endif
  141. }
  142. return false;
  143. }
  144. /// <summary>
  145. /// Android mouse input event
  146. /// </summary>
  147. private void OnHoverEvent(string delta)
  148. {
  149. float x = Convert.ToSingle(delta.Split(',')[0]);
  150. float y = Convert.ToSingle(delta.Split(',')[1]);
  151. mouseDelta[0] = x;
  152. mouseDelta[1] = y;
  153. if (InputModuleManager.Instance.GetMouseActive())
  154. {
  155. OnMouseMove?.Invoke(mouseDelta);
  156. }
  157. mouseChange = true;
  158. }
  159. /// <summary>
  160. /// Android mouse press input event
  161. /// </summary>
  162. private void OnTouchEvent(string screenPos)
  163. {
  164. float x = Convert.ToSingle(screenPos.Split(',')[0]);
  165. float y = Convert.ToSingle(screenPos.Split(',')[1]);
  166. mouseDelta[0] = x;
  167. mouseDelta[1] = y;
  168. if (InputModuleManager.Instance.GetMouseActive())
  169. {
  170. OnMouseMove?.Invoke(mouseDelta);
  171. }
  172. mouseChange = true;
  173. }
  174. private void Update()
  175. {
  176. if (InputModuleManager.Instance.GetMouseActive() == false && initialize == true)
  177. {
  178. bool active = false;
  179. #if UNITY_EDITOR
  180. active = Input.GetMouseButtonDown(2) || Input.GetKeyDown(KeyCode.M);
  181. #else
  182. active = mouseDelta.magnitude > 10.0f;
  183. #endif
  184. if (active)
  185. {
  186. ActiveModule();
  187. }
  188. }
  189. //判断滚轮值是否一直在变化
  190. if (scrollChange)
  191. {
  192. elapsedTime += Time.deltaTime;
  193. if (elapsedTime > 0.01f)
  194. {
  195. scrollChange = false;
  196. scrollDelta[0] = 0;
  197. scrollDelta[1] = 0;
  198. }
  199. }
  200. //判断鼠标是否一直在滑动
  201. if (mouseChange)
  202. {
  203. elapsedTime += Time.deltaTime;
  204. if (elapsedTime > 0.01f)
  205. {
  206. mouseChange = false;
  207. mouseDelta[0] = 0;
  208. mouseDelta[1] = 0;
  209. }
  210. }
  211. }
  212. public void ActiveModule()
  213. {
  214. RKLog.KeyInfo("====MouseEventInput==== : ActiveModule");
  215. OnActiveMouseModule?.Invoke();
  216. Input.ResetInputAxes();
  217. RKVirtualController.Instance.Change(ControllerType.Mouse);
  218. EventSystem.current.pixelDragThreshold = 10;
  219. }
  220. protected override void OnDestroy()
  221. {
  222. base.OnDestroy();
  223. OnMouseMove = null;
  224. initialize = false;
  225. }
  226. }
  227. }