如何存储 DateTime 数据并在以后计算?统一 C#
How to store DateTime data and calculate it later? Unity C#
我是初学者,我正在尝试用一朵花制作类似经典电子鸡游戏的东西,您需要每天给它浇水一次。我使用“浇花”按钮作为主要功能。为此,我需要知道最后一次点击是什么时候,以便与当前时间进行比较。
我很努力地尝试使用 PlayerPrefs
和 DateTime
方法,但问题来了 — DateTime
使用 long,而 PlayerPrefs
使用 int
或 strings
。我找到了将 DateTime
转换为二进制然后再转换为字符串的解决方案,但是 Unity 告诉我这件事:
FormatException: Input string was not in a correct format.
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 value) (at <695d1cc93cca45069c528c15c9fdd749>:0)
HealthScript.Update () (at 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);
}
}
现在,我只是想解决这个问题,然后再做其他事情。
您能否建议控制台日志的解决方案,或者更好的方法来实现我的目标?
如果要登录到控制台,命令为Debug.Log
、Debug.LogWarning
或Debug.LogError
。
您还需要确保将控制台过滤器设置为显示您要打印的内容:
如果您更改存储和检索 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);
}
}
我是初学者,我正在尝试用一朵花制作类似经典电子鸡游戏的东西,您需要每天给它浇水一次。我使用“浇花”按钮作为主要功能。为此,我需要知道最后一次点击是什么时候,以便与当前时间进行比较。
我很努力地尝试使用 PlayerPrefs
和 DateTime
方法,但问题来了 — DateTime
使用 long,而 PlayerPrefs
使用 int
或 strings
。我找到了将 DateTime
转换为二进制然后再转换为字符串的解决方案,但是 Unity 告诉我这件事:
FormatException: Input string was not in a correct format. 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 value) (at <695d1cc93cca45069c528c15c9fdd749>:0) HealthScript.Update () (at 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);
}
}
现在,我只是想解决这个问题,然后再做其他事情。 您能否建议控制台日志的解决方案,或者更好的方法来实现我的目标?
如果要登录到控制台,命令为Debug.Log
、Debug.LogWarning
或Debug.LogError
。
您还需要确保将控制台过滤器设置为显示您要打印的内容:
如果您更改存储和检索 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);
}
}