In Unity, how do I determine if a game is opening for the first time?

I want to run a script,

whenever my game in Unity opens for the first time.

+4
source share
1 answer

Use PlayerPrefs. Check if the key exists. If the key does not exist, return the default value of 1 and this is the first time. Also, if this is the first time, set this key to 0 so that it never returns 1 again . Therefore, any value not 1 means that it is not at the first opening. In this example, we can call the key FIRSTTIMEOPENING.

if (PlayerPrefs.GetInt("FIRSTTIMEOPENING", 1) == 1)
{
    Debug.Log("First Time Opening");

    //Set first time opening to false
    PlayerPrefs.SetInt("FIRSTTIMEOPENING", 0);

    //Do your stuff here

}
else
{
    Debug.Log("NOT First Time Opening");

    //Do your stuff here
}
+11

All Articles