为由给定键标识的偏好设置单个整数值。您可以使用 PlayerPrefs.GetInt 检索此值。
以下示例将 KeyName 和 Value 变量传递给名为 SetInt 的函数。该函数将 KeyName 变量用作 PlayerPrefs.SetInt 中的标识符,并将 Value 用作要存储的内容。例如,您可以使用 PlayerPrefs.SetInt 来存储用户的货币,如下所示:/PlayerPrefs.SetInt(“CharacterMoney”,123)/。
然后,GetInt 函数使用相同的 KeyName 变量来检索存储在 PlayerPrefs 数据中的值。
using UnityEngine;
public class Example : MonoBehaviour { public void SetInt(string KeyName, int Value) { PlayerPrefs.SetInt(KeyName, Value); }
public int Getint(string KeyName) { return PlayerPrefs.GetInt(KeyName); } }