unity日常——游戏存档

来源:互联网 发布:网络接口芯片 编辑:程序博客网 时间:2024/06/05 02:12

unity日常——PlayerPrefs

游戏的存档方式有很多,今天就介绍一下PlayerPrefs

(一)PlayerPrefs.SetInt

public static void SetInt(string key, int value);
设置由Key确定的整数参数值。

PlayerPrefs.SetInt("Score", 20);

(二)PlayerPrefs.SetFloat

public static void SetFloat(string key, float value);
设置由Key确定的浮点参数值。

PlayerPrefs.SetFloat("Health", 50.0F);

(三)PlayerPrefs.SetString

public static void SetString(string key, string value);
设置由Key确定的字符串参数值。

    string m_PlayerName;    void Start()    {        m_PlayerName = "Enter Your Name";        PlayerPrefs.SetString("Name", m_PlayerName);    }

(四)PlayerPrefs.GetInt

public static void SetFloat(string key, float value);
读取文件Key的整数对应值,如果没有返回默认值

PlayerPrefs.GetInt("Score");

(五)PlayerPrefs.GetFloat

public static float GetFloat(string key, float defaultValue = 0.0F);
读取文件Key的浮点对应值,如果没有返回默认值

PlayerPrefs.GetFloat("Health");

(六)PlayerPrefs.GetString

public static string GetString(string key, string defaultValue = “”);
读取文件Key的字符串对应值,如果没有返回默认值

PlayerPrefs.GetString("Name");

(七)PlayerPrefs.DeleteKey

public static void DeleteKey(string key);
从游戏存档中删除对应的Key和它的对应值

PlayerPrefs.DeleteKey("Name");

(八)PlayerPrefs.DeleteAll

public static void DeleteAll();
从游戏存档中删除所有的Key和它的对应值,慎用。

PlayerPrefs.DeleteAll();

(九)PlayerPrefs.HasKey

public static bool HasKey(string key);
如果Key在游戏存档中存在,则返回true。

PlayerPrefs.HasKey("Score");

(十)PlayerPrefs.Save

public static void Save();
写入所有修改参数到硬盘
默认unity在程序退出时保存参数。这个函数写入硬盘时,可能会稍微的停顿。

如何查看储存在哪里?

在Windows系统下,被存储在注册表里 (regedit命令打开注册表)
HKEY_CURRENT_USER\Software[company name][product Setting](company和product 名是在Project Setting中设置的。)

—— 上一篇 [ unity日常—Mathf ]
—— 下一篇 [ unity日常—EventTrigger ]

原创粉丝点击