使用Android Mobile Platform上的Playerprefs统一

问题描述 投票:0回答:1

我对我的android手机游戏中的playerprefs初始化不正确有疑问。我已经将我的playerprefs数据预先设置为一个值为3的变量。在我的播放模式下,该值始终通过我编写脚本的文本网格显示为3。但是,当我在我的android移动设备上安装并对其进行测试时,它为0。有人可以帮助您如何在android平台上正确地将我的playerprefs数据初始化为正确的值吗?

public static Game GM;
int maxAntiBody;
public TextMeshProUGUI antibodyTexMesh;

void Start () {
if (GM == null)
{
    GM = this;
}
maxAntiBody = PlayerPrefs.GetInt("MaxAntiBody");
antibodyTexMesh.text = maxAntibody.toString();
}

void Update () {
 debugging();
}

 //I use this method for setting or resetting my desired value for the 
playerprefs data I'm working with. In this case I choose 3 as a value.

void debugging() {
if (Input.GetKeyDown(KeyCode.M))
{

    PlayerPrefs.SetInt("MaxAntiBody", plus++);
    Debug.Log("max anti body value is : " + 
 PlayerPrefs.GetInt("MaxAntiBody"));
}
else if (Input.GetKeyDown(KeyCode.R))
{
    plus = 0;
    PlayerPrefs.SetInt("MaxAntiBody", plus);
    Debug.Log("max anti body is now reseted to : " + 
  PlayerPrefs.GetInt("MaxAntiBody"));
}
}
c# unity3d game-engine
1个回答
1
投票

您仅在按M或R键(在调试方法中)时设置播放器首选项。

如果您希望该值具有默认值,则应将其添加到Start();

类似的东西;

void Start()
{

    // Set the MAxAntiBody to 3 if the value was never set to an initial value
    if(PlayerPrefs.GetInt("MaxAntiBody") == default(int))        
        PlayerPrefs.SetInt("MaxAntiBody", 3);

    ...

}

编辑:如注释中所指出,您还可以执行以下操作:

var x = PlayerPrefs.GetInt("MaxAntiBody", 3);

返回与首选项文件中的键对应的值(如果存在)。如果不存在,它将返回defaultValue。

[[0]如果GetInt()返回默认值(0),那么第一段代码实际上不会创建密钥-但是第二段代码可能不会。这取决于GetInt(string, int)的实现-我不确定如果它不存在,是否可以创建密钥。

© www.soinside.com 2019 - 2024. All rights reserved.