首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何存储DateTime数据并在以后进行计算?单位C#

如何存储DateTime数据并在以后进行计算?单位C#
EN

Stack Overflow用户
提问于 2022-04-20 16:52:13
回答 2查看 457关注 0票数 0

我是个初学者,我试着用一朵花做一个经典的Tamagotchi游戏,你需要每天浇水一次。我使用“浇花”按钮作为主要功能。要做到这一点,我需要知道最后一次点击是什么时候与当前的时间进行比较。

我很努力地尝试使用PlayerPrefsDateTime方法,但这里有一个问题--使用longs的DateTime,使用intstringsPlayerPrefs。我找到了将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)

以下是整个代码:

代码语言:javascript
复制
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);
    }
}

就目前而言,我只是试图解决这个问题,以更好地解决其他问题。请您提出控制台日志的解决方案,或者更好地实现我的目标?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-04-20 17:31:25

如果您改变了存储和检索DateTime的方式,那么您的逻辑的其余部分应该是可以的。试试这个:

代码语言:javascript
复制
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);
    }
}
票数 0
EN

Stack Overflow用户

发布于 2022-04-20 16:59:20

如果您想登录到控制台,命令是Debug.LogDebug.LogWarningDebug.LogError

您还需要确保将控制台筛选器设置为显示要打印的内容:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71943473

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档