c# - unity显示最佳时间
问题描述
我是 Unity 的初学者,正在寻求帮助。我制作了一个简单的游戏,您可以在其中尝试尽可能快地完成几个关卡。我有一个计时器脚本,我想用它来将最佳时间保存在主菜单的表格中。我已经用 playerprefs 查看了一些解决方案,但我仍然有点迷茫,如果有人能指导我完成这个,我将非常感激。
定时器脚本:
public class Timercontroller : MonoBehaviour
{
public Text timeCounter;
public TimeSpan timePlaying;
public bool timerGoing;
public float elapsedTime;
public void Awake()
{
DontDestroyOnLoad(this.gameObject);
}
void Start()
{
timeCounter.text = "Time: 00:00.00";
timerGoing = false;
BeginTimer();
}
public void BeginTimer()
{
timerGoing = true;
elapsedTime = 0f;
StartCoroutine(UpdateTimer());
}
public void EndTimer()
{
timerGoing = false;
}
public IEnumerator UpdateTimer()
{
while (timerGoing)
{
elapsedTime += Time.deltaTime;
timePlaying = TimeSpan.FromSeconds(elapsedTime);
string timePlayingString = "Time: " + timePlaying.ToString("mm':'ss'.'ff");
timeCounter.text = timePlayingString;
yield return null;
}
}
void Update()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
if (currentSceneIndex == 5) {
EndTimer();
}
}
}
解决方案
您只是在寻找有关保存计时器的建议吗?因此,假设您的代码是正确的并且您只是想保存/加载时间,我会执行以下操作:
List<float> bestTimes = new List<float>();
int totalScores = 5; // the total times you want to save
/// <summary>
/// Call this in Awake to load in your data.
/// It checks to see if you have any saved times and adds them to the list if you do.
/// </summary>
public void LoadTimes() {
for (int i = 0; i < totalScores; i++) {
string key = "time" + i;
if (PlayerPrefs.HasKey(key)) {
bestTimes.Add(PlayerPrefs.GetFloat(key));
}
}
}
/// <summary>
/// Call this from your EndTimer method.
/// This will check and see if the time just created is a "best time".
/// </summary>
public void CheckTime(float time) {
// if there are not enough scores in the list, go ahead and add it
if (bestTimes.Count < totalScores) {
bestTimes.Add(time);
// make sure the times are in order from highest to lowest
bestTimes.Sort((a, b) => b.CompareTo(a));
SaveTimes();
} else {
for (int i = 0; i < bestTimes.Count; i++) {
// if the time is smaller, insert it
if (time < bestTimes[i]) {
bestTimes.Insert(i, time);
// remove the last item in the list
bestTimes.RemoveAt(bestTimes.Count - 1);
SaveTimes();
break;
}
}
}
}
/// <summary>
/// This is called from CheckTime().
/// It saves the times to PlayerPrefs.
/// </summary>
public void SaveTimes() {
for (int i = 0; i < bestTimes.Count; i++) {
string key = "time" + i;
PlayerPrefs.SetFloat(key, bestTimes[i]);
}
}
我还没有机会对此进行测试,但它似乎对我有用。
要更新高分列表上的文本,假设它们都是文本对象,我将创建一个新脚本,将其添加到每个 Text 对象,并将以下内容添加到代码中:
private void Awake(){
int index = transform.SetSiblingIndex();
Text theText = GetComponent<Text>().text;
if (PlayerPrefs.HasKey(index)) {
theText = PlayerPrefs.GetFloat(index).ToString();
}else{
theText = "0";
}
}
我没有格式化上面的文本,但这是一个小的调整。
推荐阅读
- linux - Bash 脚本会提供有关所需目录的信息
- macos - 无法在 Xamarin macOS 中启动 WebHost
- swift - UIKit:UICollectionViewCell 内的 UIPickerView 变得不可见
- r - 高级 R 函数调用
- ios - 在视图层次结构中全局更改 UIView 显示顺序
- azure - 使用基于分隔符的 Azure 数据工厂拆分文件
- r - 使用 dplyr 将新计算分组到一个数据帧中
- python - 为什么 discord.py 机器人不通过语音聊天播放音频?
- python - Python Tkinter 标签未正确移动
- go - JetBrains GoLand。如何终止程序?