1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- using System;
- using TMPro;
- using UnityEngine;
- using Debug = System.Diagnostics.Debug;
- namespace Display
- {
- public class CountdownDisplay : MonoBehaviour
- {
- public int countdown = 3;
- public TextMeshProUGUI countdownText;
- public float distance;
-
- private Camera cam;
- private Transform camTransform;
- private Transform canvasTransform;
- public delegate string CountdownDoneCallback();
- public CountdownDoneCallback OnCountdownDone { get; set; }
- private float lastTime;
- private void Start()
- {
- cam = Camera.main;
- Debug.Assert(cam != null, nameof(cam) + " != null");
- camTransform = cam.transform;
- lastTime = Time.time;
- canvasTransform = GetComponentInChildren<Canvas>().transform;
- }
- private void Update()
- {
- var t = Time.time;
- if (t - lastTime >= 1)
- {
- lastTime = t;
- countdown--;
- countdownText.text = $"{countdown}";
- if (countdown == 0)
- {
- countdownText.text = OnCountdownDone?.Invoke();
- Destroy(gameObject, 0.5f);
- }
- }
-
- canvasTransform.SetPositionAndRotation(camTransform.position + camTransform.forward * distance,
- camTransform.rotation);
- }
- }
- }
|