自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Unity3D 游戲引擎之游戲場(chǎng)景切換與持久化簡(jiǎn)單數(shù)據(jù)儲(chǔ)存

移動(dòng)開(kāi)發(fā) iOS 游戲開(kāi)發(fā)
持久化簡(jiǎn)單的數(shù)據(jù)儲(chǔ)存在Unity3D 中提供了一個(gè)簡(jiǎn)單有效的方法,如果之前的你做過(guò)Android的開(kāi)發(fā)你會(huì)發(fā)現(xiàn)在Unity3D中持久化數(shù)據(jù)的儲(chǔ)存和Android非常的想象。

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

首先我們須要熟悉一下Unity3D中的PlayerPrefs這個(gè)類(lèi)。這個(gè)類(lèi)中一共幫助我們封裝了9個(gè)方法,用來(lái)數(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ǔ)存過(guò)后通過(guò)Key可以得到之前儲(chǔ)存的Value。這里說(shuō)一下 GetString()方法中的第二個(gè)參數(shù), 它代表默認(rèn)值。意思是如果通過(guò)***個(gè)參數(shù)的Key沒(méi)有找到對(duì)應(yīng)的Value的話GetString()方法就會(huì)返回我們寫(xiě)的第二個(gè)參數(shù)的默認(rèn)值。怎么樣?很簡(jiǎn)單吧~ 感覺(jué)和Android完全一樣哈。

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

我們可以看見(jiàn)電腦中存在了很多字體,我這里隨便選一個(gè),將 華文仿宋.ttf 用鼠標(biāo)拖動(dòng)到Project中。

選中: 華文仿宋

FontSize 30 :毫無(wú)疑問(wèn)是字體的大小,這里寫(xiě)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# 腳本名稱(chēng)為Scene0Main.cs 將它綁定在攝像頭中。

Scene0Main.cs

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

01 using UnityEngine; 
02 using System.Collections; 
03    
04 public class Scene0Main : MonoBehaviour { 
05    
06     //儲(chǔ)存數(shù)據(jù)的顯示 
07     public string testStr; 
08     public string testInt; 
09     public string testFloat; 
10        
11     //GUI皮膚 為上面我們添加的皮膚 
12     //在外面用鼠標(biāo)拖動(dòng)上為它賦值 
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的皮膚設(shè)置為我們創(chuàng)建的皮膚 
35         GUI.skin = fontSkin; 
36            
37         //貼上圖片 
38         GUI.DrawTexture(new Rect((Screen.width - Imagetexture.width) >>1, 10, 120, 120), Imagetexture); 
39            
40         //添加輸入框讓用戶輸入信息,這里面我沒(méi)有捕獲異常,因?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); 
44            
45         //點(diǎn)擊按鈕保存所有數(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             //切換場(chǎng)景到scene1 
53             Application.LoadLevel("scene1"); 
54         } 
55     } 
56        
57        
58 }

Scene1Main.cs

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

01 using UnityEngine; 
02 using System.Collections; 
03    
04 public 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             // 返回場(chǎng)景0 
37             Application.LoadLevel("scene0"); 
38         } 
39            
40         if (GUI.Button(new Rect(220, 320, 150, 100), "only return")) 
41         { 
42             // 返回場(chǎng)景0 
43             Application.LoadLevel("scene0"); 
44         } 
45     } 
46 }

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

build and run 導(dǎo)出運(yùn)行項(xiàng)目,如下圖所示我分別輸入string int float 三種類(lèi)型的數(shù)據(jù),然后點(diǎn)擊commit all ,將所有數(shù)據(jù)全部保存下來(lái),游戲場(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”); 
3    
4 //判斷 PlayerPrefs中是否存在這個(gè)key 
5 bool b = PlayerPrefs.HasKey(“key”);
責(zé)任編輯:冰凝兒
相關(guān)推薦

2012-12-24 08:46:50

iOSUnity3D

2012-12-24 08:40:12

2012-12-24 08:52:44

iOSUnity3D

2012-12-24 08:48:25

iOSUnity3D

2012-12-24 08:50:21

iOSUnity3D

2012-12-24 09:04:04

iOSUnity3D

2012-12-24 08:57:35

iOSUnity3D

2012-12-24 09:01:41

iOSUnity3D

2012-12-24 09:07:09

iOSUnity3D

2012-12-24 08:51:23

iOSUnity3D

2012-12-24 08:59:13

iOSUnity3D

2012-12-24 08:45:19

iOSUnity3D

2012-12-24 08:56:15

iOSUnity3D

2012-12-24 08:54:47

iOSUnity3D

2012-12-24 09:06:14

iOSUnity3D

2012-12-24 09:11:58

iOSUnity3D

2012-12-24 09:00:31

iOSUnity3D

2012-12-24 09:17:12

iOSUnity3D

2013-06-25 09:16:10

Unity3D
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)