我是个初学者,我试着用一朵花做一个经典的Tamagotchi游戏,你需要每天浇水一次。我使用“浇花”按钮作为主要功能。要做到这一点,我需要知道最后一次点击是什么时候与当前的时间进行比较。
我很努力地尝试使用PlayerPrefs和DateTime方法,但这里有一个问题--使用longs的DateTime,使用int或strings的PlayerPrefs。我找到了将DateTime转换为二进制文件的解决方案,然后将其转换为字符串,但是联合告诉我:
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值,System.Globalization.NumberStyles选项,System.Globalization.NumberFormatInfo numfmt) (at <695d1cc93cca45069c528c15c9fdd749>:0)系统。( System.IFormatProvider provider) (at <695d1cc93cca45069c528c15c9fdd749>:0) System.Convert.ToInt64 (System.String value) (at <695d1cc93cca45069c528c15c9fdd749>:0) HealthScript.Update () (at资产/脚本/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);
}
}就目前而言,我只是试图解决这个问题,以更好地解决其他问题。请您提出控制台日志的解决方案,或者更好地实现我的目标?
发布于 2022-04-20 17:31:25
如果您改变了存储和检索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);
}
}发布于 2022-04-20 16:59:20
如果您想登录到控制台,命令是Debug.Log、Debug.LogWarning或Debug.LogError。
您还需要确保将控制台筛选器设置为显示要打印的内容:

https://stackoverflow.com/questions/71943473
复制相似问题