In Unity, how to tell if it's the first time a game is being opened?

浪尽此生 提交于 2019-12-28 04:15:14

问题


I want a script to run,

whenever my game in Unity is opened for the first time.


回答1:


Use PlayerPrefs. Check if key exist. If the key does not exist, return default value 1 and that is first time opening. Also, If this is first time opening set that key to 0 so that if will never return 1 again. So any value that is not 1 means that it is not the first time 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
}


来源:https://stackoverflow.com/questions/36956840/in-unity-how-to-tell-if-its-the-first-time-a-game-is-being-opened

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!