ArrowTraceDebug.cs 3.4 KB

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