PlayerPrefs使用

来源:互联网 发布:网络信息安全认证 编辑:程序博客网 时间:2024/06/16 02:01

其实官方文档已经说得很明确很明确了,抄写一遍:

相关信息存放的具体位置:

On Mac OS X PlayerPrefs are stored in ~/Library/Preferences folder, in a file namedunity.[company name].[product name].plist, where company and product names are the names set up in Project Settings. The same .plist file is used for both Projects run in the Editor and standalone players.



On Windows, PlayerPrefs are stored in the registry underHKCU\Software\[company name]\[product name] key, where company and product names are the names set up in Project Settings.


On Linux, PlayerPrefs can be found in ~/.config/unity3d/[CompanyName]/[ProductName]again using the company and product names specified in the Project Settings.


On Windows Store Apps, Player Prefs can be found in %userprofile%\AppData\Local\Packages\[ProductPackageId]>\LocalState\playerprefs.dat


On Windows Phone 8, Player Prefs can be found in application's local folder


On Android data is stored (persisted) on the device. The data is saved in SharerPreferences. C#/JavaScript, Android Java and Native code can all access the PlayerPrefs data. The PlayerPrefs data is physically stored in /data/data/pkg-name/shared_prefs/pkg-name.xml.


On WebGL, PlayerPrefs are stored using the browser's IndexedDB API.


常用的方法

DeleteAllRemoves 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.

public static float GetFloat(string key, float defaultValue = 0.0F);
Returns the value corresponding to key in the preference file if it exists.
If it doesn't exist, it will return defaultValue.

如果key存在,返回key对应的值;如果不存在,返回默认值。

using UnityEngine;using System.Collections;public class ExampleClass : MonoBehaviour {    void Example() {        print(PlayerPrefs.GetFloat("Player Score"));    }}

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.
Writes all modified preferences to disk.

By default Unity writes preferences to disk during OnApplicationQuit(). In cases when the game crashes or otherwise prematuraly exits, you might want to write the PlayerPrefs at sensible 'checkpoints' in your game. This function will write to disk potentially causing a small hiccup, therefore it is not recommended to call during actual gameplay.

Note: There is no need to call this function manually inside OnApplicationQuit().
Note: On Windows Store Apps and Windows Phone 8.1 preferences are saved during application suspend, because there's no application quit event.

默认情况下在OnApplicationQuit()里unity会自动调用该方法,所以没必要在去手动调用;当然了,为了防止游戏崩溃却没有存档,你可以在游戏的“存盘点”处使用该方法。由于在存盘的时候会引起小卡顿,所以通常不建议在游戏时调用。

而在Windows Store Apps 和 Windows Phone 8.1上是在挂起的时候调用,因为这两者没有application quit详情点这里

SetFloatSets the value of the preference identified by key.

using UnityEngine;using System.Collections;public class ExampleClass : MonoBehaviour {    void Example() {        PlayerPrefs.SetFloat("Player Score", 10.0F);    }}
SetIntSets the value of the preference identified by key.
SetString Sets the value of the preference identified by key.

1 0
原创粉丝点击