簡體   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