针对给定键标识的首选项设置单个字符串值。您可以使用 PlayerPrefs.GetString 来检索此值。
将值保持在 2 KB 或更小。若要存储更大数量的数据,请将其写入 Application.persistentDataPath 中的文件。
以下示例将 KeyName 和 Value 变量传递给名为 SetString 的函数。该函数在 PlayerPrefs.SetString 中使用 KeyName 变量作为标识符,并使用 Value 作为要存储的内容。例如,您可以使用 PlayerPrefs.SetString 来存储用户名,如下所示:PlayerPrefs.SetString(“CharacterName”, “James”)
然后,GetString 函数使用相同的 KeyName 变量来检索存储在 PlayerPrefs 数据中的值。
using UnityEngine;
public class Example : MonoBehaviour { public void SetString(string KeyName, string Value) { PlayerPrefs.SetString(KeyName, Value); }
public string GetString(string KeyName) { return PlayerPrefs.GetString(KeyName); } }