SB_EventSystem.cs 7.3 KB

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