• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 311, comments - 0, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

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

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


            舉一個例子

            [csharp] view plaincopy
            1. PlayerPrefs.SetString("key""value");  
            2. 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

            [csharp] view plaincopy
            1. using UnityEngine;  
            2. using System.Collections;  
            3.   
            4. public class Scene0Main : MonoBehaviour {  
            5.   
            6.     //儲存數(shù)據(jù)的顯示  
            7.     public string testStr;  
            8.     public string testInt;  
            9.     public string testFloat;  
            10.       
            11.     //GUI皮膚 為上面我們添加的皮膚  
            12.     //在外面用鼠標(biāo)拖動上為它賦值  
            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.         //添加輸入框讓用戶輸入信息,這里面我沒有捕獲異常,因為用戶有可能輸入一個不合法的數(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


            [csharp] view plaincopy
            1. using UnityEngine;  
            2. using System.Collections;  
            3.   
            4. public class scene1Main : MonoBehaviour {  
            5.   
            6.     public string testStr;  
            7.     public string testInt;  
            8.     public string testFloat;  
            9.       
            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 導(dǎo)出運行項目,如下圖所示我分別輸入string int float 三種類型的數(shù)據(jù),然后點擊commit all ,將所有數(shù)據(jù)全部保存下來,游戲場景切換到scene1場景中。







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

            久久99免费视频| 狠狠人妻久久久久久综合蜜桃| 91亚洲国产成人久久精品网址| 久久99精品久久久久子伦| 7国产欧美日韩综合天堂中文久久久久 | 久久精品国产亚洲av麻豆色欲 | 91精品婷婷国产综合久久| 国产精品综合久久第一页| 久久国产精品视频| 久久久久久久波多野结衣高潮| 久久午夜伦鲁片免费无码| 久久青草国产精品一区| 亚洲欧洲精品成人久久曰影片| 午夜精品久久久久久中宇| 99久久精品毛片免费播放| 久久精品亚洲男人的天堂| 日产精品久久久一区二区| 久久国产热这里只有精品| 国产精品9999久久久久| 久久久久无码专区亚洲av| 天堂久久天堂AV色综合| 久久国产精品一区| 色综合色天天久久婷婷基地 | 久久综合给合久久狠狠狠97色69| 久久99精品国产| 无码专区久久综合久中文字幕| 国产精品99久久久久久董美香| 久久久婷婷五月亚洲97号色| 91精品日韩人妻无码久久不卡| 久久久久女人精品毛片| 久久精品免费网站网| 青青草国产精品久久久久| 麻豆一区二区99久久久久| 久久无码高潮喷水| 久久天天躁狠狠躁夜夜2020一 | 狠狠色婷婷久久一区二区| 久久久91人妻无码精品蜜桃HD| 国产精品久久国产精麻豆99网站| 国产亚洲美女精品久久久2020| 久久AV高潮AV无码AV| 亚洲av成人无码久久精品 |