您的位置:首页 > 移动开发 > Unity3D

Unity PlayerPrefs 保存本地持久化数据

2015-08-07 10:41 639 查看

Unity PlayerPrefs 保存本地持久化数据

在Unity中,可以使用PlayerPrefs来保存一些简单的本地数据

先看下API
PlayerPrefs 玩家偏好

Stores and accesses player preferences between game sessions.

在游戏会话中储存和访问玩家偏好设置。

On Mac OS X PlayerPrefs are stored in ~/Library/Preferences folder, in a file named unity.[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.

在Mac OS X上PlayerPrefs存储在~/Library/PlayerPrefs文件夹,名为unity.[company name].[product name].plist,这里company和product名是在Project Setting中设置的,相同的plist用于在编辑器中运行的工程和独立模式.

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

在Windows独立模式下,PlayerPrefs被存储在注册表的 HKCU\Software\[company name]\[product name]键下,这里company和product名是在Project Setting中设置的.

On Web players, PlayerPrefs are stored in binary files under ~/Library/Preferences/Unity/WebPlayerPrefs on Mac OS X and %APPDATA%\Unity\WebPlayerPrefs on Windows. There is one preference file per Web player URL and the file size is limited to 1 megabyte. If this limit would be exceeded, SetInt, SetFloat and SetString will not store the value and throw a PlayerPrefsException.

在Web模式,PlayerPrefs存储在Mac OS X的二进制文件 ~/Library/Preferences/Unity/WebPlayerPrefs中和Windows的 %APPDATA%\Unity\WebPlayerPrefs中,一个偏好设置文件对应一个web播放器URL并且文件大小被限制为1MB。如果超出这个限制,SetInt、SetFloat和SetString将不会存储值并抛出一个PlayerPrefsException。

Class Functions类函数
SetIntSets the value of the preference identified by key.
设置由key确定的偏好值。GetIntReturns the value corresponding to key in the preference file if it exists.
如果存在,返回偏好文件中key对应的值。 SetFloatSets the value of the preference identified by key.
设置由key确定的偏好值。 GetFloatReturns the value corresponding to key in the preference file if it exists.
如果存在,返回偏好文件中key对应的值。 SetStringSets the value of the preference identified by key.
设置由key确定的偏好值。 GetStringReturns the value corresponding to key in the preference file if it exists.
如果存在,返回偏好文件中key对应的值。 HasKeyReturns true if key exists in the preferences.
如果key在偏好中存在,返回true。 DeleteKeyRemoves key and its corresponding value from the preferences.
从偏好中删除key和它对应的值。
DeleteAllRemoves all keys and values from the preferences. Use with caution.
从偏好中删除所有key。请谨慎使用。


创建一个简单界面来测试保存和提取Int,Float 和String类型的数据



submit按钮用于提交填写的数据,
using UnityEngine;
using System.Collections;

public class MyButton : MonoBehaviour {

public UIInput uiinputfieldint;

public UIInput uiinputfieldfloat;

public UIInput uiinputfieldstring;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}
void OnClick()
{

PlayerPrefs.SetInt("testUserInt", int.Parse(uiinputfieldint.value));
PlayerPrefs.SetFloat("testUserFloat", float.Parse(uiinputfieldfloat.value));
PlayerPrefs.SetString("testUserString", uiinputfieldstring.value);

Debug.Log("testUserInt = " + PlayerPrefs.GetInt("testUserInt"));
Debug.Log("testUserInt = " + PlayerPrefs.GetFloat("testUserFloat"));
Debug.Log("testUserInt = " + PlayerPrefs.GetString("testUserString"));
}
}


clear清空保存的数据
using System.Collections;

public class myButtonClear : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

void OnClick()
{

PlayerPrefs.DeleteAll();

}
}


check检查是否存在数据

using UnityEngine;
using System.Collections;

public class myButtonCheck : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

void OnClick()
{
if (PlayerPrefs.HasKey("testUserInt"))
{
Debug.Log("PlayerPrefs  Has Key testUserInt");
}
else
{
Debug.Log("PlayerPrefs  does not Have Key testUserInt");
}

if (PlayerPrefs.HasKey("testUserFloat"))
{
Debug.Log("PlayerPrefs  Has Key testUserFloat");
}
else
{
Debug.Log("PlayerPrefs  does not Have Key testUserFloat");
}

if (PlayerPrefs.HasKey("testUserString"))
{
Debug.Log("PlayerPrefs  Has Key testUserString");
}
else
{
Debug.Log("PlayerPrefs  does not Have Key testUserString");
}

}
}


先分别填写好Int Float String 三种数据
提交 -> Submit
检查是否存入 -> Check
清空数据 -> Clear
检查是否清空 -> Check

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: