[英]How to save in Unity the time obtained in the game timer with C #?
I have created a timer for my game and I want to save the lower time in a Variable that is displayed on the screen.我为我的游戏创建了一个计时器,我想将较短的时间保存在屏幕上显示的变量中。 I want to show on the screen the shortest time achieved, when a shorter time is achieved, replace, (overwritten) the time that was up to that moment.我想在屏幕上显示达到的最短时间,当达到更短的时间时,替换(覆盖)到那一刻的时间。 So far I have managed to show the time that is passing and a text to show the "Record"
that only shows "00:00"
since I don't know how to join my code.到目前为止,由于我不知道如何加入我的代码,我已经设法显示正在过去的时间和显示“记录”的文本,该"Record"
仅显示"00:00"
。
The stopwatch works and restarts every time a new game starts, but I cannot save the time achieved How do I get the shortest time achieved to be displayed, removing from the screen the time achieved so far with my code?每次新游戏开始时秒表都会工作并重新启动,但我无法保存所达到的时间 如何获得显示的最短时间,从屏幕上删除我的代码到目前为止所达到的时间?
I have the Clock code, but when I add the function to save the time it breaks the game.我有时钟代码,但是当我添加 function 以节省时间时,它会破坏游戏。 However, creating a clean new project does work.但是,创建一个干净的新项目确实有效。
I show the code If I've used PlayerPrefs, forget that code snippet.我显示代码 如果我使用过 PlayerPrefs,请忘记该代码片段。 but as I say, I don't know how to link it to my relog.但正如我所说,我不知道如何将它链接到我的重新登录。 I add the missing code我添加缺少的代码
Test code to save Record time测试代码以节省记录时间
public class LogicaPuntuaje : MonoBehaviour
{
public Text textoPuntaje;
public int numPuntaje;
public Text textoRecord;
void Start()
{
numPuntaje = 0;
textoRecord.text = PlayerPrefs.GetInt("PuntajeRecord", 0).ToString();
}
void Update()
{
}
// boton para crear puntos al hazar que yo no necesito
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();
}
}
//funcion para borrar los datos del record
public void BorrarDatos()
{
PlayerPrefs.DeleteKey("PuntajeRecord");
textoRecord.text = "00:00";
}
}
}
My stopwatch code, Reloj.cs我的秒表代码 Reloj.cs
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;
}
}
}
// here is the solution:
float currentTime; // your current timer (without logic)
float highscore = 99999; // set it to a really high value by default
void start()
{
highscore = PlayerPrefs.GetFloat("best"); // get your previous highscore
}
//on level completed:
{
if(currentTime <= highscore //your best time
{
highscore = currentTime;
PlayerPrefs.SetFloat("best", highscore); // save your score
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.