SB_EventSystem.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using JC.Unity;
  6. /*
  7. * 姿态角鼠标
  8. * SmartBow_事件系统
  9. * 可进入硬件控制虚拟鼠标模式
  10. */
  11. public class SB_EventSystem : MonoBehaviour
  12. {
  13. public static SB_EventSystem ins;
  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. InitListenerForMouseHoverHightColor();
  26. }
  27. void Update() {
  28. UpdateMoveSimulateMouse();
  29. }
  30. [SerializeField] SimulateMouse simulateMouse;
  31. #region 客户要求鼠标点到按钮时,按钮高亮
  32. Color pointerHoverColor = new Color(233f/255, 233f/255, 233f/255, 128f/255);
  33. void InitListenerForMouseHoverHightColor() {
  34. simulateMouse.OnPointerEnter += (Selectable target) => {
  35. Button btn = target.GetComponent<Button>();
  36. if (!btn) return;
  37. if (btn.transition != Selectable.Transition.ColorTint) return;
  38. if (!btn.interactable) return;
  39. ColorBlock colorBlock = btn.colors;
  40. colorBlock.highlightedColor = pointerHoverColor;
  41. btn.colors = colorBlock;
  42. };
  43. }
  44. #endregion
  45. #region 鼠标激活/关闭
  46. [System.NonSerialized] public bool simulateMouseIsAwaked;
  47. public void AwakenSimulateMouse() {
  48. simulateMouseIsAwaked = !simulateMouse.gameObject.activeSelf;
  49. simulateMouse.gameObject.SetActive(simulateMouseIsAwaked);
  50. try {
  51. GlobalEventCenter.ins.onSimulateMouseAwakeChanged?.Invoke(simulateMouseIsAwaked);
  52. } catch (System.Exception e) { Debug.LogError(e.Message); }
  53. }
  54. #endregion 鼠标激活/关闭
  55. #region 模拟鼠标移动
  56. Quaternion nowAxisQuat = Quaternion.identity;
  57. Quaternion newAxisQuat;
  58. Vector2 mousePointerPosition;
  59. public void MoveSimulateMouse(Quaternion axisQuat) {
  60. if (IsClickMouseCooling()) { //点击就是射出,射出会产生抖动,防止接收抖动后的数据
  61. return;
  62. }
  63. newAxisQuat = axisQuat;
  64. }
  65. void UpdateMoveSimulateMouse() {
  66. if (!simulateMouseIsAwaked) return;
  67. nowAxisQuat = Quaternion.Lerp(nowAxisQuat, newAxisQuat, 0.033f * 8);
  68. Vector3 resEulerAngles = FormatQuat(nowAxisQuat).eulerAngles;
  69. resEulerAngles.x = Mathf.Clamp(resEulerAngles.x > 180 ? resEulerAngles.x - 360 : resEulerAngles.x, -12.5f, 12.5f);
  70. resEulerAngles.y = Mathf.Clamp(resEulerAngles.y > 180 ? resEulerAngles.y - 360 : resEulerAngles.y, -22.2f, 22.2f);
  71. mousePointerPosition.x = (resEulerAngles.y + 22.2f) / 44.4f * Screen.width;
  72. mousePointerPosition.y = (12.5f - resEulerAngles.x) / 25f * Screen.height;
  73. simulateMouse.SetMousePointerPosition(mousePointerPosition);
  74. }
  75. Quaternion FormatQuat(Quaternion quat) {
  76. Vector3 normalVector = quat * Vector3.forward; //得出对应的法向量
  77. return Quaternion.LookRotation(normalVector); //变回四元组,主要作用是规范,去除z轴影响
  78. }
  79. #endregion 模拟鼠标移动
  80. //点击鼠标
  81. float _lastClickMouseTime = -100;
  82. void RecordClickMouseTime() {
  83. _lastClickMouseTime = Time.realtimeSinceStartup;
  84. }
  85. bool IsClickMouseCooling() { //防止高频连续多次点击
  86. return Time.realtimeSinceStartup - _lastClickMouseTime < 1;
  87. }
  88. public void ClickMouse() {
  89. bool isCooling = IsClickMouseCooling();
  90. RecordClickMouseTime();
  91. if (isCooling) return;
  92. simulateMouse.ClickMousePointer();
  93. }
  94. }
  95. /**传统鼠标 */
  96. // public class SB_EventSystem : MonoBehaviour
  97. // {
  98. // public static SB_EventSystem ins;
  99. // MouseTest mouseTest;
  100. // void Awake()
  101. // {
  102. // if (ins) {
  103. // Destroy(this.gameObject);
  104. // } else {
  105. // ins = this;
  106. // DontDestroyOnLoad(this.gameObject);
  107. // AwakenSimulateMouse();
  108. // }
  109. // }
  110. // void Start() {
  111. // mouseTest = new MouseTest(this);
  112. // // InitListenerForMouseClickHightColor();
  113. // InitListenerForMouseHoverHightColor();
  114. // }
  115. // void Update() {
  116. // UpdateMoveSimulateMouse();
  117. // mouseTest.Update();
  118. // }
  119. // [SerializeField] SimulateMouse simulateMouse;
  120. // #region 客户要求鼠标点到按钮时,按钮高亮
  121. // // Graphic lockGraphic = null;
  122. // // Color pointerClickColor = Color.yellow;
  123. // // void InitListenerForMouseClickHightColor() {
  124. // // simulateMouse.OnPointerClick += (Selectable target) => {
  125. // // if (lockGraphic) return;
  126. // // Button btn = target.GetComponent<Button>();
  127. // // if (btn.transition != Selectable.Transition.ColorTint) return;
  128. // // if (btn.targetGraphic) {
  129. // // Graphic graphic = btn.targetGraphic;
  130. // // lockGraphic = graphic;
  131. // // Color oldColor = graphic.color;
  132. // // graphic.color = pointerClickColor;
  133. // // DoTweenUtil.CallDelay(0.3f, () => {
  134. // // lockGraphic = null;
  135. // // graphic.color = oldColor;
  136. // // });
  137. // // }
  138. // // };
  139. // // }
  140. // Color pointerHoverColor = new Color(233f/255, 233f/255, 233f/255, 128f/255);
  141. // void InitListenerForMouseHoverHightColor() {
  142. // simulateMouse.OnPointerEnter += (Selectable target) => {
  143. // Button btn = target.GetComponent<Button>();
  144. // if (!btn) return;
  145. // if (btn.transition != Selectable.Transition.ColorTint) return;
  146. // if (!btn.interactable) return;
  147. // ColorBlock colorBlock = btn.colors;
  148. // colorBlock.highlightedColor = pointerHoverColor;
  149. // btn.colors = colorBlock;
  150. // };
  151. // }
  152. // #endregion
  153. // #region 鼠标激活/关闭
  154. // [System.NonSerialized] public bool simulateMouseIsAwaked;
  155. // public void AwakenSimulateMouse() {
  156. // simulateMouseIsAwaked = !simulateMouse.gameObject.activeSelf;
  157. // simulateMouse.gameObject.SetActive(simulateMouseIsAwaked);
  158. // hasAxisQuat = false;
  159. // try {
  160. // GlobalEventCenter.ins.onSimulateMouseAwakeChanged?.Invoke(simulateMouseIsAwaked);
  161. // } catch (System.Exception e) { Debug.LogError(e.Message); }
  162. // }
  163. // #endregion 鼠标激活/关闭
  164. // #region 模拟鼠标移动
  165. // Vector3 targetNormalVector;
  166. // Quaternion targetAxisQuat;
  167. // Quaternion nowAxisQuat;
  168. // bool hasAxisQuat;
  169. // public void MoveSimulateMouse(Quaternion axisQuat) {
  170. // if (IsClickMouseCooling()) { //点击就是射出,射出会产生抖动,防止接收抖动后的数据
  171. // hasAxisQuat = false;
  172. // return;
  173. // }
  174. // //格式化
  175. // Vector3 normalVector = axisQuat * Vector3.forward; //得出对应的法向量
  176. // axisQuat = Quaternion.LookRotation(normalVector); //变回四元组,主要作用是规范,去除z轴影响
  177. // if (hasAxisQuat) {
  178. // //法向量的z从+到-或从-到+,都会导致y轴转180度,也就是x轴旋转从<90跨越>90时触发的问题,这种情况要避免
  179. // if (
  180. // normalVector.z <= 0 && targetNormalVector.z >= 0 ||
  181. // normalVector.z >= 0 && targetNormalVector.z <= 0
  182. // ) {
  183. // hasAxisQuat = false; //重置
  184. // return;
  185. // }
  186. // //ok
  187. // targetAxisQuat = axisQuat;
  188. // targetNormalVector = normalVector;
  189. // } else {
  190. // nowAxisQuat = targetAxisQuat = axisQuat;
  191. // targetNormalVector = normalVector;
  192. // hasAxisQuat = true;
  193. // }
  194. // }
  195. // Vector2 deltaVectorForMouse;
  196. // void UpdateMoveSimulateMouse() {
  197. // if (!simulateMouseIsAwaked) return;
  198. // if (hasAxisQuat) {
  199. // Vector3 lastAngle = nowAxisQuat.eulerAngles;
  200. // nowAxisQuat = Quaternion.Lerp(nowAxisQuat, targetAxisQuat, 0.033f * 8);
  201. // Vector3 curAngle = nowAxisQuat.eulerAngles;
  202. // float dx = FormatDeltaAngleY(curAngle.y - lastAngle.y) / 30f * simulateMouse.GetScaleScreenWidth();
  203. // float dy = -FormatDeltaAngleX(curAngle.x - lastAngle.x) / 21f * simulateMouse.GetScaleScreenHeight();
  204. // deltaVectorForMouse.x = dx;
  205. // deltaVectorForMouse.y = dy;
  206. // simulateMouse.MoveMousePointer(deltaVectorForMouse);
  207. // }
  208. // }
  209. // float FormatDeltaAngleX(float value)
  210. // {
  211. // return FormatDeltaAngleY(value);
  212. // }
  213. // float FormatDeltaAngleY(float value)
  214. // {
  215. // if (Mathf.Abs(value) > 180) {
  216. // if (value < 0) {
  217. // return 360f + value;
  218. // }
  219. // if (value > 0) {
  220. // return value - 360f;
  221. // }
  222. // }
  223. // return value;
  224. // }
  225. // #endregion 模拟鼠标移动
  226. // //点击鼠标
  227. // float _lastClickMouseTime = -100;
  228. // void RecordClickMouseTime() {
  229. // _lastClickMouseTime = Time.realtimeSinceStartup;
  230. // }
  231. // bool IsClickMouseCooling() { //防止高频连续多次点击
  232. // return Time.realtimeSinceStartup - _lastClickMouseTime < 1;
  233. // }
  234. // public void ClickMouse() {
  235. // AimHandler.ins._9Axis.axisCSBridge.o09AxisCS.OnShot(100, 100, 100000);
  236. // bool isCooling = IsClickMouseCooling();
  237. // RecordClickMouseTime();
  238. // if (isCooling) return;
  239. // simulateMouse.ClickMousePointer();
  240. // }
  241. // //鼠标居中
  242. // public void MakeMouseToScreenCenter() {
  243. // hasAxisQuat = false;
  244. // simulateMouse.MakeMouseToScreenCenter();
  245. // }
  246. // /** 鼠标测试类 */
  247. // class MouseTest {
  248. // SB_EventSystem es;
  249. // float mouseTestSensitivity = 3f;
  250. // bool mouseTest = false; //开启测试-开关
  251. // public MouseTest(SB_EventSystem es) {
  252. // this.es = es;
  253. // }
  254. // public void Update() {
  255. // if (mouseTest) {
  256. // if (Input.GetKey(KeyCode.A)) {
  257. // es.deltaVectorForMouse.x = -mouseTestSensitivity;
  258. // }
  259. // else if (Input.GetKey(KeyCode.D)) {
  260. // es.deltaVectorForMouse.x = +mouseTestSensitivity;
  261. // } else {
  262. // es.deltaVectorForMouse.x = 0;
  263. // }
  264. // if (Input.GetKey(KeyCode.W)) {
  265. // es.deltaVectorForMouse.y = +mouseTestSensitivity;
  266. // }
  267. // else if (Input.GetKey(KeyCode.S)) {
  268. // es.deltaVectorForMouse.y = -mouseTestSensitivity;
  269. // } else {
  270. // es.deltaVectorForMouse.y = 0;
  271. // }
  272. // es.simulateMouse.MoveMousePointer(es.deltaVectorForMouse);
  273. // if (Input.GetKeyDown(KeyCode.E)) {
  274. // es.AwakenSimulateMouse();
  275. // }
  276. // if (Input.GetKeyDown(KeyCode.R)) {
  277. // es.simulateMouse.ClickMousePointer();
  278. // }
  279. // }
  280. // }
  281. // }
  282. // }