SB_EventSystem.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using JC.Unity;
  6. /**SmartBow_事件系统 */
  7. public class SB_EventSystem : MonoBehaviour
  8. {
  9. public static SB_EventSystem ins;
  10. MouseTest mouseTest;
  11. void Awake()
  12. {
  13. if (ins) {
  14. Destroy(this.gameObject);
  15. } else {
  16. ins = this;
  17. DontDestroyOnLoad(this.gameObject);
  18. AwakenSimulateMouse();
  19. }
  20. }
  21. void Start() {
  22. mouseTest = new MouseTest(this);
  23. // InitListenerForMouseClickHightColor();
  24. InitListenerForMouseHoverHightColor();
  25. }
  26. void Update() {
  27. UpdateMoveSimulateMouse();
  28. mouseTest.Update();
  29. }
  30. [SerializeField] SimulateMouse simulateMouse;
  31. #region 客户要求鼠标点到按钮时,按钮高亮
  32. // Graphic lockGraphic = null;
  33. // Color pointerClickColor = Color.yellow;
  34. // void InitListenerForMouseClickHightColor() {
  35. // simulateMouse.OnPointerClick += (Selectable target) => {
  36. // if (lockGraphic) return;
  37. // Button btn = target.GetComponent<Button>();
  38. // if (btn.transition != Selectable.Transition.ColorTint) return;
  39. // if (btn.targetGraphic) {
  40. // Graphic graphic = btn.targetGraphic;
  41. // lockGraphic = graphic;
  42. // Color oldColor = graphic.color;
  43. // graphic.color = pointerClickColor;
  44. // DoTweenUtil.CallDelay(0.3f, () => {
  45. // lockGraphic = null;
  46. // graphic.color = oldColor;
  47. // });
  48. // }
  49. // };
  50. // }
  51. Color pointerHoverColor = Color.yellow;
  52. void InitListenerForMouseHoverHightColor() {
  53. simulateMouse.OnPointerEnter += (Selectable target) => {
  54. Button btn = target.GetComponent<Button>();
  55. if (!btn) return;
  56. if (btn.transition != Selectable.Transition.ColorTint) return;
  57. ColorBlock colorBlock = btn.colors;
  58. colorBlock.highlightedColor = pointerHoverColor;
  59. btn.colors = colorBlock;
  60. };
  61. }
  62. #endregion
  63. #region 鼠标激活/关闭
  64. [System.NonSerialized] public bool simulateMouseIsAwaked;
  65. public void AwakenSimulateMouse() {
  66. simulateMouseIsAwaked = !simulateMouse.gameObject.activeSelf;
  67. simulateMouse.gameObject.SetActive(simulateMouseIsAwaked);
  68. hasAxisQuat = false;
  69. try {
  70. GlobalEventCenter.ins.onSimulateMouseAwakeChanged?.Invoke(simulateMouseIsAwaked);
  71. } catch (System.Exception e) { Debug.LogError(e.Message); }
  72. }
  73. #endregion 鼠标激活/关闭
  74. #region 模拟鼠标移动
  75. Vector3 targetNormalVector;
  76. Quaternion targetAxisQuat;
  77. Quaternion nowAxisQuat;
  78. bool hasAxisQuat;
  79. public void MoveSimulateMouse(Quaternion axisQuat) {
  80. if (clickCooling) { //点击就是射出,射出会产生抖动,防止接收抖动后的数据
  81. hasAxisQuat = false;
  82. return;
  83. }
  84. //格式化
  85. Vector3 normalVector = axisQuat * Vector3.forward; //得出对应的法向量
  86. axisQuat = Quaternion.LookRotation(normalVector); //变回四元组,主要作用是规范,去除z轴影响
  87. if (hasAxisQuat) {
  88. //法向量的z从+到-或从-到+,都会导致y轴转180度,也就是x轴旋转从<90跨越>90时触发的问题,这种情况要避免
  89. if (
  90. normalVector.z <= 0 && targetNormalVector.z >= 0 ||
  91. normalVector.z >= 0 && targetNormalVector.z <= 0
  92. ) {
  93. hasAxisQuat = false; //重置
  94. return;
  95. }
  96. //ok
  97. targetAxisQuat = axisQuat;
  98. targetNormalVector = normalVector;
  99. } else {
  100. nowAxisQuat = targetAxisQuat = axisQuat;
  101. targetNormalVector = normalVector;
  102. hasAxisQuat = true;
  103. }
  104. }
  105. Vector2 deltaVectorForMouse;
  106. void UpdateMoveSimulateMouse() {
  107. if (!simulateMouseIsAwaked) return;
  108. if (hasAxisQuat) {
  109. Vector3 lastAngle = nowAxisQuat.eulerAngles;
  110. nowAxisQuat = Quaternion.Lerp(nowAxisQuat, targetAxisQuat, 0.033f * 8);
  111. Vector3 curAngle = nowAxisQuat.eulerAngles;
  112. float dx = FormatDeltaAngleY(curAngle.y - lastAngle.y) / 150f * simulateMouse.GetScaleScreenWidth();
  113. float dy = -FormatDeltaAngleX(curAngle.x - lastAngle.x) / 100f * simulateMouse.GetScaleScreenHeight();
  114. deltaVectorForMouse.x = dx;
  115. deltaVectorForMouse.y = dy;
  116. simulateMouse.MoveMousePointer(deltaVectorForMouse);
  117. }
  118. }
  119. float FormatDeltaAngleX(float value)
  120. {
  121. return FormatDeltaAngleY(value);
  122. }
  123. float FormatDeltaAngleY(float value)
  124. {
  125. if (Mathf.Abs(value) > 180) {
  126. if (value < 0) {
  127. return 360f + value;
  128. }
  129. if (value > 0) {
  130. return value - 360f;
  131. }
  132. }
  133. return value;
  134. }
  135. #endregion 模拟鼠标移动
  136. //点击鼠标
  137. bool clickCooling = false; //防止高频连续多次点击
  138. public void ClickMouse() {
  139. if (clickCooling) return;
  140. clickCooling = true;
  141. DoTweenUtil.CallDelay(0.3f, () => {
  142. clickCooling = false;
  143. });
  144. simulateMouse.ClickMousePointer();
  145. }
  146. //鼠标居中
  147. public void MakeMouseToScreenCenter() {
  148. hasAxisQuat = false;
  149. simulateMouse.MakeMouseToScreenCenter();
  150. }
  151. /** 鼠标测试类 */
  152. class MouseTest {
  153. SB_EventSystem es;
  154. float mouseTestSensitivity = 3f;
  155. bool mouseTest = false; //开启测试-开关
  156. public MouseTest(SB_EventSystem es) {
  157. this.es = es;
  158. }
  159. public void Update() {
  160. if (mouseTest) {
  161. if (Input.GetKey(KeyCode.A)) {
  162. es.deltaVectorForMouse.x = -mouseTestSensitivity;
  163. }
  164. else if (Input.GetKey(KeyCode.D)) {
  165. es.deltaVectorForMouse.x = +mouseTestSensitivity;
  166. } else {
  167. es.deltaVectorForMouse.x = 0;
  168. }
  169. if (Input.GetKey(KeyCode.W)) {
  170. es.deltaVectorForMouse.y = +mouseTestSensitivity;
  171. }
  172. else if (Input.GetKey(KeyCode.S)) {
  173. es.deltaVectorForMouse.y = -mouseTestSensitivity;
  174. } else {
  175. es.deltaVectorForMouse.y = 0;
  176. }
  177. es.simulateMouse.MoveMousePointer(es.deltaVectorForMouse);
  178. if (Input.GetKeyDown(KeyCode.E)) {
  179. es.AwakenSimulateMouse();
  180. }
  181. if (Input.GetKeyDown(KeyCode.R)) {
  182. es.simulateMouse.ClickMousePointer();
  183. }
  184. }
  185. }
  186. }
  187. }