持久化簡單的數據儲存在Unity3D 中提供了一個簡單有效的方法,如果之前的你做過Android的開發你會發現在Unity3D中持久化數據的儲存和Android非常的想象。
持久化簡單的數據儲存在Unity3D 中提供了一個簡單有效的方法,如果之前的你做過Android的開發你會發現在Unity3D中持久化數據的儲存和Android非常的想象。那么下面MOMO 將用一個簡單有效的例子向大家介紹Unity3D中持久化數據。
首先我們須要熟悉一下Unity3D中的PlayerPrefs這個類。這個類中一共幫助我們封裝了9個方法,用來數據的儲存與讀取。
舉一個例子
[代碼]c#/cpp/oc代碼:
1 |
PlayerPrefs.SetString("key", "value"); |
2 |
string str = PlayerPrefs.GetString("key", "defaule")); |
我們發現它是以鍵值對的形式進行儲存與讀取,每一個Key對應一個Value,儲存過后通過Key可以得到之前儲存的Value。這里說一下 GetString()方法中的第二個參數, 它代表默認值。意思是如果通過***個參數的Key沒有找到對應的Value的話GetString()方法就會返回我們寫的第二個參數的默認值。怎么樣?很簡單吧~ 感覺和Android完全一樣哈。
Unity3D 默認的字體的 size 只有 16 ,這就意味了放在iPhone4 (960 X 640)上 字體會顯示的非常小。字體的來源有很多,大家可以在互聯網上下載,或者從自己的電腦中拷貝,在Mac電腦下字體放在 Finder -> 資源庫 -> Fonts

我們可以看見電腦中存在了很多字體,我這里隨便選一個,將 華文仿宋.ttf 用鼠標拖動到Project中。
選中: 華文仿宋
FontSize 30 :毫無疑問是字體的大小,這里寫30讓字體幾乎放大1倍。
Character: 設置字體的文字編碼 Unicode ASCLL 編碼
Style:設置字體的風格,粗體 斜體
點擊Cretae ->GUISkin 創建一個GUI的皮膚,將 華文仿宋 拖動到箭頭所指向的方向。發現下面存在很多GUI皮膚相關控件設置的,可以在這里設置每一個高級控件~大家可以手動的修改一下看看效果哈。

游戲場景在游戲制作中是一個非常重要的部分,因為任何一款游戲都是由若干的場景組成,Unity3D的游戲場景做的非常貼心。
創建2個游戲場景,一個是scene0 一個是scene1 ,本章的目標是在***個游戲場景中保存一些基本游戲數據,然后切換到第二個場景中顯示***個場景中保存的數據,實現場景的切換已經數據的儲存。
在scene0中創建一個c# 腳本名稱為Scene0Main.cs 將它綁定在攝像頭中。
Scene0Main.cs
[代碼]c#/cpp/oc代碼:
02 |
using System.Collections; |
04 |
public class Scene0Main : MonoBehaviour { |
07 |
public string testStr; |
08 |
public string testInt; |
09 |
public string testFloat; |
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 |
38 |
GUI.DrawTexture(new Rect((Screen.width - Imagetexture.width) >>1, 10, 120, 120), Imagetexture); |
40 |
//添加輸入框讓用戶輸入信息,這里面我沒有捕獲異常,因為用戶有可能輸入一個不合法的數值 |
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); |
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 點擊Add Current添加場景,這一步很重要,如果不添加的話在代碼中切換場景會拋異常,盆友們還得注意一下~

build and run 導出運行項目,如下圖所示我分別輸入string int float 三種類型的數據,然后點擊commit all ,將所有數據全部保存下來,游戲場景切換到scene1場景中。

切換到scene1中可以正常的顯示scene0中儲存的數值,點擊clean all 將清空儲存的所有信息后返回場景scene0,點擊only return 直接返回場景scene0

另外兩個重要的方法
[代碼]c#/cpp/oc代碼:
1 |
//刪除 PlayerPrefs 中某一個key的值 |
2 |
PlayerPrefs. DeleteKey (“key”); |
4 |
//判斷 PlayerPrefs中是否存在這個key |
5 |
bool b = PlayerPrefs.HasKey(“key”); |