在 Android 移动平台上使用 Playerprefs 实现统一
using Playerprefs on Android Mobile Platform for unity
我有一个关于 android 手机游戏的玩家偏好未正确初始化的问题。我已经在一个值为 3 的变量上预先设置了我的播放器偏好数据。在我的统一播放模式中,该值通过我编写脚本的文本网格显示 3。但是当我在我的 android 移动设备上安装和测试它时它是 0。有人可以帮助如何在 android 平台上将我的播放器偏好数据正确初始化为正确的值吗?
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"));
}
}
当您按下 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);
Returns the value corresponding to key in the preference file if it exists. If it doesn't exist, it will return defaultValue.
如果 GetInt()
returns 默认值 (0),请注意第一段代码实际上创建了密钥 - 但第二个示例可能不会。这取决于 GetInt(string, int)
的实现 - 如果密钥不存在,我不确定是否会创建密钥。
我有一个关于 android 手机游戏的玩家偏好未正确初始化的问题。我已经在一个值为 3 的变量上预先设置了我的播放器偏好数据。在我的统一播放模式中,该值通过我编写脚本的文本网格显示 3。但是当我在我的 android 移动设备上安装和测试它时它是 0。有人可以帮助如何在 android 平台上将我的播放器偏好数据正确初始化为正确的值吗?
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"));
}
}
当您按下 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);
Returns the value corresponding to key in the preference file if it exists. If it doesn't exist, it will return defaultValue.
如果 GetInt()
returns 默认值 (0),请注意第一段代码实际上创建了密钥 - 但第二个示例可能不会。这取决于 GetInt(string, int)
的实现 - 如果密钥不存在,我不确定是否会创建密钥。