Last active
March 8, 2024 05:13
-
-
Save cadenburleson/182f8d403aaeba81a24f73e1b0349a1b to your computer and use it in GitHub Desktop.
24 hour countdown timer in Unity3D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using TMPro; | |
using System; | |
using Microsoft.Unity.VisualStudio.Editor; | |
using UnityEngine.UI; | |
using UnityEngine.Events; | |
public class CountdownTimer : MonoBehaviour | |
{ | |
private int V = 1; | |
public TextMeshProUGUI timerText; | |
private DateTime startTime; | |
private TimeSpan countdownDuration = TimeSpan.FromSeconds(6); | |
private TimeSpan remainingTime; | |
public UnityEngine.UI.Image radialFill; | |
public UnityEvent doSomethingOnceTimerHitsZero; | |
void Start() | |
{ | |
// Load the saved start time from PlayerPrefs or initialize it if not saved yet | |
if (PlayerPrefs.HasKey("StartTime")) | |
{ | |
long savedTicks = Convert.ToInt64(PlayerPrefs.GetString("StartTime")); | |
startTime = new DateTime(savedTicks); | |
} | |
else | |
{ | |
startTime = DateTime.Now; | |
PlayerPrefs.SetString("StartTime", startTime.Ticks.ToString()); | |
PlayerPrefs.Save(); | |
} | |
// Calculate remaining time based on saved start time | |
TimeSpan elapsedTime = DateTime.Now - startTime; | |
remainingTime = countdownDuration - elapsedTime; | |
// If remaining time is negative or greater than 24 hours, reset the timer | |
// if (remainingTime.TotalHours <= 0 || remainingTime > countdownDuration) | |
// { | |
// remainingTime = countdownDuration; | |
// startTime = DateTime.Now; | |
// PlayerPrefs.SetString("StartTime", startTime.Ticks.ToString()); | |
// PlayerPrefs.Save(); | |
// } | |
// Start the visual countdown | |
UpdateVisualTimer(); | |
} | |
void UpdateVisualTimer() | |
{ | |
// Update the TMP text to display the remaining time | |
timerText.text = string.Format("{0:D2}:{1:D2}:{2:D2}:{3:D3}", (int)remainingTime.TotalHours, remainingTime.Minutes, remainingTime.Seconds, remainingTime.Milliseconds); | |
// radialFill.fillAmount = remainingTime.Seconds; | |
float fillAmount = (float)remainingTime.TotalSeconds / (float)countdownDuration.TotalSeconds; | |
radialFill.fillAmount = fillAmount; | |
} | |
void Update() | |
{ | |
// if remaining time is negative or greater than countDownDuration, reset the timer | |
if (remainingTime.TotalSeconds <= 0 || remainingTime > countdownDuration) | |
{ | |
remainingTime = countdownDuration; | |
startTime = DateTime.Now; | |
PlayerPrefs.SetString("StartTime", startTime.Ticks.ToString()); | |
PlayerPrefs.Save(); | |
} | |
// Update remainingTime every frame | |
remainingTime = countdownDuration - (DateTime.Now - startTime); | |
UpdateVisualTimer(); | |
// Do some Action when the timer hits zero. | |
if (remainingTime.TotalSeconds <= 0) | |
{ | |
doSomethingOnceTimerHitsZero.Invoke(); | |
} | |
} | |
public void Ass() | |
{ | |
V = V + 1; | |
Debug.Log("~~~~~~~~~~~~~~~~~~~~~~ TIMER HAS HIT ZERO " + "~~~~~~~~~~~~~~~~~~~~~~" + V); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment