SB_EventSystem.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using JC.Unity;
  6. using DG.Tweening;
  7. /**SmartBow_事件系统 */
  8. public class SB_EventSystem : MonoBehaviour
  9. {
  10. public static SB_EventSystem ins;
  11. MouseTest mouseTest;
  12. void Awake()
  13. {
  14. if (ins) {
  15. Destroy(this.gameObject);
  16. } else {
  17. ins = this;
  18. DontDestroyOnLoad(this.gameObject);
  19. AwakenSimulateMouse();
  20. }
  21. }
  22. void Start() {
  23. mouseTest = new MouseTest(this);
  24. InitListenerForMouseClickHightColor();
  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. #endregion
  52. #region 鼠标激活/关闭
  53. [System.NonSerialized] public bool simulateMouseIsAwaked;
  54. public void AwakenSimulateMouse() {
  55. simulateMouseIsAwaked = !simulateMouse.gameObject.activeSelf;
  56. simulateMouse.gameObject.SetActive(simulateMouseIsAwaked);
  57. hasAxisQuat = false;
  58. }
  59. #endregion 鼠标激活/关闭
  60. #region 模拟鼠标移动
  61. Vector3 targetNormalVector;
  62. Quaternion targetAxisQuat;
  63. Quaternion nowAxisQuat;
  64. bool hasAxisQuat;
  65. public void MoveSimulateMouse(Quaternion axisQuat) {
  66. if (clickCooling) { //点击就是射出,射出会产生抖动,防止接收抖动后的数据
  67. hasAxisQuat = false;
  68. return;
  69. }
  70. //格式化
  71. Vector3 normalVector = axisQuat * Vector3.forward; //得出对应的法向量
  72. axisQuat = Quaternion.LookRotation(normalVector); //变回四元组,主要作用是规范,去除z轴影响
  73. if (hasAxisQuat) {
  74. //法向量的z从+到-或从-到+,都会导致y轴转180度,也就是x轴旋转从<90跨越>90时触发的问题,这种情况要避免
  75. if (
  76. normalVector.z <= 0 && targetNormalVector.z >= 0 ||
  77. normalVector.z >= 0 && targetNormalVector.z <= 0
  78. ) {
  79. hasAxisQuat = false; //重置
  80. return;
  81. }
  82. //ok
  83. targetAxisQuat = axisQuat;
  84. targetNormalVector = normalVector;
  85. } else {
  86. nowAxisQuat = targetAxisQuat = axisQuat;
  87. targetNormalVector = normalVector;
  88. hasAxisQuat = true;
  89. }
  90. }
  91. Vector2 deltaVectorForMouse;
  92. void UpdateMoveSimulateMouse() {
  93. if (!simulateMouseIsAwaked) return;
  94. if (hasAxisQuat) {
  95. Vector3 lastAngle = nowAxisQuat.eulerAngles;
  96. nowAxisQuat = Quaternion.Lerp(nowAxisQuat, targetAxisQuat, Time.deltaTime * 15f);
  97. Vector3 curAngle = nowAxisQuat.eulerAngles;
  98. float dx = FormatDeltaAngleY(curAngle.y - lastAngle.y) / 150f * simulateMouse.GetScaleScreenWidth();
  99. float dy = -FormatDeltaAngleX(curAngle.x - lastAngle.x) / 150f * simulateMouse.GetScaleScreenHeight();
  100. deltaVectorForMouse.x = dx;
  101. deltaVectorForMouse.y = dy;
  102. simulateMouse.MoveMousePointer(deltaVectorForMouse);
  103. }
  104. }
  105. float FormatDeltaAngleX(float value)
  106. {
  107. return FormatDeltaAngleY(value);
  108. }
  109. float FormatDeltaAngleY(float value)
  110. {
  111. if (Mathf.Abs(value) > 180) {
  112. if (value < 0) {
  113. return 360f + value;
  114. }
  115. if (value > 0) {
  116. return value - 360f;
  117. }
  118. }
  119. return value;
  120. }
  121. #endregion 模拟鼠标移动
  122. //点击鼠标
  123. bool clickCooling = false; //防止高频连续多次点击
  124. public void ClickMouse() {
  125. if (clickCooling) return;
  126. clickCooling = true;
  127. DoTweenUtil.CallDelay(0.3f, () => {
  128. clickCooling = false;
  129. });
  130. simulateMouse.ClickMousePointer();
  131. }
  132. //鼠标居中
  133. public void MakeMouseToScreenCenter() {
  134. simulateMouse.MakeMouseToScreenCenter();
  135. }
  136. /** 鼠标测试类 */
  137. class MouseTest {
  138. SB_EventSystem es;
  139. float mouseTestSensitivity = 3f;
  140. bool mouseTest = false; //开启测试-开关
  141. public MouseTest(SB_EventSystem es) {
  142. this.es = es;
  143. }
  144. public void Update() {
  145. if (mouseTest) {
  146. if (Input.GetKey(KeyCode.A)) {
  147. es.deltaVectorForMouse.x = -mouseTestSensitivity;
  148. }
  149. else if (Input.GetKey(KeyCode.D)) {
  150. es.deltaVectorForMouse.x = +mouseTestSensitivity;
  151. } else {
  152. es.deltaVectorForMouse.x = 0;
  153. }
  154. if (Input.GetKey(KeyCode.W)) {
  155. es.deltaVectorForMouse.y = +mouseTestSensitivity;
  156. }
  157. else if (Input.GetKey(KeyCode.S)) {
  158. es.deltaVectorForMouse.y = -mouseTestSensitivity;
  159. } else {
  160. es.deltaVectorForMouse.y = 0;
  161. }
  162. es.simulateMouse.MoveMousePointer(es.deltaVectorForMouse);
  163. if (Input.GetKeyDown(KeyCode.E)) {
  164. es.AwakenSimulateMouse();
  165. }
  166. if (Input.GetKeyDown(KeyCode.R)) {
  167. es.simulateMouse.ClickMousePointer();
  168. }
  169. }
  170. }
  171. }
  172. }