| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using JC.Unity;
- /*
- * SmartBow_事件系统
- * 可进入硬件控制虚拟鼠标模式
- */
- public class SB_EventSystem : MonoBehaviour
- {
- public static SB_EventSystem ins;
- MouseTest mouseTest;
- void Awake()
- {
- if (ins) {
- Destroy(this.gameObject);
- } else {
- ins = this;
- DontDestroyOnLoad(this.gameObject);
- AwakenSimulateMouse();
- }
- }
- void Start() {
- mouseTest = new MouseTest(this);
- // InitListenerForMouseClickHightColor();
- InitListenerForMouseHoverHightColor();
- }
- void Update() {
- UpdateMoveSimulateMouse();
- mouseTest.Update();
- }
-
- [SerializeField] SimulateMouse simulateMouse;
- #region 客户要求鼠标点到按钮时,按钮高亮
- // Graphic lockGraphic = null;
- // Color pointerClickColor = Color.yellow;
- // void InitListenerForMouseClickHightColor() {
- // simulateMouse.OnPointerClick += (Selectable target) => {
- // if (lockGraphic) return;
- // Button btn = target.GetComponent<Button>();
- // if (btn.transition != Selectable.Transition.ColorTint) return;
- // if (btn.targetGraphic) {
- // Graphic graphic = btn.targetGraphic;
- // lockGraphic = graphic;
- // Color oldColor = graphic.color;
- // graphic.color = pointerClickColor;
- // DoTweenUtil.CallDelay(0.3f, () => {
- // lockGraphic = null;
- // graphic.color = oldColor;
- // });
- // }
- // };
- // }
- Color pointerHoverColor = new Color(233f/255, 233f/255, 233f/255, 128f/255);
- void InitListenerForMouseHoverHightColor() {
- simulateMouse.OnPointerEnter += (Selectable target) => {
- Button btn = target.GetComponent<Button>();
- if (!btn) return;
- if (btn.transition != Selectable.Transition.ColorTint) return;
- if (!btn.interactable) return;
- ColorBlock colorBlock = btn.colors;
- colorBlock.highlightedColor = pointerHoverColor;
- btn.colors = colorBlock;
- };
- }
- #endregion
- #region 鼠标激活/关闭
- [System.NonSerialized] public bool simulateMouseIsAwaked;
- public void AwakenSimulateMouse() {
- simulateMouseIsAwaked = !simulateMouse.gameObject.activeSelf;
- simulateMouse.gameObject.SetActive(simulateMouseIsAwaked);
- hasAxisQuat = false;
- }
- #endregion 鼠标激活/关闭
- #region 模拟鼠标移动
- Vector3 targetNormalVector;
- Quaternion targetAxisQuat;
- Quaternion nowAxisQuat;
- bool hasAxisQuat;
- public void MoveSimulateMouse(Quaternion axisQuat) {
- if (clickCooling) { //点击就是射出,射出会产生抖动,防止接收抖动后的数据
- hasAxisQuat = false;
- return;
- }
- //格式化
- Vector3 normalVector = axisQuat * Vector3.forward; //得出对应的法向量
- axisQuat = Quaternion.LookRotation(normalVector); //变回四元组,主要作用是规范,去除z轴影响
- if (hasAxisQuat) {
- //法向量的z从+到-或从-到+,都会导致y轴转180度,也就是x轴旋转从<90跨越>90时触发的问题,这种情况要避免
- if (
- normalVector.z <= 0 && targetNormalVector.z >= 0 ||
- normalVector.z >= 0 && targetNormalVector.z <= 0
- ) {
- hasAxisQuat = false; //重置
- return;
- }
- //ok
- targetAxisQuat = axisQuat;
- targetNormalVector = normalVector;
- } else {
- nowAxisQuat = targetAxisQuat = axisQuat;
- targetNormalVector = normalVector;
- hasAxisQuat = true;
- }
- }
- Vector2 deltaVectorForMouse;
- void UpdateMoveSimulateMouse() {
- if (!simulateMouseIsAwaked) return;
- if (hasAxisQuat) {
- Vector3 lastAngle = nowAxisQuat.eulerAngles;
- nowAxisQuat = Quaternion.Lerp(nowAxisQuat, targetAxisQuat, 0.033f * 8);
- Vector3 curAngle = nowAxisQuat.eulerAngles;
- float dx = FormatDeltaAngleY(curAngle.y - lastAngle.y) / 60f * simulateMouse.GetScaleScreenWidth();
- float dy = -FormatDeltaAngleX(curAngle.x - lastAngle.x) / 40f * simulateMouse.GetScaleScreenHeight();
- deltaVectorForMouse.x = dx;
- deltaVectorForMouse.y = dy;
- simulateMouse.MoveMousePointer(deltaVectorForMouse);
- }
- }
- float FormatDeltaAngleX(float value)
- {
- return FormatDeltaAngleY(value);
- }
- float FormatDeltaAngleY(float value)
- {
- if (Mathf.Abs(value) > 180) {
- if (value < 0) {
- return 360f + value;
- }
- if (value > 0) {
- return value - 360f;
- }
- }
- return value;
- }
- #endregion 模拟鼠标移动
- //点击鼠标
- bool clickCooling = false; //防止高频连续多次点击
- public void ClickMouse() {
- if (clickCooling) return;
- clickCooling = true;
- DoTweenUtil.CallDelay(0.3f, () => {
- clickCooling = false;
- });
- simulateMouse.ClickMousePointer();
- }
- //鼠标居中
- public void MakeMouseToScreenCenter() {
- hasAxisQuat = false;
- simulateMouse.MakeMouseToScreenCenter();
- }
- /** 鼠标测试类 */
- class MouseTest {
- SB_EventSystem es;
- float mouseTestSensitivity = 3f;
- bool mouseTest = false; //开启测试-开关
- public MouseTest(SB_EventSystem es) {
- this.es = es;
- }
- public void Update() {
- if (mouseTest) {
- if (Input.GetKey(KeyCode.A)) {
- es.deltaVectorForMouse.x = -mouseTestSensitivity;
- }
- else if (Input.GetKey(KeyCode.D)) {
- es.deltaVectorForMouse.x = +mouseTestSensitivity;
- } else {
- es.deltaVectorForMouse.x = 0;
- }
- if (Input.GetKey(KeyCode.W)) {
- es.deltaVectorForMouse.y = +mouseTestSensitivity;
- }
- else if (Input.GetKey(KeyCode.S)) {
- es.deltaVectorForMouse.y = -mouseTestSensitivity;
- } else {
- es.deltaVectorForMouse.y = 0;
- }
- es.simulateMouse.MoveMousePointer(es.deltaVectorForMouse);
- if (Input.GetKeyDown(KeyCode.E)) {
- es.AwakenSimulateMouse();
- }
- if (Input.GetKeyDown(KeyCode.R)) {
- es.simulateMouse.ClickMousePointer();
- }
- }
- }
- }
- }
|