ArrowTraceDebug.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. if (CommonConfig.isReleaseVersion) {
  29. GameObject.Destroy(gameObject);
  30. return;
  31. }
  32. btnTap.onClick.AddListener(() => {
  33. isValid = !isValid;
  34. if (isValid) {
  35. GameMgr.ins.gameMode.PauseTimeCounting(this);
  36. } else {
  37. GameMgr.ins.gameMode.ResumeTimeCounting(this);
  38. }
  39. CheckValidAndUpdateUI();
  40. //自动开关准心
  41. if (isValid && CrossHair.ins) {
  42. crossHairIsOpen = CrossHair.ins.GetOpen() ? 1 : 0;
  43. CrossHair.ins.SetOpen(false);
  44. } else if (!isValid && CrossHair.ins && crossHairIsOpen > -1) {
  45. CrossHair.ins.SetOpen(crossHairIsOpen == 1 ? true : false);
  46. }
  47. });
  48. btnL.onPointerDown += (e) => {
  49. isBtnLDown = true;
  50. };
  51. btnL.onPointerUp += (e) => {
  52. isBtnLDown = false;
  53. };
  54. btnR.onPointerDown += (e) => {
  55. isBtnRDown = true;
  56. };
  57. btnR.onPointerUp += (e) => {
  58. isBtnRDown = false;
  59. };
  60. btnFar.onPointerDown += (e) => {
  61. Camera camera = cameraTF.GetComponent<Camera>();
  62. camera.orthographicSize += 3;
  63. };
  64. btnNear.onPointerDown += (e) => {
  65. Camera camera = cameraTF.GetComponent<Camera>();
  66. if (camera.orthographicSize >= 3) {
  67. camera.orthographicSize -= 3;
  68. }
  69. };
  70. CheckValidAndUpdateUI();
  71. ResumeCameraData();
  72. }
  73. void Update() {
  74. if (isValid) {
  75. if (isBtnLDown) {
  76. MoveCamera(Time.deltaTime * -20f);
  77. }
  78. if (isBtnRDown) {
  79. MoveCamera(Time.deltaTime * 20f);
  80. }
  81. }
  82. }
  83. void CheckValidAndUpdateUI() {
  84. btnTap.GetComponentInChildren<Text>().text = isValid ? "退出查看" : "查看轨迹";
  85. btnL.gameObject.SetActive(isValid);
  86. btnR.gameObject.SetActive(isValid);
  87. btnFar.gameObject.SetActive(isValid);
  88. btnNear.gameObject.SetActive(isValid);
  89. cameraTF.gameObject.SetActive(isValid);
  90. tracePoints.gameObject.SetActive(isValid);
  91. transform.Find("Canvas/Mask").gameObject.SetActive(isValid);
  92. }
  93. void MoveCamera(float dz) {
  94. Vector3 pos = cameraTF.transform.localPosition;
  95. pos.z += dz;
  96. cameraTF.transform.localPosition = pos;
  97. }
  98. Arrow arrow;
  99. public void OnArrowUpdate(Arrow arrow) {
  100. if (arrow != this.arrow) {
  101. this.arrow = arrow;
  102. ClearTracePoints();
  103. }
  104. GameObject.Instantiate(
  105. this.tracePoints.GetChild(0).gameObject,
  106. arrow.transform.position,
  107. Quaternion.identity,
  108. this.tracePoints
  109. ).SetActive(true);
  110. }
  111. void ClearTracePoints() {
  112. for (int i = 1; i < this.tracePoints.childCount; i++) {
  113. Transform t = this.tracePoints.GetChild(i);
  114. if (t && t.gameObject) Destroy(t.gameObject);
  115. }
  116. }
  117. static bool hasSaveCameraData = false;
  118. static Vector3 cameraSavePosition;
  119. static float cameraSaveOrthographicSize;
  120. void SaveCameraData() {
  121. cameraSavePosition = cameraTF.localPosition;
  122. cameraSaveOrthographicSize = cameraTF.GetComponent<Camera>().orthographicSize;
  123. hasSaveCameraData = true;
  124. }
  125. void ResumeCameraData() {
  126. if (hasSaveCameraData) {
  127. hasSaveCameraData = false;
  128. cameraTF.localPosition = cameraSavePosition;
  129. cameraTF.GetComponent<Camera>().orthographicSize = cameraSaveOrthographicSize;
  130. }
  131. }
  132. }