持久化簡(jiǎn)單的數(shù)據(jù)儲(chǔ)存在Unity3D 中提供了一個(gè)簡(jiǎn)單有效的方法,如果之前的你做過Android的開發(fā)你會(huì)發(fā)現(xiàn)在Unity3D中持久化數(shù)據(jù)的儲(chǔ)存和Android非常的想象。那么下面MOMO 將用一個(gè)簡(jiǎn)單有效的例子向大家介紹Unity3D中持久化數(shù)據(jù)。

首先我們須要熟悉一下Unity3D中的PlayerPrefs這個(gè)類。這個(gè)類中一共幫助我們封裝了9個(gè)方法,用來數(shù)據(jù)的儲(chǔ)存與讀取。
舉一個(gè)例子
[代碼]c#/cpp/oc代碼:
| 1 | PlayerPrefs.SetString("key", "value"); |
| 2 | string str = PlayerPrefs.GetString("key", "defaule")); |
我們發(fā)現(xiàn)它是以鍵值對(duì)的形式進(jìn)行儲(chǔ)存與讀取,每一個(gè)Key對(duì)應(yīng)一個(gè)Value,儲(chǔ)存過后通過Key可以得到之前儲(chǔ)存的Value。這里說一下 GetString()方法中的第二個(gè)參數(shù), 它代表默認(rèn)值。意思是如果通過***個(gè)參數(shù)的Key沒有找到對(duì)應(yīng)的Value的話GetString()方法就會(huì)返回我們寫的第二個(gè)參數(shù)的默認(rèn)值。怎么樣?很簡(jiǎn)單吧~ 感覺和Android完全一樣哈。
Unity3D 默認(rèn)的字體的 size 只有 16 ,這就意味了放在iPhone4 (960 X 640)上 字體會(huì)顯示的非常小。字體的來源有很多,大家可以在互聯(lián)網(wǎng)上下載,或者從自己的電腦中拷貝,在Mac電腦下字體放在 Finder -> 資源庫(kù) -> Fonts
我們可以看見電腦中存在了很多字體,我這里隨便選一個(gè),將 華文仿宋.ttf 用鼠標(biāo)拖動(dòng)到Project中。
選中: 華文仿宋
FontSize 30 :毫無疑問是字體的大小,這里寫30讓字體幾乎放大1倍。
Character: 設(shè)置字體的文字編碼 Unicode ASCLL 編碼
Style:設(shè)置字體的風(fēng)格,粗體 斜體
點(diǎn)擊Cretae ->GUISkin 創(chuàng)建一個(gè)GUI的皮膚,將 華文仿宋 拖動(dòng)到箭頭所指向的方向。發(fā)現(xiàn)下面存在很多GUI皮膚相關(guān)控件設(shè)置的,可以在這里設(shè)置每一個(gè)高級(jí)控件~大家可以手動(dòng)的修改一下看看效果哈。
游戲場(chǎng)景在游戲制作中是一個(gè)非常重要的部分,因?yàn)槿魏我豢钣螒蚨际怯扇舾傻膱?chǎng)景組成,Unity3D的游戲場(chǎng)景做的非常貼心。
創(chuàng)建2個(gè)游戲場(chǎng)景,一個(gè)是scene0 一個(gè)是scene1 ,本章的目標(biāo)是在***個(gè)游戲場(chǎng)景中保存一些基本游戲數(shù)據(jù),然后切換到第二個(gè)場(chǎng)景中顯示***個(gè)場(chǎng)景中保存的數(shù)據(jù),實(shí)現(xiàn)場(chǎng)景的切換已經(jīng)數(shù)據(jù)的儲(chǔ)存。
在scene0中創(chuàng)建一個(gè)c# 腳本名稱為Scene0Main.cs 將它綁定在攝像頭中。
Scene0Main.cs
[代碼]c#/cpp/oc代碼:
| 02 | using System.Collections; |
| 04 | public class Scene0Main : MonoBehaviour { |
| 06 | //儲(chǔ)存數(shù)據(jù)的顯示 |
| 07 | public string testStr; |
| 08 | public string testInt; |
| 09 | public string testFloat; |
| 12 | //在外面用鼠標(biāo)拖動(dòng)上為它賦值 |
| 13 | public GUISkin fontSkin; |
| 15 | public Texture Imagetexture; |
| 17 | // Use this for initialization |
| 20 | testStr = PlayerPrefs.GetString("testStr", "default"); |
| 21 | testInt = PlayerPrefs.GetInt("testInt", 0).ToString(); |
| 22 | testFloat = PlayerPrefs.GetFloat("testFloat", 0).ToString(); |
| 26 | // Update is called once per frame |
| 34 | //將GUI的皮膚設(shè)置為我們創(chuàng)建的皮膚 |
| 38 | GUI.DrawTexture(new Rect((Screen.width - Imagetexture.width) >>1, 10, 120, 120), Imagetexture); |
| 40 | //添加輸入框讓用戶輸入信息,這里面我沒有捕獲異常,因?yàn)橛脩粲锌赡茌斎胍粋€(gè)不合法的數(shù)值 |
| 41 | testStr = GUI.TextField (new Rect(10, 200, 200, 50), testStr, 50); |
| 42 | testInt = GUI.TextField (new Rect(10, 250, 200, 50), testInt, 50); |
| 43 | testFloat = GUI.TextField (new Rect(10, 300, 200, 50), testFloat, 50); |
| 45 | //點(diǎn)擊按鈕保存所有數(shù)據(jù) |
| 46 | if (GUI.Button(new Rect(220, 200, 150, 100), "commit all")) |
| 49 | PlayerPrefs.SetString("testStr", testStr); |
| 50 | PlayerPrefs.SetInt("testInt", int.Parse(testInt)); |
| 51 | PlayerPrefs.SetFloat("testFloat", float.Parse(testFloat)); |
| 53 | Application.LoadLevel("scene1"); |
Scene1Main.cs
[代碼]c#/cpp/oc代碼:
| 02 | using System.Collections; |
| 04 | public class scene1Main : MonoBehaviour { |
| 06 | public string testStr; |
| 07 | public string testInt; |
| 08 | public string testFloat; |
| 10 | public GUISkin fontSkin; |
| 11 | public Texture Imagetexture; |
| 13 | // Use this for initialization |
| 15 | testStr = PlayerPrefs.GetString("testStr", "default"); |
| 16 | testInt = PlayerPrefs.GetInt("testInt", 0).ToString(); |
| 17 | testFloat = PlayerPrefs.GetFloat("testFloat", 0).ToString(); |
| 25 | GUI.DrawTexture(new Rect((Screen.width - Imagetexture.width) >>1, 10, 120, 120), Imagetexture); |
| 28 | GUI.Label(new Rect(10,150,300,50),"testStr = "+ testStr); |
| 29 | GUI.Label(new Rect(10,200,300,50),"testInt = "+ testInt); |
| 30 | GUI.Label(new Rect(10,250,300,50),"testFloat = "+ testFloat); |
| 32 | if (GUI.Button(new Rect(220, 200, 150, 100), "clean all")) |
| 35 | PlayerPrefs.DeleteAll(); |
| 37 | Application.LoadLevel("scene0"); |
| 40 | if (GUI.Button(new Rect(220, 320, 150, 100), "only return")) |
| 43 | Application.LoadLevel("scene0"); |
File -> Build Settings 點(diǎn)擊Add Current添加場(chǎng)景,這一步很重要,如果不添加的話在代碼中切換場(chǎng)景會(huì)拋異常,盆友們還得注意一下~
build and run 導(dǎo)出運(yùn)行項(xiàng)目,如下圖所示我分別輸入string int float 三種類型的數(shù)據(jù),然后點(diǎn)擊commit all ,將所有數(shù)據(jù)全部保存下來,游戲場(chǎng)景切換到scene1場(chǎng)景中。
切換到scene1中可以正常的顯示scene0中儲(chǔ)存的數(shù)值,點(diǎn)擊clean all 將清空儲(chǔ)存的所有信息后返回場(chǎng)景scene0,點(diǎn)擊only return 直接返回場(chǎng)景scene0
另外兩個(gè)重要的方法
[代碼]c#/cpp/oc代碼:
| 1 | //刪除 PlayerPrefs 中某一個(gè)key的值 |
| 2 | PlayerPrefs. DeleteKey (“key”); |
| 4 | //判斷 PlayerPrefs中是否存在這個(gè)key |
| 5 | bool b = PlayerPrefs.HasKey(“key”); |
分享標(biāo)題:Unity3D游戲引擎之游戲場(chǎng)景切換與持久化簡(jiǎn)單數(shù)據(jù)儲(chǔ)存
當(dāng)前URL:
http://www.fisionsoft.com.cn/article/coppphp.html