c# - Unity 5 - Playerprefs 保存在统一但不是在 android 上?

标签 c# android unity-game-engine save scoring

我将此附加到我的英雄对象上,但它已被销毁。这可能是问题的一部分吗?我想要做的就是保存高分,然后将其显示在我的开始场景中。这是代码:

public int highscore; 
public int score;   


void AddPoint1()
{


    if (score > highscore) {
        highscore = score;
        PlayerPrefs.SetInt ("highscore", score);
        PlayerPrefs.Save ();

    }
}

这是附加到开始场景(不同场景)中的空对象的第二个脚本

void OnGUI()
{ 
    GUI.Label (new Rect (Screen.width / 2, Screen.height/2, 100, 100), "Highscore: " + PlayerPrefs.GetInt("highscore"), customGuistyle);
}

最佳答案

根据我对 Unity 的了解,您不需要调用 PlayerPrefs.Save(),我还注意到您将高分存储在本地并且从未定义,因此您将分数与空值进行比较。您的新代码如下所示:

public int score; 
void AddPoint1()
{
     if(score > PlayerPrefs.GetInt("highscore"))
     {
           PlayerPrefs.SetInt("highscore", score);
     }
}

此外,即使您最初将高分设置为某个值,比如 1。代码可以运行,但是一旦您关闭应用程序并重新运行它,高分就会重置回 1,然后即使玩家得分为 2,它将覆盖 PlayerPrefs 高分,无论它是什么。

关于c# - Unity 5 - Playerprefs 保存在统一但不是在 android 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31172496/

相关文章:

facebook - 找不到 OpenSSL Unity3D

c# - 如何从 XML 文档读取值以构建组合框?

c# - 在 Visual Studio 2008 外接程序中,如何判断类的属性实现了哪些接口(interface)?

android - OpenCV Android NDK 项目将无法构建

android - 当 Activity 从最近的 Activity 中被刷掉时,优雅地清理绑定(bind)服务

c# - MongoDB 的单例

c# - 从 Sql Server 迁移到 MySQL 后,EntityFramework 执行查询时出现问题

c# - 用Access数据库建工程成功,可以查询数据但不能删除记录

Android:为什么缩放控件是这样的?

android - 诊断无法从 OBB 文件/拆分 APK 扩展文件加载 android 构建(在 Unity 中)