I have created a timer for my game and I need to save in a variable or a function, the time elapsed until the moment of finishing, and then show it on the screen as "Record".
I want to display on screen the shortest time achieved, when a shorter time is achieved, replace, (overwrite) the time that was until that moment.
So far I have managed to show the clock time and a text to show "Record" that only shows "00:00" since I don't know how to put my code together.
The stopwatch works and restarts every time a new game starts, but I don't know how to save the time achieved at the end of the game
How do I get the shortest time achieved to display?
My experience is sparse in C#
, I have started with a simple template to experiment and make changes.
I have built the code of the clock, following an example.
On the other hand, looking for information, I see that with the PlayerPrefs
method you can save and delete data.
I found another example and studied the logic to use that method on my watch, but I don't quite understand the logic and I can't get it to work.
How can I get to save the time to display it as text on the screen?
How to make it always rewrite with the shortest time obtained at the end of the game?
I show the code of my Reloj.cs
which works and is displayed on the screen.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Reloj : MonoBehaviour
{
[Tooltip("Tiempo inicial")]
public int tiempoInicial;
[Tooltip("Escala de tiempo del relog")]
[Range(-10.0f, 10.0f)]
public float escalaDeTiempo = 1;
private Text myText;
private float tiempoDelFrameConTimeScale = 0f;
private float tiempoAMostrarEnSegundos = 0f;
private float escalaDeTiempoAlPausar, escalaDeTiempoInicial;
private bool estaPausado = false;
void Start()
{
// set the timeline
escalaDeTiempoInicial = escalaDeTiempo;
myText = GetComponent<Text>();
//we start the variable
tiempoAMostrarEnSegundos = tiempoInicial;
ActualizarRelog(tiempoInicial);
}
// Update is called once per frame
void Update()
{
if (!estaPausado)
{
// the following represents the time of each frame considered the time scale
tiempoDelFrameConTimeScale = Time.deltaTime * escalaDeTiempo;
// the following variable accumulates the elapsed time to show it in the Relog
tiempoAMostrarEnSegundos += tiempoDelFrameConTimeScale;
ActualizarRelog(tiempoAMostrarEnSegundos);
}
}
public void ActualizarRelog(float tiempoEnSegundos)
{
int minutos = 0;
int segundos = 0;
string textoDelReloj;
// ensure that the time is not negative
if (tiempoEnSegundos <= 0) tiempoEnSegundos = 0;
// calculate seconds and minutes
minutos = (int)tiempoEnSegundos / 60;
tiempoEnSegundos = (int)tiempoEnSegundos % 60;
// create the string of digital characters that form the relog
textoDelReloj = minutos.ToString("00") + ':' + tiempoEnSegundos.ToString("00");
// update UI text element with character string
myText.text = textoDelReloj;
}
public void Pausar()
{
if (!estaPausado)
{
estaPausado = true;
escalaDeTiempoAlPausar = escalaDeTiempo;
escalaDeTiempo = 0;
}
}
}
On the other hand, I have been doing tests to try to implement the following example in my Clock, but I can't. Before asking the question here, I have looked for a solution and I have tried to learn how to do it, I have seen videos, examples, and I do not get it.
The following example, I create it by watching a video, and I get it to save the data on the screen, and delete it from a button, but I don't know how to implement it in my Clock. I imagine it would be very simple, but I can't understand the logic.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LogicaPuntuaje : MonoBehaviour
{
public Text textoPuntaje;
public int numPuntaje;
// The start is called before the first table update
public Text textoRecord;
void Start()
{
numPuntaje = 0;
textoRecord.text = PlayerPrefs.GetInt("PuntajeRecord", 0).ToString();
}
// The update is called once per frame
void Update()
{
}
// button to create points by doing that I don't need
public void PuntajeAlAzaro()
{
numPuntaje = Random.Range(0, 11);
textoPuntaje.text = numPuntaje.ToString();
if (numPuntaje > PlayerPrefs.GetInt("PuntajeRecord", 0))
{
PlayerPrefs.SetInt("PuntajeRecord", numPuntaje);
textoRecord.text = numPuntaje.ToString();
}
}
// function to delete the record data
public void BorrarDatos()
{
PlayerPrefs.DeleteKey("PuntajeRecord");
textoRecord.text = "00:00";
}
}