轉(zhuǎn)自:http://www.it165.net/pro/html/201404/12519.html
1.//保存數(shù)據(jù)PlayerPrefs.SetString("Name",mName);
PlayerPrefs.SetInt("Age",mAge);
PlayerPrefs.SetFloat("Grade",mGrade)
//讀取數(shù)據(jù)mName=PlayerPrefs.GetString("Name","DefaultValue");
mAge=PlayerPrefs.GetInt("Age",0);
mGrade=PlayerPrefs.GetFloat("Grade",0F);
通過(guò)上面兩段代碼,我們可以發(fā)現(xiàn)兩點(diǎn):
1、Unity3D中的數(shù)據(jù)持久化是以鍵值的形式存儲(chǔ)的,可以看作是一個(gè)字典。
2、Unity3D中值是通過(guò)鍵名來(lái)讀取的,當(dāng)值不存在時(shí),返回默認(rèn)值。
目前,在Unity3D中只支持int、string、float三種數(shù)據(jù)類型的讀取,所以我們可以使用這三種數(shù)據(jù)類型來(lái)存儲(chǔ)簡(jiǎn)單的數(shù)據(jù)。目前Unity3D中用于數(shù)據(jù)持久化的類為layerPrefs,主要的類方法有:
好了,在了解layerPrefs的主要方法后,我們以一個(gè)具體的例子來(lái)學(xué)習(xí)Unity3D中數(shù)據(jù)持久化的實(shí)現(xiàn),我們希望實(shí)現(xiàn)在一個(gè)場(chǎng)景中輸入信息以便在新場(chǎng)景中
static function DeleteAll(): void
描述:從設(shè)置文件中移除所有鍵和值,謹(jǐn)慎的使用它們。
static function DeleteKey(key: string): void
描述:從設(shè)置文件中移除key和它對(duì)應(yīng)的值。
static function GetFloat(key: string, defaultValue: float=OF): float
描述:如果存在,返回設(shè)置文件中key對(duì)應(yīng)的值.如果不存在,它將返回defaultValue。
static function GetInt(key: string, defaultValue: int): int
描述:返回設(shè)置文件中key對(duì)應(yīng)的值,如果存在.如果不存在,它將返回defaultValue。
static function GetString(key: string, defaultValue: string=**): string
描述:返回設(shè)置文件中key對(duì)應(yīng)的值,如果存在.如果不存在,它將返回defaultValue.
static function HasKey(key: string): bool
描述:在設(shè)置文件如果存在key則返回真.
static function SetFloat(key: string, value: float): void
描述:設(shè)置由key確定的值.
static function SetInt(key: string, value: int): void
描述:設(shè)置由key確定的值.
static function SetString(key: string, value: string): void
描述:設(shè)置由key確定的值.
讀取信息。我們直接創(chuàng)建兩個(gè)場(chǎng)景,分別命名為Scene0、Scene1(據(jù)說(shuō)程序員數(shù)數(shù)都是從0開始的,哈哈),場(chǎng)景中保留主攝像機(jī)即可。接下來(lái)我們分別為兩個(gè)場(chǎng)景編寫腳本:
第一個(gè)場(chǎng)景的腳本:
using UnityEngine;
using System.Collections;
public class Scene1Script : MonoBehaviour {
//姓名
private string mName="路人甲";
//年齡
private int mAge=20;
//成績(jī)
private float mGrade=75.5F;
void OnGUI()
{
GUILayout.Label("Unity3D數(shù)據(jù)存儲(chǔ)示例程序",GUILayout.Height(25));
//姓名
GUILayout.Label("請(qǐng)輸入姓名:",GUILayout.Height(25));
mName=GUILayout.TextField(mName,GUILayout.Height(25));
//年齡
GUILayout.Label("請(qǐng)輸入年齡:",GUILayout.Height(25));
mAge=int.Parse(GUILayout.TextField(mAge.ToString(),GUILayout.Height(25)));
//成績(jī)
GUILayout.Label("請(qǐng)輸入成績(jī):",GUILayout.Height(25));
mGrade=float.Parse(GUILayout.TextField(mGrade.ToString(),GUILayout.Height(25)));
//提交數(shù)據(jù)
if(GUILayout.Button("提交數(shù)據(jù)",GUILayout.Height(25)))
{
//保存數(shù)據(jù)
PlayerPrefs.SetString("Name",mName);
PlayerPrefs.SetInt("Age",mAge);
PlayerPrefs.SetFloat("Grade",mGrade);
//切換到新場(chǎng)景
Application.LoadLevel("Scene1");
}
}
}
第二個(gè)場(chǎng)景的腳本:
using UnityEngine;
using System.Collections;
public class Scene2Script : MonoBehaviour {
private string mName;
private int mAge;
private float mGrade;
void Start ()
//讀取數(shù)據(jù)
mName=PlayerPrefs.GetString("Name","DefaultValue");
mAge=PlayerPrefs.GetInt("Age",0);
mGrade=PlayerPrefs.GetFloat("Grade",0F);
}
void OnGUI()
{
GUILayout.Label("Unity3D數(shù)據(jù)存儲(chǔ)示例程序",GUILayout.Height(25));
//姓名
GUILayout.Label("姓名:"+mName,GUILayout.Height(25));
//年齡
GUILayout.Label("年齡:"+mAge,GUILayout.Height(25));
//成績(jī)
GUILayout.Label("成績(jī):"+mGrade,GUILayout.Height(25));
//刪除數(shù)據(jù)
if(GUILayout.Button("清除數(shù)據(jù)",GUILayout.Height(25)))
{
PlayerPrefs.DeleteAll();
}
//返回Scene0
if(GUILayout.Button("返回場(chǎng)景",GUILayout.Height(25)))
{
Application.LoadLevel("Scene0");
}
}
}
我們這里直接將腳本綁定到攝像機(jī)上,然后將項(xiàng)目編譯,注意將兩個(gè)場(chǎng)景放入編譯序列,我們運(yùn)行程序:


告誡自己:知足常樂,以夢(mèng)為馬,浪跡天涯