SimulateMouse.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. namespace JC.Unity {
  7. /**模拟鼠标-场景中需要存在EventSystem */
  8. public class SimulateMouse : MonoBehaviour
  9. {
  10. [SerializeField] CanvasScaler canvasScaler;
  11. [SerializeField] RectTransform mousePointer;
  12. [NonSerialized] public Action<Selectable> OnPointerEnter;
  13. [NonSerialized] public Action<Selectable> OnPointerClick;
  14. ScreenRayRaycasterUIWrapper mScreenRayRaycasterUIWrapper = new ScreenRayRaycasterUIWrapper();
  15. void Awake()
  16. {
  17. mousePointer.anchorMin = Vector2.zero;
  18. mousePointer.anchorMax = Vector2.zero;
  19. if (mScreenRayRaycasterUIWrapper != null) {
  20. // 把鼠标的位置传 Ray 发射位置
  21. mScreenRayRaycasterUIWrapper.Init(this, () => {
  22. Vector2 pos = mousePointer.anchoredPosition;
  23. pos.x *= (float) Screen.width / GetScaleScreenWidth();
  24. pos.y *= (float) Screen.height / GetScaleScreenHeight();
  25. return pos;
  26. });
  27. }
  28. }
  29. void OnDisable() {
  30. mScreenRayRaycasterUIWrapper.PressUp();
  31. }
  32. void OnEnable() {
  33. MakeMouseToScreenCenter();
  34. }
  35. void Update() {
  36. mScreenRayRaycasterUIWrapper.Update();
  37. }
  38. Vector2 ClampValue(Vector2 val)
  39. {
  40. val.x = val.x < 0 ? 0 : val.x;
  41. val.y = val.y < 0 ? 0 : val.y;
  42. float maxWidth = GetScaleScreenWidth();
  43. float maxHeight = GetScaleScreenHeight();
  44. val.x = val.x > maxWidth ? maxWidth : val.x;
  45. val.y = val.y > maxHeight ? maxHeight : val.y;
  46. return val;
  47. }
  48. #region 对外提供的接口
  49. public float GetScaleScreenWidth() {
  50. float width = Screen.width;
  51. if (canvasScaler.uiScaleMode == CanvasScaler.ScaleMode.ScaleWithScreenSize) {
  52. if (canvasScaler.screenMatchMode == CanvasScaler.ScreenMatchMode.MatchWidthOrHeight) {
  53. float height = Screen.height;
  54. if (canvasScaler.matchWidthOrHeight == 1) {
  55. width *= canvasScaler.referenceResolution.y / height;
  56. } else if (canvasScaler.matchWidthOrHeight == 0) {
  57. width = canvasScaler.referenceResolution.x;
  58. }
  59. }
  60. }
  61. return width;
  62. }
  63. public float GetScaleScreenHeight() {
  64. float height = Screen.height;
  65. if (canvasScaler.uiScaleMode == CanvasScaler.ScaleMode.ScaleWithScreenSize) {
  66. if (canvasScaler.screenMatchMode == CanvasScaler.ScreenMatchMode.MatchWidthOrHeight) {
  67. float width = Screen.width;
  68. if (canvasScaler.matchWidthOrHeight == 0) {
  69. height *= canvasScaler.referenceResolution.x / width;
  70. } else if (canvasScaler.matchWidthOrHeight == 1) {
  71. height = canvasScaler.referenceResolution.y;
  72. }
  73. }
  74. }
  75. return height;
  76. }
  77. public void MoveMousePointer(Vector2 deltaPos)
  78. {
  79. if (mousePointer == null) return;
  80. Vector2 val = mousePointer.anchoredPosition;
  81. val += deltaPos;
  82. mousePointer.anchoredPosition = ClampValue(val);
  83. }
  84. public void SetMousePointerPosition(Vector2 pos)
  85. {
  86. if (mousePointer == null) return;
  87. mousePointer.position = pos;
  88. }
  89. public void ClickMousePointer()
  90. {
  91. mScreenRayRaycasterUIWrapper.Click();
  92. }
  93. public void MakeMouseToScreenCenter() {
  94. mousePointer.anchoredPosition = new Vector2(GetScaleScreenWidth()/2,GetScaleScreenHeight()/2);
  95. }
  96. public Selectable GetCurrentSelectable() {
  97. return mScreenRayRaycasterUIWrapper.m_currentSelectable;
  98. }
  99. #endregion 对外提供的接口
  100. }
  101. public class ScreenRayRaycasterUIWrapper
  102. {
  103. public Selectable m_currentSelectable = null;
  104. RaycastResult m_currentRaycastResult;
  105. IPointerClickHandler m_clickHandler;
  106. IDragHandler m_dragHandler;
  107. EventSystem m_eventSystem;
  108. PointerEventData m_pointerEvent;
  109. SimulateMouse mSimulateMouse;
  110. Func<Vector2> mFunPointerPos;
  111. private bool mIsClick = false;
  112. private bool mIsPress = false;
  113. private bool mIsDrag = false;
  114. public void Init(SimulateMouse simulateMouse, Func<Vector2> funPointerPos)
  115. {
  116. mSimulateMouse = simulateMouse;
  117. m_eventSystem = EventSystem.current;
  118. m_pointerEvent = new PointerEventData(m_eventSystem);
  119. m_pointerEvent.button = PointerEventData.InputButton.Left;
  120. mFunPointerPos = funPointerPos;
  121. }
  122. public void Update()
  123. {
  124. m_pointerEvent.position = mFunPointerPos.Invoke();
  125. List<RaycastResult> raycastResults = new List<RaycastResult>();
  126. m_eventSystem.RaycastAll(m_pointerEvent, raycastResults);
  127. // Detect selectable
  128. bool hasRaycastSelectable = false;
  129. if (raycastResults.Count > 0)
  130. {
  131. var result = raycastResults[0]; //只检测第一个
  132. var newSelectable = result.gameObject.GetComponentInParent<Selectable>();
  133. if (newSelectable)
  134. {
  135. hasRaycastSelectable = true;
  136. if (newSelectable != m_currentSelectable)
  137. {
  138. Select(newSelectable);
  139. m_currentRaycastResult = result;
  140. }
  141. }
  142. }
  143. if (!hasRaycastSelectable)
  144. {
  145. if (m_currentSelectable)
  146. {
  147. Select(null);
  148. }
  149. }
  150. if (m_currentSelectable)
  151. {
  152. if (m_clickHandler != null && mIsClick)
  153. {
  154. try { mSimulateMouse.OnPointerClick?.Invoke(m_currentSelectable); }
  155. catch (System.Exception e) { Debug.LogError(e.Message); }
  156. m_clickHandler.OnPointerClick(m_pointerEvent);
  157. Select(null);
  158. }
  159. else if (m_dragHandler != null && mIsPress)
  160. {
  161. mIsDrag = true;
  162. m_pointerEvent.pointerPressRaycast = m_currentRaycastResult;
  163. m_dragHandler.OnDrag(m_pointerEvent);
  164. }
  165. }
  166. mIsClick = false;
  167. }
  168. void Select(Selectable s)
  169. {
  170. if (mIsDrag == true) return;
  171. if (m_currentSelectable) {
  172. m_currentSelectable.OnPointerExit(m_pointerEvent);
  173. }
  174. m_currentSelectable = s;
  175. if (m_currentSelectable) {
  176. try { mSimulateMouse.OnPointerEnter?.Invoke(m_currentSelectable); }
  177. catch (System.Exception e) { Debug.LogError(e.Message); }
  178. m_currentSelectable.OnPointerEnter(m_pointerEvent);
  179. m_clickHandler = m_currentSelectable.GetComponent<IPointerClickHandler>();
  180. m_dragHandler = m_currentSelectable.GetComponent<IDragHandler>();
  181. } else {
  182. m_clickHandler = null;
  183. m_dragHandler = null;
  184. }
  185. }
  186. //模拟点击
  187. public void Click() {
  188. mIsClick = true;
  189. }
  190. //模拟按下
  191. public void PressDown() {
  192. mIsClick = true;
  193. mIsPress = true;
  194. mIsDrag = false;
  195. }
  196. //模拟松开
  197. public void PressUp() {
  198. mIsClick = false;
  199. mIsPress = false;
  200. mIsDrag = false;
  201. Select(null);
  202. }
  203. }
  204. }