SimulateMouse.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 ClickMousePointer()
  85. {
  86. mScreenRayRaycasterUIWrapper.Click();
  87. }
  88. public void MakeMouseToScreenCenter() {
  89. mousePointer.anchoredPosition = new Vector2(GetScaleScreenWidth()/2,GetScaleScreenHeight()/2);
  90. }
  91. #endregion 对外提供的接口
  92. }
  93. public class ScreenRayRaycasterUIWrapper
  94. {
  95. Selectable m_currentSelectable = null;
  96. RaycastResult m_currentRaycastResult;
  97. IPointerClickHandler m_clickHandler;
  98. IDragHandler m_dragHandler;
  99. EventSystem m_eventSystem;
  100. PointerEventData m_pointerEvent;
  101. SimulateMouse mSimulateMouse;
  102. Func<Vector2> mFunPointerPos;
  103. private bool mIsClick = false;
  104. private bool mIsPress = false;
  105. private bool mIsDrag = false;
  106. public void Init(SimulateMouse simulateMouse, Func<Vector2> funPointerPos)
  107. {
  108. mSimulateMouse = simulateMouse;
  109. m_eventSystem = EventSystem.current;
  110. m_pointerEvent = new PointerEventData(m_eventSystem);
  111. m_pointerEvent.button = PointerEventData.InputButton.Left;
  112. mFunPointerPos = funPointerPos;
  113. }
  114. public void Update()
  115. {
  116. m_pointerEvent.position = mFunPointerPos.Invoke();
  117. List<RaycastResult> raycastResults = new List<RaycastResult>();
  118. m_eventSystem.RaycastAll(m_pointerEvent, raycastResults);
  119. // Detect selectable
  120. bool hasRaycastSelectable = false;
  121. if (raycastResults.Count > 0)
  122. {
  123. foreach (var result in raycastResults)
  124. {
  125. var newSelectable = result.gameObject.GetComponentInParent<Selectable>();
  126. if (newSelectable)
  127. {
  128. hasRaycastSelectable = true;
  129. if (newSelectable != m_currentSelectable)
  130. {
  131. Select(newSelectable);
  132. m_currentRaycastResult = result;
  133. }
  134. break;
  135. }
  136. }
  137. }
  138. if (!hasRaycastSelectable)
  139. {
  140. if (m_currentSelectable)
  141. {
  142. Select(null);
  143. }
  144. }
  145. if (m_currentSelectable)
  146. {
  147. if (m_clickHandler != null && mIsClick)
  148. {
  149. try { mSimulateMouse.OnPointerClick?.Invoke(m_currentSelectable); }
  150. catch (System.Exception e) { Debug.LogError(e.Message); }
  151. m_clickHandler.OnPointerClick(m_pointerEvent);
  152. Select(null);
  153. }
  154. else if (m_dragHandler != null && mIsPress)
  155. {
  156. mIsDrag = true;
  157. m_pointerEvent.pointerPressRaycast = m_currentRaycastResult;
  158. m_dragHandler.OnDrag(m_pointerEvent);
  159. }
  160. }
  161. mIsClick = false;
  162. }
  163. void Select(Selectable s)
  164. {
  165. if (mIsDrag == true) return;
  166. if (m_currentSelectable) {
  167. m_currentSelectable.OnPointerExit(m_pointerEvent);
  168. }
  169. m_currentSelectable = s;
  170. if (m_currentSelectable) {
  171. try { mSimulateMouse.OnPointerEnter?.Invoke(m_currentSelectable); }
  172. catch (System.Exception e) { Debug.LogError(e.Message); }
  173. m_currentSelectable.OnPointerEnter(m_pointerEvent);
  174. m_clickHandler = m_currentSelectable.GetComponent<IPointerClickHandler>();
  175. m_dragHandler = m_currentSelectable.GetComponent<IDragHandler>();
  176. } else {
  177. m_clickHandler = null;
  178. m_dragHandler = null;
  179. }
  180. }
  181. //模拟点击
  182. public void Click() {
  183. mIsClick = true;
  184. }
  185. //模拟按下
  186. public void PressDown() {
  187. mIsClick = true;
  188. mIsPress = true;
  189. mIsDrag = false;
  190. }
  191. //模拟松开
  192. public void PressUp() {
  193. mIsClick = false;
  194. mIsPress = false;
  195. mIsDrag = false;
  196. Select(null);
  197. }
  198. }
  199. }