本篇文章為大家展示了怎么在Unity3D中利用PlayerPrefs開(kāi)發(fā)數(shù)據(jù)持久化,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比藤縣網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式藤縣網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋藤縣地區(qū)。費(fèi)用合理售后完善,十多年實(shí)體公司更值得信賴。
首先我們來(lái)看兩段Unity3D中實(shí)現(xiàn)數(shù)據(jù)讀寫的簡(jiǎn)單代碼吧:
//保存數(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,主要的類方法有:
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確定的值.
好了,在了解layerPrefs的主要方法后,我們以一個(gè)具體的例子來(lái)學(xué)習(xí)Unity3D中數(shù)據(jù)持久化的實(shí)現(xiàn),我們希望實(shí)現(xiàn)在一個(gè)場(chǎng)景中輸入信息以便在新場(chǎng)景中讀取信息。我們直接創(chuàng)建兩個(gè)場(chǎng)景,分別命名為Scene0、Scene1(據(jù)說(shuō)程序員數(shù)數(shù)都是從0開(kāi)始的,哈哈),場(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"); } } }
上述內(nèi)容就是怎么在Unity3D中利用PlayerPrefs開(kāi)發(fā)數(shù)據(jù)持久化,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁(yè)名稱:怎么在Unity3D中利用PlayerPrefs開(kāi)發(fā)數(shù)據(jù)持久化
分享網(wǎng)址:http://chinadenli.net/article32/jeidpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、App設(shè)計(jì)、定制開(kāi)發(fā)、定制網(wǎng)站、網(wǎng)站導(dǎo)航、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)