Daily Bonus Rewards
- santosh nalla
- Jul 30, 2020
- 3 min read
Updated: Jul 31, 2020
Welcome back to my blog. In this blog in am gonna teach you how to implement daily bonus in your game.
So the first question? What exactly is daily bonus. Its some thing which mobile game developers do to retain their customers. So each day, you reward your players with some in-game coins.
Players can claim their reward. Once you claim the reward for that day, "Claim" label changes to "Claimed". Again you can come back next day to claim your reward for that day.
So basically it looks like this,
(Before you claim)

(after you claim)

So the basic logic here i am gonna use is,
I calculate the present day.
I grab previous day value(which i store in a prefab)
i subtract these two values(in days). If that value is greater than or equal to one - I label it as fresh day and activate claim button for that day.
To achieve this logic, we must use System.date library. This has some very useful functions to operate with.
_currentDate = System.DateTime.Now;
long temp =
Convert.ToInt64(PlayerPrefs.GetString("LastDate"));
_oldDate = DateTime.FromBinary(temp);
TimeSpan Total_Diff_Time = _currentDate.Subtract(_oldDate);
_difference_in_days = Total_Diff_Time.Days;
print("Total Diff Days : " + _difference_in_days);
if(_difference_in_days >= 1)
{
//Its a fresh day to claim.
}
else
{
//You are in same day!!
}
A combination of Playerpref and system.Date is used to achieve this.
I am putting entire commented code here. You can just drag this code and drop it on DailyBonusManager gameobject in your scene, fill in those necessary arrays and BOOM.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.SceneManagement;
public class DailyBonusManager : MonoBehaviour
{
//All "Already Claimed" lebels you can drop here in this array. This label gets activated once you claim your day reward!!
[SerializeField] GameObject[] _claimedButtons;
//All "interactable buttons", drop it here in this array. Once you click that button you get rewarded.
[SerializeField] GameObject[] _claimButtons;
//Daily reward amounnt for each day!! This array contains how much actually player gets rewarded each day.
[SerializeField] int[] _dailyRewards;
//Private Variables
DateTime _currentDate; //Stores current date
DateTime _oldDate; //Stores old date
int _difference_in_days; //Stores difference between current and old date. this diff is actually used in the calculation to know if its a fresh day or present day only.
void Start()
{
//Deactivates "Claim" and "Unclaim" labels on all daily bonus
ClearingClaimAndClaimedButtons();
//This function actually calculates Daily bonus.
CalculatingDailyBonus();
//This piece of code is usefull to know the status for that day. Like on that day if you have claimed your reward it shows -"CLAIMED" else - if you didn't cliam your reward, it shows "CLAIM" button to be clicked!!
if (PlayerPrefs.GetInt("ClaimedForTheDay") == 0)
ActivatingClaimButtonForThatDay(PlayerPrefs.GetInt("SavedDayNumber"));
else
ActiavtingClaimedButtonForThatDay(PlayerPrefs.GetInt("SavedDayNumber"));
}
//Deactivates "Claim" and "Unclaim" labels on all daily bonus
void ClearingClaimAndClaimedButtons()
{
foreach (var item in _claimedButtons)
{
item.SetActive(false);
}
foreach (var item in _claimButtons)
{
item.SetActive(false);
}
}
//This function actually calculates Daily bonus.
void CalculatingDailyBonus()
{
//If it is first time ever you are running this application, this piece of "if" is executed!!
if (PlayerPrefs.GetInt("FirstTime", 0) == 0)
{
PlayerPrefs.SetInt("FirstTime", 1);
//deactivating all claimed and claim buttons stored in arrays
ClearingClaimAndClaimedButtons();
//Activating day1
_claimButtons[0].SetActive(true);
PlayerPrefs.SetInt("SavedDayNumber", 1);
PlayerPrefs.SetString("LastDate", System.DateTime.Now.ToBinary().ToString());
}
//This part calculated the differece between current day and old day. If that diff is greater than one- that means a day has passed and that day claim is activated.
else
{
_currentDate = System.DateTime.Now;
long temp = Convert.ToInt64(PlayerPrefs.GetString("LastDate"));
_oldDate = DateTime.FromBinary(temp);
TimeSpan Total_Diff_Time = _currentDate.Subtract(_oldDate);
_difference_in_days = Total_Diff_Time.Days;
print("Total Diff Days : " + _difference_in_days);
if(_difference_in_days >= 1)
{
Debug.Log("FRESH DAY!!!");
PlayerPrefs.SetString("LastDate", System.DateTime.Now.ToBinary().ToString());
int temp_savedDayUpdate = PlayerPrefs.GetInt("SavedDayNumber"); //this SavedDayNumber prefab actually tell us what day it actually is.
temp_savedDayUpdate += 1;
PlayerPrefs.SetInt("SavedDayNumber", temp_savedDayUpdate);
//For seven plus days
if (PlayerPrefs.GetInt("SavedDayNumber") >= 8)
PlayerPrefs.SetInt("SavedDayNumber", 8);
ClearingClaimAndClaimedButtons();
ActivatingClaimButtonForThatDay(PlayerPrefs.GetInt("SavedDayNumber"));
PlayerPrefs.SetInt("ClaimedForTheDay", 0);
}
}
}
//Function which iis executed on clicking "CLAIM" button - to claim the reward for that day.
public void OnClaimButtonClicked()
{
int _savedDayNumber = PlayerPrefs.GetInt("SavedDayNumber") - 1;
Debug.Log("Button Clicked:" + _savedDayNumber);
_claimButtons[_savedDayNumber].SetActive(false);
_claimedButtons[_savedDayNumber].SetActive(true);
PlayerPrefs.SetInt("ClaimedForTheDay", 1);
//Updating score and taking to next scene
int score = PlayerPrefs.GetInt("Score", 0);
score += _dailyRewards[_savedDayNumber];
PlayerPrefs.SetInt("Score", score);
SceneManager.LoadScene("Gameplay");
}
void ActivatingClaimButtonForThatDay(int _day)
{
_claimButtons[_day - 1].SetActive(true);
}
void ActiavtingClaimedButtonForThatDay(int _day)
{
_claimedButtons[_day - 1].SetActive(true);
}
}
Final Output:
Comments