Unity日常记录-本地保存未来时间实现倒计时
本地保存未来时间实现倒计时
TimeTool工具类:获取当前时间、未来时间、两时间差
1 using System; 2 using UnityEngine; 3 4 public class TimeTool { 5 6 public static DateTime GetFutureTime() 7 { 8 return DateTime.Today.AddDays(30) ; 9 } 10 11 //获取当前 12 public static DateTime GetNowTime() 13 { 14 return DateTime.Today; 15 } 16 17 public static int GetLeadTime(DateTime d1, DateTime d2) 18 { 19 return (d1 - d2).Days; 20 } 21 22 }
使用unity自带本地保存数据,PlayerPrefs
1 //记录时间 2 DateTime time = TimeTool.GetFutureTime(); 3 4 //设置时间 5 PlayerPrefs.SetString("CountDownTime", time.ToString()); 6 7 //获取时间 8 PlayerPrefs.GetString("CountDownTime", "");
获得剩余时间,进行倒计时提示
1 string timeStr = PlayerPrefs.GetString("CountDownTime", ""); 2 3 int day = TimeTool.GetLeadTime(Convert.ToDateTime(timeStr), TimeTool.GetNowTime()); 4 5 text = "剩余" + day + "天";