在游戏会话中储存和访问游戏存档。这个是持久化数据储存,好比保存游戏记录。c#
DeleteAll
Removes all keys and values from the preferences. Use with caution. 从游戏存档中删除全部key。请谨慎使用。ide
DeleteKey
Removes key and its corresponding value from the preferences. 从游戏存档中删除key和它对应的值。函数
GetFloat
Returns the value corresponding to key in the preference file if it exists. 若是存在,返回游戏存档文件中key对应的浮点数值。3d
GetInt
Returns the value corresponding to key in the preference file if it exists. 若是存在,返回游戏存档文件中key对应的整数值。code
GetString
Returns the value corresponding to key in the preference file if it exists. 若是存在,返回游戏存档文件中key对应的字符串值。游戏
HasKey
Returns true if key exists in the preferences. 若是key在游戏存档中存在,返回true。ip
Save
Writes all modified preferences to disk. 写入全部修改参数到硬盘。字符串
SetFloat
Sets the value of the preference identified by key. 设置由key肯定的浮点数值。get
SetInt
Sets the value of the preference identified by key. 设置由key键肯定的整数值。string
SetString
Sets the value of the preference identified by key. 设置由key肯定的字符串值。
PlayerPrefs.SetString("Name",mName); PlayerPrefs.SetInt("Age",mAge); PlayerPrefs.SetFloat("Grade",mGrade)
mName=PlayerPrefs.GetString("Name","DefaultValue"); mAge=PlayerPrefs.GetInt("Age",0); mGrade=PlayerPrefs.GetFloat("Grade",0F);
PlayerPrefs的存储是有局限的,在unty3D中只支持int,string,float三种数据类型的写和读。
因为vector3是Unity3d中很是常见的数据类型,所以在这里我举例把vector3类型扩展到PlayerPrefs里面.
/// <summary> /// 存储Vector3类型的值 /// </summary> public static bool SetVector3(string key, Vector3 vector) { return SetFloatArray(key, new float[3] { vector.x, vector.y, vector.z }); } /// <summary> /// 读取Vector3类型的值 /// </summary> public static Vector3 GetVector3(string key) { float[] floatArray = GetFloatArray(key); if (floatArray.Length < 3) return Vector3.zero; return new Vector3(floatArray[0], floatArray[1], floatArray[2]); }
把上面的代码放到playerprefs原来的代码里面,就能保存和读取Vector3类型的数据了,其余类型的扩展相似,就不贴代码了.