本地保存未来时间实现倒计时
TimeTool工具类:获取当前时间、未来时间、两时间差
using System;
using UnityEngine; public class TimeTool { public static DateTime GetFutureTime()
{
return DateTime.Today.AddDays() ;
} //获取当前
public static DateTime GetNowTime()
{
return DateTime.Today;
} public static int GetLeadTime(DateTime d1, DateTime d2)
{
return (d1 - d2).Days;
} }
使用unity自带本地保存数据,PlayerPrefs
//记录时间
DateTime time = TimeTool.GetFutureTime(); //设置时间
PlayerPrefs.SetString("CountDownTime", time.ToString()); //获取时间
PlayerPrefs.GetString("CountDownTime", "");
获得剩余时间,进行倒计时提示
string timeStr = PlayerPrefs.GetString("CountDownTime", ""); int day = TimeTool.GetLeadTime(Convert.ToDateTime(timeStr), TimeTool.GetNowTime()); text = "剩余" + day + "天";