【问题标题】:Unity 2019.2.12f1 PlayerPrefs halting scriptUnity 2019.2.12f1 PlayerPrefs 停止脚本
【发布时间】:2019-12-11 22:44:03
【问题描述】:

我正在为我正在进行的项目构建身份验证系统。

其中一部分是将 UserData 保存在本地,这样它就不必在每次运行时都连接到云,或强制登录 ETC。

我遇到的问题是,当我尝试设置 PlayerPrefs 时它不起作用。我让它在每个 PlayerPrefs.SetString 之前运行一个 Debug.Log,它只显示第一个。

我可以在我的 Start() 中执行 PlayerPrefs.SetString 但由于某种原因它在这里不起作用,我不知道为什么。

一切正常,直到我保存到 PlayerPrefs

public Dictionary<string, string> LoggedUser = new Dictionary<string, string>();

public void getUserData(string id, string email)
    {
        Debug.Log("Getting User Data for ID: " + id);
        FirebaseDatabase.DefaultInstance
            .GetReference(id)
            .GetValueAsync().ContinueWith(task1 =>
            {
                if (task1.IsFaulted)
                {
                    Debug.Log("Failed");
                }
                else if (task1.IsCompleted)
                {
                    Debug.Log("Data Retrieved");
                    DataSnapshot snapshot = task1.Result;
                    foreach (DataSnapshot user in snapshot.Children)
                    {

                        string firstName = user.Child("firstName").Value.ToString();
                        string lastName = user.Child("lastName").Value.ToString();
                        string sex = user.Child("sex").Value.ToString();
                        string country = user.Child("country").Value.ToString();
                        string age = user.Child("age").Value.ToString();
                        string userName = user.Child("userName").Value.ToString();

                        Debug.Log(firstName);

                        LoggedUser.Add("FirstName", firstName);
                        LoggedUser.Add("LastName", lastName);
                        LoggedUser.Add("Email", email);
                        LoggedUser.Add("age", age);
                        LoggedUser.Add("sex", sex);
                        LoggedUser.Add("country", country);
                        LoggedUser.Add("userName", userName);


                        Debug.Log("Welcome " + LoggedUser["FirstName"]);
                        Debug.Log("User signed in successfully: " +newUser.Email +" " +newUser.UserId);
                        setPlayer();
                    }
                }
            });
    }

public void setPlayer()
    {
        Debug.Log("Saving to PlayerPrefs");
        Debug.Log("Setting First Name");
        PlayerPrefs.SetString("FirstName", "Ezekiel");
        Debug.Log("Setting Last Name");
        PlayerPrefs.SetString("LastName", LoggedUser["LastName"]);
        Debug.Log("Setting Email");
        PlayerPrefs.SetString("Email", LoggedUser["Email"]);
        Debug.Log("Setting Age");
        PlayerPrefs.SetString("age", LoggedUser["age"]);
        Debug.Log("Setting Sex");
        PlayerPrefs.SetString("sex", LoggedUser["sex"]);
        Debug.Log("Setting Country");
        PlayerPrefs.SetString("country", LoggedUser["country"]);
        Debug.Log("Setting userName");
        PlayerPrefs.SetString("userName", LoggedUser["username"]);
        Debug.Log("Setting Loggedn State");
        PlayerPrefs.SetInt("LoggedIn", 1);
        Debug.Log("Saving Player Prefs");
        PlayerPrefs.Save();


        Debug.Log("Welcome " + PlayerPrefs.GetString("username"));
    }

【问题讨论】:

  • PlayerPrefs.Save() 将数据写入磁盘(例如,在 pc 上它是 hdd/ssd,在移动设备上它是内部存储(我知道这个信息是 2*2=4 :D))。也许您没有权限在您的机器上写入应用程序的日期,只需在其他设备或其他操作系统上尝试,否则在上面编写的代码中一切看起来都很好。
  • 另外,如果我错了并且没有设备权限问题,那么您必须在与上述脚本相关的其他脚本中搜索问题(如果有的话)
  • 什么样的搜索问题?我是计算机上的管理员,它在同一脚本的启动方法中工作
  • 我的意思是您的问题必须在其他脚本中搜索,而不是您在此处发布的这个脚本(这是唯一的假设),它在哪里起作用?在编辑器中还是在实际设备上构建之后?
  • 或者你的目标平台是什么?

标签: c# firebase unity3d authentication


【解决方案1】:

如果您不介意,我可以稍微扩展您的答案。

如果你使用ContinueWithOnMainThread 而不是ContinueWith,你的延续逻辑会自动发生在主线程上(所以你可以写PlayerPrefs)。如果您想比较它们,我会在 this article 中介绍大量使用 Firebase 和 Unity 处理线程的方法。

此外,这是在黑暗中尝试,但如果您使用的是实时数据库,那么您也可能使用Firebase Authentication。如果是这种情况,那么FirebaseAuth.DefaultInstance.CurrentUser 实际上会在您的游戏运行之间持续存在(我在this video 中稍微介绍了这一点)。由于实时数据库还会缓存您在本地请求的数据,因此您实际上可能正在做 Firebase 插件将为您处理的额外工作!

【讨论】:

    【解决方案2】:

    我找到了解决办法。

    getUserData() 方法正在异步运行,这会创建一个新线程。 PlayerPrefs 只能在主线程上运行。

    我的解决方案是创建一个布尔值,当数据加载到 LoggedUser 时我将其翻转为 true。我的 Update() 方法正在观察它是否翻转为 true,然后调用 setPlayer()。

    感谢大家的帮助!

    【讨论】:

      【解决方案3】:

      你正在设置userName

      PlayerPrefs.SetString("userName", LoggedUser["username"]);

      并检索username

      Debug.Log("欢迎" + PlayerPrefs.GetString("username"));

      应该也是

      PlayerPrefs.SetString("userName", LoggedUser["userName"]);

      而不是

      PlayerPrefs.SetString("userName", LoggedUser["username"]);

      【讨论】:

      • 感谢您指出这一点,我确实注意到了这一点并已修复。但这不是我的问题的解决方案
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      相关资源
      最近更新 更多