Unity 判断两次时间间隔

using System;
using UnityEngine;

public class TimeMessage : MonoBehaviour
{
	//储存第一次的时间
    public void SetDateTime() 
    {
	//使用了PlayerPrefs    获取的时候一样

        PlayerPrefs.SetString("SetTime", DateTime.Now.ToShortTimeString()); 

    }
    // 判断第一次和现在的时间间隔
    public void GetDateTime()
    {
      
        DateTime nowTime = DateTime.Now;
        DateTime oldTime = DateTime.Parse(PlayerPrefs.GetString("SetTime"));

        TimeSpan timeSpan = nowTime - oldTime;

        //判断上次储存的时间
        if (timeSpan.Days > 1) 
        {
           
            Debug.Log("Days: "+ timeSpan.Days);
        }
        else if (timeSpan.Hours > 1)
        {
           
            Debug.Log("Hours: " + timeSpan.Hours);
        }
        else if (timeSpan.Minutes < 60)
        {
            Debug.Log("Minutes: " + timeSpan.Minutes);
        }
      
    }

}

发布了41 篇原创文章 · 获赞 36 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/yzx5452830/article/details/76921441