Unity3D游戲開發(fā)之?dāng)?shù)據(jù)持久化PlayerPrefs的使用

轉(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)為馬,浪跡天涯

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,564評(píng)論 19 139
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,334評(píng)論 0 17
  • 1.在C/C++中實(shí)現(xiàn)本地方法 生成C/C++頭文件之后,你就需要寫頭文件對(duì)應(yīng)的本地方法。注意:所有的本地方法的第...
    JayQiu閱讀 2,532評(píng)論 0 3
  • SwiftDay011.MySwiftimport UIKitprintln("Hello Swift!")var...
    smile麗語(yǔ)閱讀 4,097評(píng)論 0 6
  • 摘自阿德勒《洞察人性》 心靈的一切活動(dòng)都擁有相同的目標(biāo),這是其最明顯的特征。可見心靈是種類多樣的行動(dòng)力的綜合體,而...
    靜水觀瀾閱讀 1,400評(píng)論 0 1

友情鏈接更多精彩內(nèi)容