SB_EventSystem.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. }
  74. #endregion 鼠标激活/关闭
  75. #region 模拟鼠标移动
  76. Vector3 targetNormalVector;
  77. Quaternion targetAxisQuat;
  78. Quaternion nowAxisQuat;
  79. bool hasAxisQuat;
  80. public void MoveSimulateMouse(Quaternion axisQuat) {
  81. if (clickCooling) { //点击就是射出,射出会产生抖动,防止接收抖动后的数据
  82. hasAxisQuat = false;
  83. return;
  84. }
  85. //格式化
  86. Vector3 normalVector = axisQuat * Vector3.forward; //得出对应的法向量
  87. axisQuat = Quaternion.LookRotation(normalVector); //变回四元组,主要作用是规范,去除z轴影响
  88. if (hasAxisQuat) {
  89. //法向量的z从+到-或从-到+,都会导致y轴转180度,也就是x轴旋转从<90跨越>90时触发的问题,这种情况要避免
  90. if (
  91. normalVector.z <= 0 && targetNormalVector.z >= 0 ||
  92. normalVector.z >= 0 && targetNormalVector.z <= 0
  93. ) {
  94. hasAxisQuat = false; //重置
  95. return;
  96. }
  97. //ok
  98. targetAxisQuat = axisQuat;
  99. targetNormalVector = normalVector;
  100. } else {
  101. nowAxisQuat = targetAxisQuat = axisQuat;
  102. targetNormalVector = normalVector;
  103. hasAxisQuat = true;
  104. }
  105. }
  106. Vector2 deltaVectorForMouse;
  107. void UpdateMoveSimulateMouse() {
  108. if (!simulateMouseIsAwaked) return;
  109. if (hasAxisQuat) {
  110. Vector3 lastAngle = nowAxisQuat.eulerAngles;
  111. nowAxisQuat = Quaternion.Lerp(nowAxisQuat, targetAxisQuat, 0.033f * 8);
  112. Vector3 curAngle = nowAxisQuat.eulerAngles;
  113. float dx = FormatDeltaAngleY(curAngle.y - lastAngle.y) / 60f * simulateMouse.GetScaleScreenWidth();
  114. float dy = -FormatDeltaAngleX(curAngle.x - lastAngle.x) / 40f * simulateMouse.GetScaleScreenHeight();
  115. deltaVectorForMouse.x = dx;
  116. deltaVectorForMouse.y = dy;
  117. simulateMouse.MoveMousePointer(deltaVectorForMouse);
  118. }
  119. }
  120. float FormatDeltaAngleX(float value)
  121. {
  122. return FormatDeltaAngleY(value);
  123. }
  124. float FormatDeltaAngleY(float value)
  125. {
  126. if (Mathf.Abs(value) > 180) {
  127. if (value < 0) {
  128. return 360f + value;
  129. }
  130. if (value > 0) {
  131. return value - 360f;
  132. }
  133. }
  134. return value;
  135. }
  136. #endregion 模拟鼠标移动
  137. //点击鼠标
  138. bool clickCooling = false; //防止高频连续多次点击
  139. public void ClickMouse() {
  140. if (clickCooling) return;
  141. clickCooling = true;
  142. DoTweenUtil.CallDelay(0.3f, () => {
  143. clickCooling = false;
  144. });
  145. simulateMouse.ClickMousePointer();
  146. }
  147. //鼠标居中
  148. public void MakeMouseToScreenCenter() {
  149. hasAxisQuat = false;
  150. simulateMouse.MakeMouseToScreenCenter();
  151. }
  152. /** 鼠标测试类 */
  153. class MouseTest {
  154. SB_EventSystem es;
  155. float mouseTestSensitivity = 3f;
  156. bool mouseTest = false; //开启测试-开关
  157. public MouseTest(SB_EventSystem es) {
  158. this.es = es;
  159. }
  160. public void Update() {
  161. if (mouseTest) {
  162. if (Input.GetKey(KeyCode.A)) {
  163. es.deltaVectorForMouse.x = -mouseTestSensitivity;
  164. }
  165. else if (Input.GetKey(KeyCode.D)) {
  166. es.deltaVectorForMouse.x = +mouseTestSensitivity;
  167. } else {
  168. es.deltaVectorForMouse.x = 0;
  169. }
  170. if (Input.GetKey(KeyCode.W)) {
  171. es.deltaVectorForMouse.y = +mouseTestSensitivity;
  172. }
  173. else if (Input.GetKey(KeyCode.S)) {
  174. es.deltaVectorForMouse.y = -mouseTestSensitivity;
  175. } else {
  176. es.deltaVectorForMouse.y = 0;
  177. }
  178. es.simulateMouse.MoveMousePointer(es.deltaVectorForMouse);
  179. if (Input.GetKeyDown(KeyCode.E)) {
  180. es.AwakenSimulateMouse();
  181. }
  182. if (Input.GetKeyDown(KeyCode.R)) {
  183. es.simulateMouse.ClickMousePointer();
  184. }
  185. }
  186. }
  187. }
  188. }