【发布时间】:2021-11-22 07:26:27
【问题描述】:
我为我的游戏创建了一个计时器,我需要将时间保存到变量或函数中,直到完成的那一刻,然后在屏幕上显示为“记录” .
我想在屏幕上显示达到的最短时间,当达到更短的时间时,替换(覆盖)直到那一刻的时间。
到目前为止,我已经设法显示时钟时间和显示“记录”的文本,该文本仅显示 “00:00”,因为我不知道如何将我的代码放在一起。
秒表会在每次新游戏开始时工作并重新启动,但我不知道如何保存游戏结束时获得的时间
如何获得最短的展示时间?
我在C# 的经验很少,我从一个简单的模板开始进行实验和更改。
我已经构建了时钟的代码,下面是一个例子。
另一方面,在查找资料时,我看到使用PlayerPrefs 方法可以保存和删除数据。
我找到了另一个例子并研究了在我的手表上使用该方法的逻辑,但我不太了解逻辑,我无法让它工作。
我怎样才能节省时间在屏幕上显示为文本?
如何让它始终以游戏结束时获得的最短时间重写?
我展示了我的Reloj.cs 的代码,该代码有效并显示在屏幕上。
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;
}
}
}
另一方面,我一直在进行测试以尝试在我的时钟中实现以下示例,但我做不到。在这里提出问题之前,我已经寻找了解决方案并尝试学习如何去做,我看过视频,示例,但我不明白。
以下示例,我通过观看视频创建它,并将其保存在屏幕上,并从按钮中删除它,但我不知道如何在我的时钟中实现它。我想这会很简单,但我无法理解其中的逻辑。
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";
}
}
【问题讨论】:
-
您的值不是持久的,这意味着它们会在应用重新启动时丢失。您需要将它们保存在磁盘上,使用以docs.unity3d.com/ScriptReference/PlayerPrefs.html 开头的 PlayerPrefs。您需要研究序列化和 json 以将数据转换为字符串并使用 Set/GetString。
-
我删除了您的
[unityscript]标记,因为此代码是 C# 代码,而不是 UnityScript 语言(为 Unity 创建的已弃用的 Javascript 衍生语言)。 -
这会回答你的问题Saving/Loading data in Unity 吗?
-
如果我使用过
PlayerPrefs,请忘记代码 sn-p。但正如我所说,我不知道如何将它链接到我的重新登录。我添加了缺少的代码。坦克@fafase -
我展示的第一个代码 sn-p 工作并保存结果。但是我是按照教程完成的,但我不知道如何将它添加到我的 Reloj.cs 中。只会将该站链接到我的代码,但我不知道该怎么做,@derHugo