The Countdown Timer script for Unity allows you to create a countdown timer that displays the remaining days, hours, and minutes. The timer's state is saved to a file so that it persists across application sessions.
- Customizable initial countdown time (days, hours, minutes).
- Displays the remaining time in a
TextMeshProUGUIelement. - Saves and loads the countdown state to/from a file.
- Handles application pause, quit, and focus events to ensure accurate time tracking.
- Attach the
Countdownscript to a GameObject in your Unity scene.
- Create a
TextMeshProUGUIelement in your scene to display the countdown timer. - Assign this
TextMeshProUGUIelement to thecountdownTextfield in the script.
- In the Inspector, set the
daysLeft,hoursLeft, andminutesLeftfields to define the initial countdown duration.
- When you run the scene, the countdown will start from the specified time.
- The remaining time will be displayed in the format:
Time Left: 00d 00h 00m. - When the countdown reaches zero, it will display
Time's Up!.
- The countdown state is saved to a file located at
Application.persistentDataPath + "/countdown.txt". - The timer state is saved when the application is paused, loses focus, or quits.
- On restarting the application, the countdown will resume from the last saved state.
- Ensure that the TextMeshPro package is installed in your Unity project.
- Customize the display format or behavior as needed by modifying the script.
Here’s a basic setup example:
- Create a new GameObject in the scene and name it
CountdownTimer. - Attach the
Countdownscript to theCountdownTimerGameObject. - Create a UI Text element using TextMeshPro and assign it to the
countdownTextfield of the script. - Set the initial time values (e.g.,
daysLeft = 1,hoursLeft = 2,minutesLeft = 30).
This project is licensed under the Creative Commons Attribution 4.0 International Public License. You are free to use, distribute, remix, adapt, and build upon this work, even commercially, as long as you credit the original creator.
This script was developed by Daniel Chakarov/Sleeping Impulse.