SimulateMouse.cs 8.3 KB

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