|
|
@@ -1,11 +1,15 @@
|
|
|
using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
|
using UnityEngine;
|
|
|
+using UnityEngine.UI;
|
|
|
using JC.Unity;
|
|
|
+using DG.Tweening;
|
|
|
|
|
|
+/**SmartBow_事件系统 */
|
|
|
public class SB_EventSystem : MonoBehaviour
|
|
|
{
|
|
|
public static SB_EventSystem ins;
|
|
|
+ MouseTest mouseTest;
|
|
|
|
|
|
void Awake()
|
|
|
{
|
|
|
@@ -17,51 +21,164 @@ public class SB_EventSystem : MonoBehaviour
|
|
|
AwakenSimulateMouse();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ void Start() {
|
|
|
+ mouseTest = new MouseTest(this);
|
|
|
+ InitListenerForMouseClickHightColor();
|
|
|
+ }
|
|
|
+
|
|
|
+ void Update() {
|
|
|
+ UpdateMoveSimulateMouse();
|
|
|
+ mouseTest.Update();
|
|
|
+ }
|
|
|
|
|
|
[SerializeField] SimulateMouse simulateMouse;
|
|
|
- [System.NonSerialized] public bool simulateMouseIsAwaked;
|
|
|
- public void AwakenSimulateMouse() {
|
|
|
- simulateMouseIsAwaked = !simulateMouse.gameObject.activeSelf;
|
|
|
- simulateMouse.gameObject.SetActive(simulateMouseIsAwaked);
|
|
|
- hasAxisQuat = false;
|
|
|
- }
|
|
|
- Quaternion targetAxisQuat;
|
|
|
- Quaternion nowAxisQuat;
|
|
|
- bool hasAxisQuat;
|
|
|
- Vector2 deltaVectorForMouse;
|
|
|
- public void MoveSimulateMouse(Quaternion axisQuat) {
|
|
|
- if (hasAxisQuat) {
|
|
|
- Vector3 lastAngle = nowAxisQuat.eulerAngles;
|
|
|
- targetAxisQuat = axisQuat;
|
|
|
- nowAxisQuat = Quaternion.Lerp(nowAxisQuat, targetAxisQuat, Time.deltaTime * 15f);
|
|
|
- Vector3 curAngle = nowAxisQuat.eulerAngles;
|
|
|
- float dx = FormatDeltaAngleY(curAngle.y - lastAngle.y) / 72f * simulateMouse.GetScaleScreenWidth();
|
|
|
- float dy = -FormatDeltaAngleX(curAngle.x - lastAngle.x) / 72f * simulateMouse.GetScaleScreenHeight();
|
|
|
- deltaVectorForMouse.x = dx;
|
|
|
- deltaVectorForMouse.y = dy;
|
|
|
- simulateMouse.MoveMousePointer(deltaVectorForMouse);
|
|
|
- } else {
|
|
|
- nowAxisQuat = targetAxisQuat = axisQuat;
|
|
|
- hasAxisQuat = true;
|
|
|
+
|
|
|
+ #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;
|
|
|
+ });
|
|
|
+ }
|
|
|
+ };
|
|
|
}
|
|
|
- }
|
|
|
+ #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, Time.deltaTime * 15f);
|
|
|
+ Vector3 curAngle = nowAxisQuat.eulerAngles;
|
|
|
+ float dx = FormatDeltaAngleY(curAngle.y - lastAngle.y) / 150f * simulateMouse.GetScaleScreenWidth();
|
|
|
+ float dy = -FormatDeltaAngleX(curAngle.x - lastAngle.x) / 150f * 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();
|
|
|
}
|
|
|
- float FormatDeltaAngleX(float value)
|
|
|
- {
|
|
|
- return FormatDeltaAngleY(value);
|
|
|
+
|
|
|
+ //鼠标居中
|
|
|
+ public void MakeMouseToScreenCenter() {
|
|
|
+ simulateMouse.MakeMouseToScreenCenter();
|
|
|
}
|
|
|
- float FormatDeltaAngleY(float value)
|
|
|
- {
|
|
|
- if (Mathf.Abs(value) > 180) {
|
|
|
- if (value < 0) {
|
|
|
- return 360f + value;
|
|
|
- }
|
|
|
- if (value > 0) {
|
|
|
- return value - 360f;
|
|
|
+
|
|
|
+ /** 鼠标测试类 */
|
|
|
+ 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();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- return value;
|
|
|
}
|
|
|
}
|
|
|
+
|