using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ArrowTraceDebug : MonoBehaviour { [SerializeField] Transform cameraTF; [SerializeField] Transform tracePoints; [SerializeField] Button btnTap; [SerializeField] ButtonListener btnL; [SerializeField] ButtonListener btnR; [SerializeField] ButtonListener btnFar; [SerializeField] ButtonListener btnNear; bool isBtnLDown = false; bool isBtnRDown = false; bool isValid = false; int crossHairIsOpen = -1; public static ArrowTraceDebug ins; void Awake() { ins = this; } void OnDestroy() { if (ins == this) ins = null; SaveCameraData(); } void Start() { btnTap.onClick.AddListener(() => { isValid = !isValid; if (isValid) { GameMgr.ins.gameMode.PauseTimeCounting(this); } else { GameMgr.ins.gameMode.ResumeTimeCounting(this); } CheckValidAndUpdateUI(); //自动开关准心 if (isValid && CrossHair.ins) { crossHairIsOpen = CrossHair.ins.GetOpen() ? 1 : 0; CrossHair.ins.SetOpen(false); } else if (!isValid && CrossHair.ins && crossHairIsOpen > -1) { CrossHair.ins.SetOpen(crossHairIsOpen == 1 ? true : false); } }); btnL.onPointerDown += (e) => { isBtnLDown = true; }; btnL.onPointerUp += (e) => { isBtnLDown = false; }; btnR.onPointerDown += (e) => { isBtnRDown = true; }; btnR.onPointerUp += (e) => { isBtnRDown = false; }; btnFar.onPointerDown += (e) => { Camera camera = cameraTF.GetComponent(); camera.orthographicSize += 3; }; btnNear.onPointerDown += (e) => { Camera camera = cameraTF.GetComponent(); if (camera.orthographicSize >= 3) { camera.orthographicSize -= 3; } }; CheckValidAndUpdateUI(); ResumeCameraData(); } void Update() { if (isValid) { if (isBtnLDown) { MoveCamera(Time.deltaTime * -20f); } if (isBtnRDown) { MoveCamera(Time.deltaTime * 20f); } } } void CheckValidAndUpdateUI() { btnTap.GetComponentInChildren().text = isValid ? "退出查看" : "查看轨迹"; btnL.gameObject.SetActive(isValid); btnR.gameObject.SetActive(isValid); btnFar.gameObject.SetActive(isValid); btnNear.gameObject.SetActive(isValid); cameraTF.gameObject.SetActive(isValid); tracePoints.gameObject.SetActive(isValid); transform.Find("Canvas/Mask").gameObject.SetActive(isValid); } void MoveCamera(float dz) { Vector3 pos = cameraTF.transform.localPosition; pos.z += dz; cameraTF.transform.localPosition = pos; } Arrow arrow; public void OnArrowUpdate(Arrow arrow) { if (arrow != this.arrow) { this.arrow = arrow; ClearTracePoints(); } GameObject.Instantiate( this.tracePoints.GetChild(0).gameObject, arrow.transform.position, Quaternion.identity, this.tracePoints ).SetActive(true); } void ClearTracePoints() { for (int i = 1; i < this.tracePoints.childCount; i++) { Transform t = this.tracePoints.GetChild(i); if (t && t.gameObject) Destroy(t.gameObject); } } static bool hasSaveCameraData = false; static Vector3 cameraSavePosition; static float cameraSaveOrthographicSize; void SaveCameraData() { cameraSavePosition = cameraTF.localPosition; cameraSaveOrthographicSize = cameraTF.GetComponent().orthographicSize; hasSaveCameraData = true; } void ResumeCameraData() { if (hasSaveCameraData) { hasSaveCameraData = false; cameraTF.localPosition = cameraSavePosition; cameraTF.GetComponent().orthographicSize = cameraSaveOrthographicSize; } } }