繁体   English   中英

如何用 C # 在 Unity 中保存游戏计时器中获得的时间?

[英]How to save in Unity the time obtained in the game timer with C #?

我为我的游戏创建了一个计时器,我想将较短的时间保存在屏幕上显示的变量中。 我想在屏幕上显示达到的最短时间,当达到更短的时间时,替换(覆盖)到那一刻的时间。 到目前为止,由于我不知道如何加入我的代码,我已经设法显示正在过去的时间和显示“记录”的文本,该"Record"仅显示"00:00"

每次新游戏开始时秒表都会工作并重新启动,但我无法保存所达到的时间 如何获得显示的最短时间,从屏幕上删除我的代码到目前为止所达到的时间?

我有时钟代码,但是当我添加 function 以节省时间时,它会破坏游戏。 但是,创建一个干净的新项目确实有效。

我显示代码 如果我使用过 PlayerPrefs,请忘记该代码片段。 但正如我所说,我不知道如何将它链接到我的重新登录。 我添加缺少的代码

测试代码以节省记录时间

 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";
  }
}

    }

我的秒表代码 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM