持久化简单的数据储存在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”); | 
            
                标题名称:Unity3D游戏引擎之游戏场景切换与持久化简单数据储存
                
                文章地址:http://www.csdahua.cn/qtweb/news38/473588.html
            
            
                网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
                                
            
                广告
                
            
            
                声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源:
                快上网