SimulateMouse.cs 7.9 KB

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