1+ using System . Collections ;
2+ using UnityEngine ;
3+ using UnityEngine . UI ;
4+ using TMPro ;
5+
6+ public class StandaloneCountdown : MonoBehaviour
7+ {
8+ [ Header ( "Countdown Settings" ) ]
9+ [ Tooltip ( "Values to show during the countdown in order" ) ]
10+ public string [ ] countdownSteps = { "3" , "2" , "1" , "GO!" } ;
11+
12+ [ Tooltip ( "Time (in seconds) to wait before the first value is shown" ) ]
13+ public float initialDelay = 0.5f ;
14+
15+ [ Tooltip ( "Duration each countdown value remains on screen (in seconds)" ) ]
16+ public float stepDuration = 1f ;
17+
18+ [ Tooltip ( "Automatically start the countdown on Start()" ) ]
19+ public bool startAutomatically = true ;
20+
21+ [ Header ( "UI References" ) ]
22+ [ Tooltip ( "TextMeshPro component to display the countdown" ) ]
23+ public TextMeshProUGUI countdownTextTMP ;
24+
25+ [ Tooltip ( "Legacy Text component to display the countdown" ) ]
26+ public Text countdownText ;
27+
28+ [ Tooltip ( "Hide the countdown text after the countdown completes" ) ]
29+ public bool hideTextOnFinish = true ;
30+
31+ private Coroutine countdownRoutine ;
32+
33+ void Start ( )
34+ {
35+ if ( startAutomatically )
36+ {
37+ StartCountdown ( ) ;
38+ }
39+ }
40+
41+ public void StartCountdown ( )
42+ {
43+ if ( countdownRoutine != null )
44+ {
45+ StopCoroutine ( countdownRoutine ) ;
46+ }
47+
48+ countdownRoutine = StartCoroutine ( CountdownSequence ( ) ) ;
49+ }
50+
51+ private IEnumerator CountdownSequence ( )
52+ {
53+ // 1. Pause the game
54+ Time . timeScale = 0f ;
55+
56+ SetCountdownVisibility ( true ) ;
57+
58+ // Wait for the initial delay
59+ if ( initialDelay > 0f )
60+ {
61+ yield return new WaitForSecondsRealtime ( initialDelay ) ;
62+ }
63+
64+ // --- Run the countdown steps ---
65+ if ( countdownSteps == null || countdownSteps . Length == 0 )
66+ {
67+ SetCountdownText ( "GO!" ) ;
68+ yield return new WaitForSecondsRealtime ( stepDuration ) ;
69+ }
70+ else
71+ {
72+ foreach ( string step in countdownSteps )
73+ {
74+ SetCountdownText ( step ) ;
75+ // Wait for the step duration (using Realtime)
76+ yield return new WaitForSecondsRealtime ( Mathf . Max ( 0f , stepDuration ) ) ;
77+ }
78+ }
79+
80+ // --- Countdown Finished ---
81+
82+ if ( hideTextOnFinish )
83+ {
84+ SetCountdownVisibility ( false ) ;
85+ }
86+
87+ // 2. Unpause the game
88+ Time . timeScale = 1f ;
89+
90+ countdownRoutine = null ;
91+ }
92+
93+ // --- Helper methods to show/hide the text ---
94+
95+ private void SetCountdownText ( string text )
96+ {
97+ if ( countdownTextTMP != null )
98+ {
99+ countdownTextTMP . text = text ;
100+ }
101+
102+ if ( countdownText != null )
103+ {
104+ countdownText . text = text ;
105+ }
106+ }
107+
108+ private void SetCountdownVisibility ( bool visible )
109+ {
110+ if ( countdownTextTMP != null )
111+ {
112+ countdownTextTMP . gameObject . SetActive ( visible ) ;
113+ }
114+
115+ if ( countdownText != null )
116+ {
117+ countdownText . gameObject . SetActive ( visible ) ;
118+ }
119+ }
120+ }
0 commit comments