我是一個初學者,我正在嘗試用花制作一個經典的 Tamagotchi 游戲,你需要每天給它澆一次水。我使用“澆花”按鈕作為主要功能。為此,我需要知道最后一次單擊以將其與當前時間進行比較是什么時候。
我很努力地嘗試使用PlayerPrefsandDateTime方法,但問題出在這里——DateTime使用 long 和PlayerPrefs使用intor strings。我找到了轉換DateTime為二進制然后轉換為字串的解決方案,但 Unity 告訴我這件事:
FormatException:輸入字串的格式不正確。System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Number.ParseInt64 (System.String value, System.Globalization.NumberStyles options, System.Globalization.NumberFormatInfo numfmt) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Int64.Parse (System.String s, System.IFormatProvider provider) (at <695d1cc93cca45069c528c15c9fdd749>: 0) System.Convert.ToInt64 (System.String 值) (在 <695d1cc93cca45069c528c15c9fdd749>:0) HealthScript.Update () (在 Assets/Scripts/HealthScript.cs:42)
這是整個代碼:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HealthScript : MonoBehaviour
{
public int health = 100;
public Slider slider;
public int damage;
public int waterTime;
public bool isGoing = true;
DateTime currentTime = DateTime.UtcNow;
DateTime lastTimeClicked;
//reseting the health every time button is clicked and saving the time of it
public void buttonClicked()
{
health = 100;
PlayerPrefs.SetString("Last Time Clicked", DateTime.UtcNow.ToBinary().ToString());
}
public void Update()
{
//connecting the health to the slider
slider.value = health;
//quit of the game
if (Input.GetKey(KeyCode.Escape))
{
Application.Quit();
}
//calculating the difference between last click and actual time
long temp = Convert.ToInt64(PlayerPrefs.GetString("Last Time Clicked"));
lastTimeClicked = DateTime.FromBinary(temp);
print("LastTimeClicked" lastTimeClicked);
TimeSpan difference = currentTime.Subtract(lastTimeClicked);
print("Difference: " difference);
}
}
現在,我只是想解決這個問題,以便繼續做其他事情。您能否建議控制臺日志的解決方案,或者是實作我目標的更好方法?
uj5u.com熱心網友回復:
如果您更改存盤和檢索DateTime邏輯的其余部分的方式應該沒問題。試試這個:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HealthScript : MonoBehaviour
{
public int health = 100;
public Slider slider;
public int damage;
public int waterTime;
public bool isGoing = true;
DateTime currentTime = DateTime.UtcNow;
DateTime lastTimeClicked;
//reseting the health every time button is clicked and saving the time of it
public void buttonClicked()
{
health = 100;
PlayerPrefs.SetString("Last Time Clicked", DateTime.UtcNow.ToString()); // you can add a formatter here if you want
}
public void Update()
{
//connecting the health to the slider
slider.value = health;
//quit of the game
if (Input.GetKey(KeyCode.Escape))
{
Application.Quit();
}
//calculating the difference between last click and actual time
lastTimeClicked = DateTime.Parse(PlayerPrefs.GetString("Last Time Clicked"));
print("LastTimeClicked" lastTimeClicked);
TimeSpan difference = currentTime.Subtract(lastTimeClicked);
print("Difference: " difference);
}
}
uj5u.com熱心網友回復:
如果要登錄到控制臺,命令為Debug.Log、Debug.LogWarning或Debug.LogError。
您需要確保您的控制臺過濾器也設定為顯示您要列印的任何內容:

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/461612.html
