unity数值变量第二天自动更新恢复
实现逻辑
例如:有一个数值num初始为10,通过各种操作变为0了,第二天让这个值再更新恢复为10。
该方法可以做每日挑战次数10次,用完后第二天再恢复为10次等操作。
方法1:使用PlayerPrefs和DateTime
代码如下,可以根据自己需求添加功能:
using UnityEngine;
using System;
public class DailyNumManager : MonoBehaviour
{
private const string LastDateKey = "LastPlayDate";
private const string NumValueKey = "CurrentNum";
public int num = 0;
void Start()
{
// 获取上次游戏的日期
string lastDateStr = PlayerPrefs.GetString(LastDateKey, "");
DateTime lastDate;
// 如果是第一次游戏或者没有记录
if (string.IsNullOrEmpty(lastDateStr))
{
lastDate = DateTime.Today;
PlayerPrefs.SetString(LastDateKey, lastDate.ToString());
num = 0;
}
else
{
lastDate = DateTime.Pars