Save the date of first run

Hi.

I want to know how can I save the date of first run of the game, so I can compare it with each days date and according to days passed the game will do some thing. I would really appreciate if you help me. Or show me some documents since I didn’t know what to search.

Thanks in advanced

As @Deathdefy suggested, you could use PlayerPrefs - but with some extra code to ensure that the launch date would be saved only once. The logic is simple: try to read the initial date and, if there’s none, save the current date. You must save the date as a string, since PlayerPrefs doesn’t handle DateTime directly. Subtracting the launch date from the current date produces a TimeSpan value, which can be converted to days.

All of these weird functions and types are part of Mono/.Net (see .Net docs for more details), and you must import the namespace System in order to introduce them to Unity’s JS. The code could be something like this:

#pragma strict

import System; // introduce DateTime to the script

function Start(){
	// try to get the launch date saved as a string:
	var savedDate: String = PlayerPrefs.GetString("LaunchDate", "");
	if (savedDate == ""){ // if not saved yet...
		// convert current date to string...
		savedDate = DateTime.Now.ToString();
		// and save it in PlayerPrefs as LaunchDate:
		PlayerPrefs.SetString("LaunchDate", savedDate);
	}
	// at this point, the string savedDate contains the launch date
	// let's convert it to DateTime:
	var launchDate: DateTime;
	DateTime.TryParse(savedDate, launchDate);
	// get current DateTime:
	var now: DateTime = DateTime.Now;
	// calculate days ellapsed since launch date:
	var days: long = (now-launchDate).Days;
	print("Days since first launch:"+days);
}

You can save it to your Playerprefs and load it up the exact same way.

PlayerPrefs.SetString("FirstLaunchTime",System.DateTime.Now.ToString("MM/dd/yyyy"));

This will save the month day and year of the first launch.

You can access it by using GetString…

string dateTime = PlayerPrefs.GetString(“FirstLaunchTime”);