Unity 3d中的PlayerPrefs不起作用(有时) [英] PlayerPrefs in Unity 3d doesn't work (sometimes)

查看:128
本文介绍了Unity 3d中的PlayerPrefs不起作用(有时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮助,PlayerPrefs似乎无法正常工作,无法弄清原因.

Help, PlayerPrefs doesn't seem to work and can't figure out why.

我有一个简单的游戏,开头是:

I have a simple game that starts with:

void Start()
{

    if (PlayerPrefs.GetInt("IsReturningUser") == 1)
    {
        ScoreLabel.text = "Welcome back, friend!";
    }
    else
    {
        PlayerPrefs.SetInt("IsReturningUser", 1);
        ScoreLabel.text = "Welcome new player";
    }

从Unity Editor在我的Android设备上进行测试(在连接状态下构建并运行)时,它可以按预期工作-当我第二次返回应用时,它检测到我正在返回.

When testing on my Android device from Unity Editor (build and run while connected) it works as intended - when I return to app second time it detects I'm returning.

但是,当我将相同的代码发布到Google Play商店并从那里进行干净安装时,当我第二次返回应用时,它永远不会检测到我是老用户.

However when I published the same code to Google Play store and clean installed from there, it never detects I'm a returning user when I return to app the second time.

也很奇怪,虽然它在我的Android手机上不起作用,但是在我妻子从Play商店安装的电话上,当她第二次打开它时,确实会检测为回头用户.

Also strange, while it doesn't work on my Android phone, on my wife's phone installed from the Play Store it does detect as a returning user when she opens it the second time.

有什么想法吗?

团结5.3.6 我的手机-Redmi Note 3,Android 6.0.1 MIUI 8.2稳定 妻子的电话-OnePlusX

Unity 5.3.6 My phone - Redmi Note 3, Android 6.0.1 MIUI 8.2 Stable Wife phone - OnePlusX

推荐答案

我之前也遇到过类似的问题.我通过在调用其他PlayerPrefs.Set *方法之后添加PlayerPrefs.Save()来解决.这会强制所有写入磁盘.

I had similar issue before. I solved by adding a PlayerPrefs.Save() after calling the other PlayerPrefs.Set* methods. This forces all writes to disk.

这篇关于Unity 3d中的PlayerPrefs不起作用(有时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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