ArrowTraceDebug.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. bool isBtnLDown = false;
  13. bool isBtnRDown = false;
  14. bool isValid = false;
  15. public static ArrowTraceDebug ins;
  16. void Awake() {
  17. ins = this;
  18. }
  19. void OnDestroy() {
  20. if (ins == this) ins = null;
  21. }
  22. void Start()
  23. {
  24. btnTap.onClick.AddListener(() => {
  25. isValid = !isValid;
  26. CheckValidAndUpdateUI();
  27. });
  28. btnL.onPointerDown += (e) => {
  29. isBtnLDown = true;
  30. };
  31. btnL.onPointerUp += (e) => {
  32. isBtnLDown = false;
  33. };
  34. btnR.onPointerDown += (e) => {
  35. isBtnRDown = true;
  36. };
  37. btnR.onPointerUp += (e) => {
  38. isBtnRDown = false;
  39. };
  40. CheckValidAndUpdateUI();
  41. }
  42. void Update() {
  43. if (isValid) {
  44. if (isBtnLDown) {
  45. MoveCamera(Time.deltaTime * -20f);
  46. }
  47. if (isBtnRDown) {
  48. MoveCamera(Time.deltaTime * 20f);
  49. }
  50. }
  51. }
  52. void CheckValidAndUpdateUI() {
  53. btnTap.GetComponentInChildren<Text>().text = isValid ? "退出查看" : "查看轨迹";
  54. btnL.gameObject.SetActive(isValid);
  55. btnR.gameObject.SetActive(isValid);
  56. cameraTF.gameObject.SetActive(isValid);
  57. tracePoints.gameObject.SetActive(isValid);
  58. }
  59. void MoveCamera(float dz) {
  60. Vector3 pos = cameraTF.transform.localPosition;
  61. pos.z += dz;
  62. cameraTF.transform.localPosition = pos;
  63. }
  64. Arrow arrow;
  65. public void OnArrowUpdate(Arrow arrow) {
  66. if (arrow != this.arrow) {
  67. this.arrow = arrow;
  68. ClearTracePoints();
  69. }
  70. GameObject.Instantiate(
  71. this.tracePoints.GetChild(0).gameObject,
  72. arrow.transform.position,
  73. Quaternion.identity,
  74. this.tracePoints
  75. ).SetActive(true);
  76. }
  77. void ClearTracePoints() {
  78. for (int i = 1; i < this.tracePoints.childCount; i++) {
  79. Transform t = this.tracePoints.GetChild(i);
  80. if (t && t.gameObject) Destroy(t.gameObject);
  81. }
  82. }
  83. }