ludumdare50/Assets/Scripts/GameTimer.cs
2022-04-03 18:05:13 -04:00

42 lines
839 B
C#

using System;
using TMPro;
using UnityEngine;
public class GameTimer : MonoBehaviour {
TMP_Text label;
float timer;
float highScore;
public bool stopped;
public bool showHighScore;
const string HighScoreKey = "High Score";
void Awake() {
label = GetComponent<TMP_Text>();
timer = 0f;
highScore = PlayerPrefs.GetFloat(HighScoreKey);
stopped = true;
}
void Update() {
if (stopped)
return;
timer += Time.deltaTime;
SaveHighScore();
label.text = TimeSpan.FromSeconds(timer)
.ToString(@"mm\:ss");
if (showHighScore)
label.text += "\nBest Time: " + TimeSpan.FromSeconds(highScore)
.ToString(@"mm\:ss");
}
public void SaveHighScore() {
PlayerPrefs.SetFloat(HighScoreKey, timer);
highScore = timer;
}
public void ResetHighScore() {
PlayerPrefs.DeleteKey(HighScoreKey);
highScore = 0f;
}
}