using UnityEngine;
using UnityEngine.UI;
public class LevelManager : MonoBehaviour
{
public Text[] timeTexts; // 存储显示时间的文本框
private float[] bestTimes; // 存储最短通关时间的数组
void Start()
{
// 初始化最短通关时间数组
bestTimes = new float[timeTexts.Length];
// 从PlayerPrefs中读取最短通关时间
for (int i = 0; i < timeTexts.Length; i++)
{
bestTimes[i] = PlayerPrefs.GetFloat("BestTime" + i.ToString(), 0f);
timeTexts[i].text = FormatTime(bestTimes[i]); // 显示最短通关时间
}
}
// 更新最短通关时间
public void UpdateBestTime(int levelIndex, float time)
{
if (time < bestTimes[levelIndex] || bestTimes[levelIndex] == 0f)
{
bestTimes[levelIndex] = time;
PlayerPrefs.SetFloat("BestTime" + levelIndex.ToString(), time);
timeTexts[levelIndex].text = FormatTime(time); // 更新显示的最短通关时间
}
}
// 格式化时间为分:秒.毫秒的形式
private string FormatTime(float time)
{
int minutes = (int)(time / 60f);
int seconds = (int)(time % 60f);
int milliseconds = (int)((time * 1000f) % 1000f);
return string.Format("{0:00}:{1:00}.{2:000}", minutes, seconds, milliseconds);
}
}
//关卡对应索引
for (int i = 0; i < LevelGame.Count; i++)
{
if (LevelGame[i].activeSelf)
{
TimeAndScoreRecorder.Instance.Index = i;
}
}
UpdateBestTime方法在通关时调用,传入关卡的下标和完成的时间