亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

TryParse 從字符串到浮點數沒有給出結果

TryParse 從字符串到浮點數沒有給出結果

C#
HUH函數 2023-07-23 14:05:23
嘿伙計們,我知道它已經被覆蓋了很多,我已經到處看了,嘗試了所有的方法,我正在制作一個游戲,其中高分應該保存為最高分,為此,我需要將一個字符串作為該級別的最后時間來檢查它是否是最高分,但是當我這樣做時,我得到的所有結果都是 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;


查看完整回答
反對 回復 2023-07-23
  • 1 回答
  • 0 關注
  • 141 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號