SB_EventSystem.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using JC.Unity;
  5. public class SB_EventSystem : MonoBehaviour
  6. {
  7. public static SB_EventSystem ins;
  8. void Awake()
  9. {
  10. if (ins) {
  11. Destroy(this.gameObject);
  12. } else {
  13. ins = this;
  14. DontDestroyOnLoad(this.gameObject);
  15. AwakenSimulateMouse();
  16. }
  17. }
  18. [SerializeField] SimulateMouse simulateMouse;
  19. [System.NonSerialized] public bool simulateMouseIsAwaked;
  20. public void AwakenSimulateMouse() {
  21. simulateMouseIsAwaked = !simulateMouse.gameObject.activeSelf;
  22. simulateMouse.gameObject.SetActive(simulateMouseIsAwaked);
  23. hasAxisQuat = false;
  24. }
  25. Quaternion targetAxisQuat;
  26. Quaternion nowAxisQuat;
  27. bool hasAxisQuat;
  28. Vector2 deltaVectorForMouse;
  29. public void MoveSimulateMouse(Quaternion axisQuat) {
  30. if (hasAxisQuat) {
  31. Vector3 lastAngle = nowAxisQuat.eulerAngles;
  32. targetAxisQuat = axisQuat;
  33. nowAxisQuat = Quaternion.Lerp(nowAxisQuat, targetAxisQuat, Time.deltaTime * 15f);
  34. Vector3 curAngle = nowAxisQuat.eulerAngles;
  35. float dx = FormatDeltaAngleY(curAngle.y - lastAngle.y) / 72f * simulateMouse.GetScaleScreenWidth();
  36. float dy = -FormatDeltaAngleX(curAngle.x - lastAngle.x) / 72f * simulateMouse.GetScaleScreenHeight();
  37. deltaVectorForMouse.x = dx;
  38. deltaVectorForMouse.y = dy;
  39. simulateMouse.MoveMousePointer(deltaVectorForMouse);
  40. } else {
  41. nowAxisQuat = targetAxisQuat = axisQuat;
  42. hasAxisQuat = true;
  43. }
  44. }
  45. public void ClickMouse() {
  46. simulateMouse.ClickMousePointer();
  47. }
  48. float FormatDeltaAngleX(float value)
  49. {
  50. return FormatDeltaAngleY(value);
  51. }
  52. float FormatDeltaAngleY(float value)
  53. {
  54. if (Mathf.Abs(value) > 180) {
  55. if (value < 0) {
  56. return 360f + value;
  57. }
  58. if (value > 0) {
  59. return value - 360f;
  60. }
  61. }
  62. return value;
  63. }
  64. }