嘿伙计们,我知道它已经被覆盖了很多,我已经到处看了,尝试了所有的方法,我正在制作一个游戏,其中高分应该保存为最高分,为此,我需要将一个字符串作为该级别的最后时间来检查它是否是最高分,但是当我这样做时,我得到的所有结果都是 0,我在代码中进行了两次调试以查看我得到了什么,我看到刺痛作为最后一次,但浮点数为 0。我知道我的代码非常糟糕,它是我尝试制作的第一个游戏,但是我坚持这个问题好几个星期了。我用线条标记了我的问题,请帮助我!using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;using UnityEngine.SceneManagement;using System.Globalization;public class Timer : MonoBehaviour{public string strTag;public Text timerText;private float finalTime;private float startTime;// Start is called before the first frame updatevoid Start(){ startTime = Time.time;}// Update is called once per framevoid Update(){ float t = Time.time - startTime; string minutes = ((int)t / 60).ToString(); string seconds = (t % 60).ToString("f2"); timerText.text = minutes + ":" + seconds; } private void OnCollisionEnter(Collision collision) { if (collision.collider.tag == strTag) { -------------------------------------------------------- string finalTimeStr = timerText.text; float.TryParse(finalTimeStr, NumberStyles.Any, CultureInfo.InvariantCulture, out finalTime); Debug.Log(finalTimeStr); Debug.Log(finalTime); -------------------------------------------------------- if (finalTime > 0 && finalTime < PlayerPrefs.GetFloat("bestTime" + SceneManager.GetActiveScene().buildIndex, 9999)) { PlayerPrefs.SetFloat("bestTime" + SceneManager.GetActiveScene().buildIndex, finalTime); int Check = 0; Check = PlayerPrefs.GetInt("Check" + SceneManager.GetActiveScene().buildIndex, 0); Debug.Log(Check);
1 回答
慕码人2483693
TA贡献1860条经验 获得超9个赞
如果必须使用字符串来存储时间,您可能会考虑的一件事是使用该类Timespan来创建string值并将其解析为float值:
// This format is for one or two digit minutes and two digit seconds
const string timeFormat = "m\\:ss";
// Get the elapsed seconds
float t = Time.time - startTime;
// Display the seconds as a formatted string
timerText.text = TimeSpan.FromSeconds(t).ToString(timeFormat);
// Get the total number of seconds from the formatted time string
float finalTime = (float)TimeSpan.ParseExact(timerText.text, timeFormat,
CultureInfo.CurrentCulture).TotalSeconds;
- 1 回答
- 0 关注
- 111 浏览
添加回答
举报
0/150
提交
取消