日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Unity3D游戲引擎之游戲場景切換與持久化簡單數(shù)據(jù)儲存

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

首先我們須要熟悉一下Unity3D中的PlayerPrefs這個類。這個類中一共幫助我們封裝了9個方法,用來數(shù)據(jù)的儲存與讀取。

舉一個例子

[代碼]c#/cpp/oc代碼:

1PlayerPrefs.SetString("key", "value"); 
2string str = PlayerPrefs.GetString("key", "defaule"));

我們發(fā)現(xiàn)它是以鍵值對的形式進行儲存與讀取,每一個Key對應一個Value,儲存過后通過Key可以得到之前儲存的Value。這里說一下 GetString()方法中的第二個參數(shù), 它代表默認值。意思是如果通過***個參數(shù)的Key沒有找到對應的Value的話GetString()方法就會返回我們寫的第二個參數(shù)的默認值。怎么樣?很簡單吧~ 感覺和Android完全一樣哈。

Unity3D 默認的字體的 size 只有 16 ,這就意味了放在iPhone4 (960 X 640)上 字體會顯示的非常小。字體的來源有很多,大家可以在互聯(lián)網(wǎng)上下載,或者從自己的電腦中拷貝,在Mac電腦下字體放在 Finder -> 資源庫 -> Fonts

我們可以看見電腦中存在了很多字體,我這里隨便選一個,將 華文仿宋.ttf 用鼠標拖動到Project中。

選中: 華文仿宋

FontSize 30 :毫無疑問是字體的大小,這里寫30讓字體幾乎放大1倍。

Character:  設置字體的文字編碼 Unicode  ASCLL 編碼

Style:設置字體的風格,粗體 斜體

點擊Cretae ->GUISkin 創(chuàng)建一個GUI的皮膚,將 華文仿宋 拖動到箭頭所指向的方向。發(fā)現(xiàn)下面存在很多GUI皮膚相關控件設置的,可以在這里設置每一個高級控件~大家可以手動的修改一下看看效果哈。

游戲場景在游戲制作中是一個非常重要的部分,因為任何一款游戲都是由若干的場景組成,Unity3D的游戲場景做的非常貼心。

創(chuàng)建2個游戲場景,一個是scene0 一個是scene1 ,本章的目標是在***個游戲場景中保存一些基本游戲數(shù)據(jù),然后切換到第二個場景中顯示***個場景中保存的數(shù)據(jù),實現(xiàn)場景的切換已經數(shù)據(jù)的儲存。

在scene0中創(chuàng)建一個c# 腳本名稱為Scene0Main.cs 將它綁定在攝像頭中。

Scene0Main.cs

[代碼]c#/cpp/oc代碼:

01using UnityEngine; 
02using System.Collections; 
03   
04public class Scene0Main : MonoBehaviour { 
05   
06    //儲存數(shù)據(jù)的顯示 
07    public string testStr; 
08    public string testInt; 
09    public string testFloat; 
10       
11    //GUI皮膚 為上面我們添加的皮膚 
12    //在外面用鼠標拖動上為它賦值 
13    public GUISkin fontSkin; 
14    //顯示的圖片 
15    public Texture Imagetexture; 
16        
17    // Use this for initialization 
18    void Start () { 
19        //讀取key的值 
20        testStr = PlayerPrefs.GetString("testStr", "default"); 
21        testInt = PlayerPrefs.GetInt("testInt", 0).ToString(); 
22        testFloat = PlayerPrefs.GetFloat("testFloat", 0).ToString(); 
23           
24    } 
25       
26    // Update is called once per frame 
27    void Update () { 
28       
29    } 
30       
31       
32    void OnGUI() { 
33           
34        //將GUI的皮膚設置為我們創(chuàng)建的皮膚 
35        GUI.skin = fontSkin; 
36           
37        //貼上圖片 
38        GUI.DrawTexture(new Rect((Screen.width - Imagetexture.width) >>1, 10, 120, 120), Imagetexture); 
39           
40        //添加輸入框讓用戶輸入信息,這里面我沒有捕獲異常,因為用戶有可能輸入一個不合法的數(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); 
44           
45        //點擊按鈕保存所有數(shù)據(jù) 
46        if (GUI.Button(new Rect(220, 200, 150, 100), "commit all")) 
47        { 
48               
49            PlayerPrefs.SetString("testStr", testStr); 
50            PlayerPrefs.SetInt("testInt", int.Parse(testInt)); 
51            PlayerPrefs.SetFloat("testFloat", float.Parse(testFloat)); 
52            //切換場景到scene1 
53            Application.LoadLevel("scene1"); 
54        } 
55    } 
56       
57       
58}

Scene1Main.cs

[代碼]c#/cpp/oc代碼:

01using UnityEngine; 
02using System.Collections; 
03   
04public class scene1Main : MonoBehaviour { 
05   
06    public string testStr; 
07    public string testInt; 
08    public string testFloat; 
09       
10    public GUISkin fontSkin; 
11    public Texture Imagetexture; 
12        
13    // Use this for initialization 
14    void Start () { 
15        testStr = PlayerPrefs.GetString("testStr", "default"); 
16        testInt = PlayerPrefs.GetInt("testInt", 0).ToString(); 
17        testFloat = PlayerPrefs.GetFloat("testFloat", 0).ToString(); 
18           
19    } 
20       
21       
22    void OnGUI() { 
23        GUI.skin = fontSkin; 
24           
25        GUI.DrawTexture(new Rect((Screen.width - Imagetexture.width) >>1, 10, 120, 120), Imagetexture); 
26           
27        //顯示label 
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); 
31           
32        if (GUI.Button(new Rect(220, 200, 150, 100), "clean all")) 
33        { 
34            //刪除所有鍵值 
35            PlayerPrefs.DeleteAll(); 
36            // 返回場景0 
37            Application.LoadLevel("scene0"); 
38        } 
39           
40        if (GUI.Button(new Rect(220, 320, 150, 100), "only return")) 
41        { 
42            // 返回場景0 
43            Application.LoadLevel("scene0"); 
44        } 
45    } 
46}

File -> Build Settings 點擊Add Current添加場景,這一步很重要,如果不添加的話在代碼中切換場景會拋異常,盆友們還得注意一下~

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

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

另外兩個重要的方法

[代碼]c#/cpp/oc代碼:

1//刪除 PlayerPrefs 中某一個key的值 
2PlayerPrefs. DeleteKey (“key”); 
3   
4//判斷 PlayerPrefs中是否存在這個key 
5bool b = PlayerPrefs.HasKey(“key”);

當前題目:Unity3D游戲引擎之游戲場景切換與持久化簡單數(shù)據(jù)儲存
瀏覽地址:http://www.5511xx.com/article/coppphp.html