Skip to content

Instantly share code, notes, and snippets.

@0xdw
Created February 1, 2021 02:26
Show Gist options
  • Save 0xdw/5ddd15f893bda5aca850c35b978954f0 to your computer and use it in GitHub Desktop.
Save 0xdw/5ddd15f893bda5aca850c35b978954f0 to your computer and use it in GitHub Desktop.
Unity - Simple Daily Rewards
using System;
using UnityEngine;
public static class DailyRewards {
private const int RewardValue = 100;
public static int Coins;
private static DateTime LastRewardTime {
get {
long time = long.Parse(PlayerPrefs.GetString(nameof(LastRewardTime), "0"));
return new DateTime(time);
}
set => PlayerPrefs.SetString(nameof(LastRewardTime), value.Ticks.ToString());
}
public static int PendingRewardValue {
get {
if (!HasPendingDailyReward) return -1;
int value = PlayerPrefs.GetInt(nameof(PendingRewardValue), -1);
if (value <= 0) {
PendingRewardValue = RewardValue;
return PendingRewardValue;
}
return value;
}
set => PlayerPrefs.SetInt(nameof(PendingRewardValue), value);
}
public static bool HasPendingDailyReward => DateTime.Now.Subtract(LastRewardTime).Days > 0;
public static bool GetReward() {
if (!HasPendingDailyReward) return false;
Coins += PendingRewardValue;
PendingRewardValue = -1;
LastRewardTime = DateTime.Now;
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment