unity的本地化存储PlayerPrefs

在flash中可以用shareobject保存游戏变量到本地文件中,在unity中可以用PlayerPrefs实现同样的功能。

官方文档:http://docs.unity3d.com/ScriptReference/PlayerPrefs.html

unity圣典的翻译:http://game.ceeger.com/Script/PlayerPrefs/PlayerPrefs.html

文档中介绍了不同系统下这个本地文件的位置及命名格式。

PlayerPrefs以key value方式保存数据,最大文件大小为1M,超过限制会抛异常PlayerPrefsException.



提供的static方法如下:

DeleteAll Removes all keys and values from the preferences. Use with caution.
DeleteKey Removes key and its corresponding value from the preferences.
GetFloat Returns the value corresponding to key in the preference file if it exists.
GetInt Returns the value corresponding to key in the preference file if it exists.
GetString Returns the value corresponding to key in the preference file if it exists.
HasKey Returns true if key exists in the preferences.
Save Writes all modified preferences to disk.
SetFloat Sets the value of the preference identified by key.
SetInt Sets the value of the preference identified by key.
SetString Sets the value of the preference identified by key.
对于get方法,可以传入第二个参数作为默认返回值,如果从本地文件没有找到要取的值,则返回默认值。

float mGlobalVolume = PlayerPrefs.GetFloat("Sound", 1f);


如果要保存复杂数据到本地文件,可以序列化后保存为字符串。

不建议保存大量数据到本地,游戏中音效开关等设置比较合适保存到这个文件。


你可能感兴趣的:(技术,unity)