Previous coins shown at screen and Daily Reward System is added.

posted in: Weekly Progress | 0

Player preff script is added for saving the previous coins and new coins which the player will get.

In player preff script daily random award for player is also added.

if user will come after 24 hours any amount of coins will be rewarded to the user .

using UnityEngine;
using System.Collections;

public class PlayerPreffManager : MonoBehaviour
{
private static PlayerPreffManager _instance;
public static PlayerPreffManager Instance {get {return _instance ?? (_instance = new PlayerPreffManager());} }

public void SaveInt(int SaveIntValue)
{
PlayerPrefs.SetInt(“Coins”,SaveIntValue);
}

public void SaveString(string Name)
{
PlayerPrefs.SetString(“Previous”,Name);
}

public void SaveFloat(float SaveFloatValue)
{
PlayerPrefs.SetFloat(“variable”, SaveFloatValue );
}

public int LoadInt()
{
int loadIntValue;
loadIntValue= PlayerPrefs.GetInt (“Coins”);
return loadIntValue;
}

public string LoadString()
{
string LoadStringName;
LoadStringName = PlayerPrefs.GetString(“Previous”);

return LoadStringName;
}

public float LoadFloat()
{
float LoadFloatValue;
LoadFloatValue = PlayerPrefs.GetFloat(“variable”);

return LoadFloatValue;

 

Award Manager script is as follows:

 

using UnityEngine.UI;
using System;
//using System.Random;

public class AwardManager : MonoBehaviour
{

private static AwardManager _instance;
public static AwardManager Instance {get {return _instance ?? (_instance = new AwardManager());} }
public GUISkin Skin;
public bool Display = true;
public String SPreviousTime;
public int GlobalAward = 100;
public int CoinsAwarded;

void Awake ()
{
Display = false;

System.Random rand = new System.Random();
int ChangeAward = rand.Next(1, 10);
CoinsAwarded = ChangeAward * GlobalAward;

Debug.Log(“Change award is “);
Debug.Log(ChangeAward);
int dayDifference = 0;
int monthDifference = 0;
int YearDifference = 0;
String sCurrentTime = “”;
SPreviousTime = PlayerPreffManager.Instance.LoadString();
DateTime Current = DateTime.Now;
sCurrentTime = Current.ToString ();
Debug.Log (“Previous time is “);
Debug.Log (SPreviousTime);
Debug.Log (“current time is “);
Debug.Log (sCurrentTime);

if (SPreviousTime == “”)
{
PlayerPreffManager.Instance.SaveString(sCurrentTime);
Debug.Log (“MAin if part “);
}

else
{
Debug.Log (“Main else part executed “);

DateTime Currdatetime = Convert.ToDateTime (sCurrentTime);
DateTime Predatetime = Convert.ToDateTime (SPreviousTime);
int prevTimeDay = Predatetime.Day;
int prevTimeMonth = Predatetime.Month;
int currentTimeDay = Currdatetime.Day;
int currentTimeMonth = Currdatetime.Month;
int prevTimeYear = Predatetime.Year;
int currentTimeYear = Currdatetime.Year;
dayDifference = currentTimeDay – prevTimeDay;
monthDifference = currentTimeMonth – prevTimeMonth;
YearDifference = currentTimeYear-prevTimeYear;

if (YearDifference > 0)
{
Display = true;
Debug.Log(“Reward given Year is increased”);
Predatetime = DateTime.Now;
SPreviousTime = Predatetime.ToString();
PlayerPreffManager.Instance.SaveString(SPreviousTime);
CurrencyManager.Instance.GlobalScore = PlayerPrefs.GetInt (“Coins”);
CurrencyManager.Instance.GlobalScore =CurrencyManager.Instance.GlobalScore + (GlobalAward*ChangeAward);

PlayerPreffManager.Instance.SaveInt(CurrencyManager.Instance.GlobalScore);
}
else if (monthDifference > 0 && YearDifference == 0)
{
Display = true;
Debug.Log(“Reward given month is greater”);
Predatetime = DateTime.Now;
SPreviousTime = Predatetime.ToString();
PlayerPreffManager.Instance.SaveString(SPreviousTime);
CurrencyManager.Instance.GlobalScore = PlayerPreffManager.Instance.LoadInt();
CurrencyManager.Instance.GlobalScore = CurrencyManager.Instance.GlobalScore + (GlobalAward*ChangeAward);

PlayerPreffManager.Instance.SaveInt(CurrencyManager.Instance.GlobalScore);

}
else if (dayDifference > 0 && monthDifference == 0)
{
Display = true;
Debug.Log(“Reward given month is greater”);
Predatetime = DateTime.Now;
SPreviousTime = Predatetime.ToString();
PlayerPreffManager.Instance.SaveString(SPreviousTime);
CurrencyManager.Instance.GlobalScore = PlayerPreffManager.Instance.LoadInt();
CurrencyManager.Instance.GlobalScore = CurrencyManager.Instance.GlobalScore + (GlobalAward*ChangeAward);

PlayerPreffManager.Instance.SaveInt(CurrencyManager.Instance.GlobalScore);
}
else
{
Display = false;
//No award
Debug.Log(“No reward “);
Predatetime = DateTime.Now;
SPreviousTime = Predatetime.ToString();
PlayerPreffManager.Instance.SaveString(SPreviousTime);
CurrencyManager.Instance.GlobalScore = PlayerPreffManager.Instance.LoadInt();
PlayerPreffManager.Instance.SaveInt(CurrencyManager.Instance.GlobalScore);
}
}
}

public void OnGUI()
{
if (Display == true)
{
GUILayout.Label(string.Format(
“{0} Coins Awarded”,CoinsAwarded), Skin.GetStyle(“PointsText”));
}

}
}

 

Leave a Reply