在 Unity 中,如何判断是否是第一次打开游戏? [英] In Unity, how to tell if it's the first time a game is being opened?

查看:94
本文介绍了在 Unity 中,如何判断是否是第一次打开游戏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个脚本来运行,

I want a script to run,

每当我在 Unity 中的游戏第一次打开时.

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

推荐答案

使用 PlayerPrefs.检查密钥是否存在.如果key不存在,返回默认值1,即第一次打开.另外,如果这是第一次打开,将该键设置为 0,这样就不会再返回 1.因此,任何not 1 的值意味着它不是第一次打开.在这个例子中,我们可以调用键 FIRSTTIMEOPENING.

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
}

这篇关于在 Unity 中,如何判断是否是第一次打开游戏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆