Unity中如何保存游戏计时器耗时并展示最短通关记录
实现方案
核心逻辑说明
- 最短耗时记录和分数记录逻辑相反:分数越高越好,所以新分数>旧记录才更新;耗时越短越好,所以新耗时<旧记录才更新
- 用
PlayerPrefs以int类型存储总秒数,读取后再转成「00:00」格式显示 - 游戏结束时触发记录对比和保存逻辑
修改后的代码
1. 调整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; } } // 新增:获取当前已计时的总秒数(取整) public int ObtenerTiempoActualEnSegundos() { return (int)tiempoAMostrarEnSegundos; } }
2. 调整LogicaPuntuaje.cs 适配计时记录逻辑
替换原有随机分逻辑,改为适配计时记录的对比、保存、格式转换逻辑:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class LogicaPuntuaje : MonoBehaviour { // Inspector面板拖入挂载了Reloj脚本的游戏物体 public Reloj reloj; // Inspector面板拖入专门显示Record的Text组件 public Text textoRecord; void Start() { // 初始加载已保存的记录,默认值给9999代表还没有有效记录 int recordGuardado = PlayerPrefs.GetInt("RecordTiempo", 9999); // 转成00:00格式显示 textoRecord.text = ConvertirSegundosAFormatoReloj(recordGuardado); } // 游戏结束时调用这个方法即可触发记录校验 public void GuardarRecordAlTerminarJuego() { // 获取本次游戏的耗时 int tiempoActual = reloj.ObtenerTiempoActualEnSegundos(); // 读取已保存的最短记录 int recordAnterior = PlayerPrefs.GetInt("RecordTiempo", 9999); // 如果本次耗时比之前的记录更短,就更新存储的记录 if (tiempoActual < recordAnterior) { PlayerPrefs.SetInt("RecordTiempo", tiempoActual); textoRecord.text = ConvertirSegundosAFormatoReloj(tiempoActual); } } // 工具方法:把总秒数转成和计时器一致的00:00格式 private string ConvertirSegundosAFormatoReloj(int totalSegundos) { // 没有有效记录的情况显示00:00 if (totalSegundos >= 9999) { return "00:00"; } int minutos = totalSegundos / 60; int segundos = totalSegundos % 60; return minutos.ToString("00") + ":" + segundos.ToString("00"); } // 删除记录的方法适配新的存储key,功能不变 public void BorrarDatos() { PlayerPrefs.DeleteKey("RecordTiempo"); textoRecord.text = "00:00"; } }
使用步骤
- 用上面的代码替换你原有的两个脚本
- 在Inspector面板给
LogicaPuntuaje组件完成参数赋值 - 在你的游戏结束逻辑里(比如玩家到达终点、生命值归零的触发代码中),调用
FindObjectOfType<LogicaPuntuaje>().GuardarRecordAlTerminarJuego()即可;如果是按钮触发游戏结束,也可以直接把这个方法绑到按钮的OnClick事件上 - 删除记录的方法可以直接绑定到你原来的删除按钮上,功能正常可用
内容的提问来源于stack exchange,提问作者Estudiante
相关产品推荐
相关产品推荐

