【问题标题】:How to save Playerprefs to this case (Newbie)如何将 Playerprefs 保存到这种情况下(新手)
【发布时间】:2020-11-14 02:14:29
【问题描述】:

编辑:我想更清楚,也许我解释得不好,抱歉。 这是一个安卓游戏 关于仅切换角色图标(游戏对象)(而不是角色本身)的脚本 加载或关闭游戏时,我需要该图标保持用户选择的状态。

我可以在这个(相同的)脚本中保存和调用播放器首选项吗?


这是脚本:

using System.Collections;

使用 System.Collections.Generic; 使用 UnityEngine;

公共类角色选择:MonoBehaviour {

// referenses to controlled game objects
public GameObject avatar1, avatar2, avatar3;

// variable contains which avatar is on and active
int whichAvatarIsOn = 1;


// Use this for initialization
void Start()
{

    // enable first avatar and disable another one
    avatar1.gameObject.SetActive(true);
    avatar2.gameObject.SetActive(false);
    avatar3.gameObject.SetActive(false);

    
}

// public method to switch avatars by pressing UI button
public void SwitchAvatar()
{

    // processing whichAvatarIsOn variable
    switch (whichAvatarIsOn)
    {

        // if the first avatar and third is on
        case 1:

            // then the second avatar is on now
            whichAvatarIsOn = 2;

            // disable the first  and third one anable the second one
            avatar1.gameObject.SetActive(false);
            avatar2.gameObject.SetActive(true);
            avatar3.gameObject.SetActive(false);
            break;

        // if the second and first avatar is on
        case 2:

            // then the third  is on now
            whichAvatarIsOn = 3;

            // disable the second and first one and anable the third one
            avatar1.gameObject.SetActive(false);
            avatar2.gameObject.SetActive(false);
            avatar3.gameObject.SetActive(true);
            break;


        // if the third and second avatar is on
        case 3:

            // then the first avatar is on now
            whichAvatarIsOn = 1;

            // disable the second and first one and anable the third one
            avatar1.gameObject.SetActive(true);
            avatar2.gameObject.SetActive(false);
            avatar3.gameObject.SetActive(false);
            break;
    }

}

}

感谢您的宝贵时间

【问题讨论】:

标签: c# database unity3d


【解决方案1】:

PlayerPrefs 只能保存字符串、整数或浮点数。如果你想保存自定义数据,你必须编写自己的解析器并通过 File.WriteAllBytes 或类似的东西保存到本地文件。

现在您可以使用 PlayerPrefs.SetInt() 和 PlayerPrefs.Save() 保存 whichAvatarIsOn,并在游戏重新启动时使用 PlayerPrefs.GetInt() 加载它。

编辑:

Unity 的文档会比我解释得更好:https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

无论如何,我会给你一个简短的版本:

PlayerPrefs 类将数据保存到您设备上某处的文件中(文件夹\路径因您的应用名称和您使用的操作系统而异)。要保存数据,您需要指定一个 Key(这是一个字符串参数,即您要保存的值的 ID),然后指定要保存的值。因此,例如,如果我想保存 whichAvatarIsOn 我会调用:

PlayerPrefs.SetInt("MyIdForThisParameter",whichAvatarIsOn);

然后

PlayerPrefs.Save();

之所以需要调用 Save() 是因为 Save() 有效地将所有值写入文件,而 SetInt() 仅告诉 PlayerPrefs 在保存时会有一些数据需要更改。这样做是因为,如果您要设置大量数据,每次保存到文件会花费更长的时间。

完成此操作后,您基本上将 int 保存在文件中。所以,当你需要它时,你只需从文件中加载它:

int someInt = PlayerPrefs.GetInt("MyIdForThisParameter");

所以你只需传递你用来保存文件的密钥。

但是如果你一开始就没有保存“MyIdForThisParameter”会发生什么?

只需在调用中添加一个默认值:

int someInt = PlayerPrefs.GetInt("MyIdForThisParameter",0);

因此,如果您从未保存过该特定 ID,程序将默认分配 0。

正如我之前所说:请注意,您只能保存 int、float 或 string。你不能保存像 Vector3 或 Quaternion 这样的复杂类型。就这三种类型。

【讨论】:

  • 您好,谢谢您的回答,我是编码新手,我从教程中复制了这个脚本,想知道在这种情况下如何保存播放器首选项。我的意思是在哪里/何时保存以及在哪里/何时加载。
  • 编辑答案以更好地满足您的需求。
  • 非常感谢您向我解释这一点,我尽了最大努力,但我无法让它工作,我确定我犯了一些错误,我知道何时保存和加载,但我仍然不知道如何在这个特定的脚本中使用它。顺便说一句,谢谢你的努力
  • Pastebin 链接可能会更清楚:pastebin.com/KpbFMJWu
  • 你好,谢谢你的想法,我试过了,但有点我的开关字符停止工作,停留在默认字符:(,我真的不知道在哪里将命令正确放置到他们的正确的地方。我同意这个特定的脚本对我来说并不容易(目前我正在学习)
猜你喜欢
  • 1970-01-01
  • 2021-08-13
  • 1970-01-01
  • 2012-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多