ArrowTraceDebug.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class ArrowTraceDebug : MonoBehaviour
  6. {
  7. [SerializeField] Transform cameraTF;
  8. [SerializeField] Transform tracePoints;
  9. [SerializeField] Button btnTap;
  10. [SerializeField] ButtonListener btnL;
  11. [SerializeField] ButtonListener btnR;
  12. [SerializeField] ButtonListener btnFar;
  13. [SerializeField] ButtonListener btnNear;
  14. bool isBtnLDown = false;
  15. bool isBtnRDown = false;
  16. bool isValid = false;
  17. int crossHairIsOpen = -1;
  18. public static ArrowTraceDebug ins;
  19. void Awake() {
  20. ins = this;
  21. }
  22. void OnDestroy() {
  23. if (ins == this) ins = null;
  24. SaveCameraData();
  25. }
  26. void Start()
  27. {
  28. btnTap.onClick.AddListener(() => {
  29. isValid = !isValid;
  30. if (isValid) {
  31. GameMgr.ins.gameMode.PauseTimeCounting(this);
  32. } else {
  33. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  34. }
  35. CheckValidAndUpdateUI();
  36. //自动开关准心
  37. if (isValid && CrossHair.ins) {
  38. crossHairIsOpen = CrossHair.ins.GetOpen() ? 1 : 0;
  39. CrossHair.ins.SetOpen(false);
  40. } else if (!isValid && CrossHair.ins && crossHairIsOpen > -1) {
  41. CrossHair.ins.SetOpen(crossHairIsOpen == 1 ? true : false);
  42. }
  43. });
  44. btnL.onPointerDown += (e) => {
  45. isBtnLDown = true;
  46. };
  47. btnL.onPointerUp += (e) => {
  48. isBtnLDown = false;
  49. };
  50. btnR.onPointerDown += (e) => {
  51. isBtnRDown = true;
  52. };
  53. btnR.onPointerUp += (e) => {
  54. isBtnRDown = false;
  55. };
  56. btnFar.onPointerDown += (e) => {
  57. Camera camera = cameraTF.GetComponent<Camera>();
  58. camera.orthographicSize += 3;
  59. };
  60. btnNear.onPointerDown += (e) => {
  61. Camera camera = cameraTF.GetComponent<Camera>();
  62. if (camera.orthographicSize >= 3) {
  63. camera.orthographicSize -= 3;
  64. }
  65. };
  66. CheckValidAndUpdateUI();
  67. ResumeCameraData();
  68. }
  69. void Update() {
  70. if (isValid) {
  71. if (isBtnLDown) {
  72. MoveCamera(Time.deltaTime * -20f);
  73. }
  74. if (isBtnRDown) {
  75. MoveCamera(Time.deltaTime * 20f);
  76. }
  77. }
  78. }
  79. void CheckValidAndUpdateUI() {
  80. btnTap.GetComponentInChildren<Text>().text = isValid ? "退出查看" : "查看轨迹";
  81. btnL.gameObject.SetActive(isValid);
  82. btnR.gameObject.SetActive(isValid);
  83. btnFar.gameObject.SetActive(isValid);
  84. btnNear.gameObject.SetActive(isValid);
  85. cameraTF.gameObject.SetActive(isValid);
  86. tracePoints.gameObject.SetActive(isValid);
  87. transform.Find("Canvas/Mask").gameObject.SetActive(isValid);
  88. }
  89. void MoveCamera(float dz) {
  90. Vector3 pos = cameraTF.transform.localPosition;
  91. pos.z += dz;
  92. cameraTF.transform.localPosition = pos;
  93. }
  94. Arrow arrow;
  95. public void OnArrowUpdate(Arrow arrow) {
  96. if (arrow != this.arrow) {
  97. this.arrow = arrow;
  98. ClearTracePoints();
  99. }
  100. GameObject.Instantiate(
  101. this.tracePoints.GetChild(0).gameObject,
  102. arrow.transform.position,
  103. Quaternion.identity,
  104. this.tracePoints
  105. ).SetActive(true);
  106. }
  107. void ClearTracePoints() {
  108. for (int i = 1; i < this.tracePoints.childCount; i++) {
  109. Transform t = this.tracePoints.GetChild(i);
  110. if (t && t.gameObject) Destroy(t.gameObject);
  111. }
  112. }
  113. static bool hasSaveCameraData = false;
  114. static Vector3 cameraSavePosition;
  115. static float cameraSaveOrthographicSize;
  116. void SaveCameraData() {
  117. cameraSavePosition = cameraTF.localPosition;
  118. cameraSaveOrthographicSize = cameraTF.GetComponent<Camera>().orthographicSize;
  119. hasSaveCameraData = true;
  120. }
  121. void ResumeCameraData() {
  122. if (hasSaveCameraData) {
  123. hasSaveCameraData = false;
  124. cameraTF.localPosition = cameraSavePosition;
  125. cameraTF.GetComponent<Camera>().orthographicSize = cameraSaveOrthographicSize;
  126. }
  127. }
  128. }