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

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

游戲場景在游戲制作中是一個非常重要的部分,因為任何一款游戲都是由若干的場景組成,Unity3D的游戲場景做的非常貼心。
創(chuàng)建2個游戲場景,一個是scene0 一個是scene1 ,本章的目標(biāo)是在第一個游戲場景中保存一些基本游戲數(shù)據(jù),然后切換到第二個場景中顯示第一個場景中保存的數(shù)據(jù),實現(xiàn)場景的切換已經(jīng)數(shù)據(jù)的儲存。
在scene0中創(chuàng)建一個c# 腳本名稱為Scene0Main.cs 將它綁定在攝像頭中。
Scene0Main.cs
- using UnityEngine;
- using System.Collections;
-
- public class Scene0Main : MonoBehaviour {
-
-
- public string testStr;
- public string testInt;
- public string testFloat;
-
-
-
- public GUISkin fontSkin;
-
- public Texture Imagetexture;
-
-
- void Start () {
-
- testStr = PlayerPrefs.GetString("testStr", "default");
- testInt = PlayerPrefs.GetInt("testInt", 0).ToString();
- testFloat = PlayerPrefs.GetFloat("testFloat", 0).ToString();
-
- }
-
-
- void Update () {
-
- }
-
-
- void OnGUI() {
-
-
- GUI.skin = fontSkin;
-
-
- GUI.DrawTexture(new Rect((Screen.width - Imagetexture.width) >>1, 10, 120, 120), Imagetexture);
-
-
- testStr = GUI.TextField (new Rect(10, 200, 200, 50), testStr, 50);
- testInt = GUI.TextField (new Rect(10, 250, 200, 50), testInt, 50);
- testFloat = GUI.TextField (new Rect(10, 300, 200, 50), testFloat, 50);
-
-
- if (GUI.Button(new Rect(220, 200, 150, 100), "commit all"))
- {
-
- PlayerPrefs.SetString("testStr", testStr);
- PlayerPrefs.SetInt("testInt", int.Parse(testInt));
- PlayerPrefs.SetFloat("testFloat", float.Parse(testFloat));
-
- Application.LoadLevel("scene1");
- }
- }
-
-
- }
Scene1Main.cs
- using UnityEngine;
- using System.Collections;
-
- public class scene1Main : MonoBehaviour {
-
- public string testStr;
- public string testInt;
- public string testFloat;
-
- public GUISkin fontSkin;
- public Texture Imagetexture;
-
-
- void Start () {
- testStr = PlayerPrefs.GetString("testStr", "default");
- testInt = PlayerPrefs.GetInt("testInt", 0).ToString();
- testFloat = PlayerPrefs.GetFloat("testFloat", 0).ToString();
-
- }
-
-
- void OnGUI() {
- GUI.skin = fontSkin;
-
- GUI.DrawTexture(new Rect((Screen.width - Imagetexture.width) >>1, 10, 120, 120), Imagetexture);
-
-
- GUI.Label(new Rect(10,150,300,50),"testStr = "+ testStr);
- GUI.Label(new Rect(10,200,300,50),"testInt = "+ testInt);
- GUI.Label(new Rect(10,250,300,50),"testFloat = "+ testFloat);
-
- if (GUI.Button(new Rect(220, 200, 150, 100), "clean all"))
- {
-
- PlayerPrefs.DeleteAll();
-
- Application.LoadLevel("scene0");
- }
-
- if (GUI.Button(new Rect(220, 320, 150, 100), "only return"))
- {
-
- Application.LoadLevel("scene0");
- }
- }
- }
File -> Build Settings 點擊Add Current添加場景,這一步很重要,如果不添加的話在代碼中切換場景會拋異常,盆友們還得注意一下~

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

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