在Unity中,如何判断这是不是第一次打开游戏?
In Unity, how to tell if it's the first time a game is being opened?
我想要一个脚本 运行,
每当我在 Unity 中的游戏第一次打开时。
使用PlayerPrefs
。检查密钥是否存在。如果key不存在,return默认值为1,即第一次打开。此外,如果这是第一次打开,请将该键设置为 0,这样就再也不会 return 1 了。所以凡是not 1的值都表示是not第一次打开。在这个例子中我们可以调用键 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
}
我的代码:
public class LoadSaveManager : MonoBehaviour
{
public int IsFirst;
void Start ()
{
IsFirst = PlayerPrefs.GetInt("IsFirst") ;
if (IsFirst == 0)
{
//Do stuff on the first time
Debug.Log("first run");
PlayerPrefs.SetInt("IsFirst", 1);
} else {
//Do stuff other times
Debug.Log("welcome again!");
}
}
}
很简单。
您可以使用PlayerPrefs.HasKey
示例:
if(!PlayerPrefs.HasKey("firstTime")) //return true if the key exist
{
print("First time in the game.");
PlayerPrefs.SetInt("firstTime", 0); //to save a key with a value 0
//then came into being for the next time
}
else
{
print("It is not the first time in the game.");
//because the key "firstTime"
}
参考
https://docs.unity3d.com/ScriptReference/PlayerPrefs.HasKey.html
祝你好运!
很简单
您可以使用Time.realtimeSinceStartup;
示例:
if(Time.realtimeSinceStartup < 10)
{
// first game opening
}else
{
// Welcome again
}
参考:https://docs.unity3d.com/ScriptReference/Time-realtimeSinceStartup.html
祝你好运!
我想要一个脚本 运行,
每当我在 Unity 中的游戏第一次打开时。
使用PlayerPrefs
。检查密钥是否存在。如果key不存在,return默认值为1,即第一次打开。此外,如果这是第一次打开,请将该键设置为 0,这样就再也不会 return 1 了。所以凡是not 1的值都表示是not第一次打开。在这个例子中我们可以调用键 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
}
我的代码:
public class LoadSaveManager : MonoBehaviour
{
public int IsFirst;
void Start ()
{
IsFirst = PlayerPrefs.GetInt("IsFirst") ;
if (IsFirst == 0)
{
//Do stuff on the first time
Debug.Log("first run");
PlayerPrefs.SetInt("IsFirst", 1);
} else {
//Do stuff other times
Debug.Log("welcome again!");
}
}
}
很简单。
您可以使用PlayerPrefs.HasKey
示例:
if(!PlayerPrefs.HasKey("firstTime")) //return true if the key exist
{
print("First time in the game.");
PlayerPrefs.SetInt("firstTime", 0); //to save a key with a value 0
//then came into being for the next time
}
else
{
print("It is not the first time in the game.");
//because the key "firstTime"
}
参考 https://docs.unity3d.com/ScriptReference/PlayerPrefs.HasKey.html
祝你好运!
很简单
您可以使用Time.realtimeSinceStartup;
示例:
if(Time.realtimeSinceStartup < 10)
{
// first game opening
}else
{
// Welcome again
}
参考:https://docs.unity3d.com/ScriptReference/Time-realtimeSinceStartup.html
祝你好运!