补充html
最近在作客户端与服务器的交互,使用JSON 和XML会感受数据量太大,影响效率。最后使用二进制的方式来完成。以下图所示,使用二进制能够把空间节省到803K ,是否是很不错呢? 下面咱们开始学习如何制做吧。git
导出场景时增长导出二进制文件选项,代码以下。数组
C#服务器
1编辑器 2ide 3oop 4学习 5ui 6spa 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
[MenuItem ("GameObject/BINARY")] static void XMLJSONTOBinary () { string filepath = Application.dataPath + @"/StreamingAssets/binary.txt"; if(File.Exists (filepath)) { File.Delete(filepath); } FileStream fs = new FileStream(filepath, FileMode.Create); BinaryWriter bw = new BinaryWriter(fs); foreach (UnityEditor.EditorBuildSettingsScene S in UnityEditor.EditorBuildSettings.scenes) { if (S.enabled) { string name = S.path; EditorApplication.OpenScene(name);
foreach (GameObject obj in Object.FindObjectsOfType(typeof(GameObject))) { if (obj.transform.parent == null) { //注解 直接写入字符串 bw.Write(name); bw.Write(obj.name);
short posx = (short)(obj.transform.position.x * 100); |
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
bw.Write(posx); bw.Write((short)(obj.transform.position.y * 100.0f)); bw.Write((short)(obj.transform.position.z * 100.0f)); bw.Write((short)(obj.transform.rotation.eulerAngles.x * 100.0f)); bw.Write((short)(obj.transform.rotation.eulerAngles.y * 100.0f)); bw.Write((short)(obj.transform.rotation.eulerAngles.z * 100.0f)); bw.Write((short)(obj.transform.localScale.x * 100.0f)); bw.Write((short)(obj.transform.localScale.y * 100.0f)); bw.Write((short)(obj.transform.localScale.z * 100.0f));
} }
} }
bw.Flush(); bw.Close(); fs.Close(); } |
注解
在写入二进制数据时用到的核心类就是BinaryWriter ,Binary是二进制的意思 ,可见操做二进制写入就用BinaryWriter了。 经常使用的数据类型会分配固定的字节数量,假设BinaryWriter 写入一个short 那么就占2字节,写一个 int 就占4字节,若是是数组的话须要数组类型字节长度在乘以数组长度。
而后在说说string,字符串它并非标准的数据类型,它是一个对象 object 那么它的字节长度就是可变的。开始我也在string 上纠结了一小会儿。还有BinaryWriter 在写入string 的时候会现将字符串的长度以byte的形式储存,而后在储存字符串的字节长度。那么在解析字符串的时候须要先解析字符串长度,而后在根据长度取得后面对应长度的字节数组,再把这个字节数组转换成string就行啦。还有,上面我用的是short x 100 其实上为了节省长度, 由于short是2字节,float是4字节。我在解析的时候用short 在除以100 就能够 换算成float拉。
而后咱们在看看解析的代码,写入的时候咱们用的是BinaryWriter 那么读取的时候应该是 BinaryReader。
Binary.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
using UnityEngine; using System.Collections; using System.IO; using System.Text; using System; public class Binary : MonoBehaviour {
void Start () { string filepath = Application.dataPath + @"/StreamingAssets/binary.txt";
if(File.Exists (filepath)) { FileStream fs = new FileStream (filepath,FileMode.Open); BinaryReader br = new BinaryReader(fs);
int index = 0; //将二进制字节流所有读取在这个byte数组当中 //ReadBytes传递的参数是一个长度,也就是流的长度 byte[] tempall = br.ReadBytes((int)fs.Length);
//开始解析这个字节数组 while(true) { //当超过流长度,跳出循环 if(index >= tempall.Length) { break; }
//获得第一个byte 也就是获得字符串的长度 int scenelength = tempall[index]; byte []sceneName = new byte [scenelength]; index += 1; //根据长度拷贝出对应长度的字节数组 System.Array.Copy(tempall,index,sceneName,0,sceneName.Length); //而后把字节数组对应转换成字符串 string sname = System.Text.Encoding.Default.GetString(sceneName);
//这里和上面原理同样就不赘述 int objectLength = tempall[index + sceneName.Length]; byte []objectName = new byte [objectLength];
index += sceneName.Length + 1; System.Array.Copy(tempall,index,objectName,0,objectName.Length); string oname = System.Text.Encoding.Default.GetString(objectName);
//下面就是拿short 每个short的长度是2字节。
index += objectName.Length; byte[] posx = new byte[2]; System.Array.Copy(tempall,index,posx,0,posx.Length); //取得对应的数值 而后 除以100 就是float拉。 float x = System.BitConverter.ToInt16(posx,0) /100.0f;
//下面都差很少 index += posx.Length; byte[] posy = new byte[2]; System.Array.Copy(tempall,index,posy,0,posy.Length); float y = System.BitConverter.ToInt16(posy,0) /100.0f;
index += posy.Length; byte[] posz = new byte[2]; System.Array.Copy(tempall,index,posz,0,posz.Length); float z = System.BitConverter.ToInt16(posz,0) /100.0f;
index += posz.Length; byte[] rotx = new byte[2]; System.Array.Copy(tempall,index,rotx,0,rotx.Length); float rx = System.BitConverter.ToInt16(rotx,0) /100.0f;
index += rotx.Length; byte[] roty = new byte[2]; System.Array.Copy(tempall,index,roty,0,roty.Length); float ry = System.BitConverter.ToInt16(roty,0) /100.0f;
index += roty.Length; byte[] rotz = new byte[2]; System.Array.Copy(tempall,index,rotz,0,rotz.Length); float rz = System.BitConverter.ToInt16(rotz,0) /100.0f;
index += rotz.Length; byte[] scax = new byte[2]; System.Array.Copy(tempall,index,scax,0,scax.Length); float sx = System.BitConverter.ToInt16(scax,0) /100.0f;
index += scax.Length; byte[] scay = new byte[2]; System.Array.Copy(tempall,index,scay,0,scay.Length); float sy = System.BitConverter.ToInt16(scay,0) /100.0f;
index += scay.Length; byte[] scaz = new byte[2]; System.Array.Copy(tempall,index,scaz,0,scaz.Length); float sz = System.BitConverter.ToInt16(scaz,0) /100.0f;
index+=scaz.Length;
if(sname.Equals("Assets/StarTrooper.unity")) { //最后在这里把场景生成出来 string asset = "Prefab/" + oname; Vector3 pos = new Vector3 (x,y,z); Vector3 rot = new Vector3(rx,ry,rz); Vector3 sca = new Vector3(sx,sy,sz); GameObject ob = (GameObject)Instantiate(Resources.Load(asset),pos,Quaternion.Euler(rot)); ob.transform.localScale = sca; }
} } }
// Update is called once per frame void Update () {
} } |
运行一下,场景依然生成的很是完美,在处理二进制解析的时候须要特别注意的就是字节对齐,由于你的全部数据其实就是一个byte[]字节数组,须要有理有序的把字节数组拆分,而后在转换成对应的数据,因此必定要对齐否则确定会出错的。
最后把代码放出来,晚安 Good Ngith 哇咔咔。
下载地址 :http://vdisk.weibo.com/s/la_QE
留言中恰好有人讨论到这块。另外还有一种方式也能够实现动态增长创建场景,使用.unity 来实现场景的加载,我以为这种方式可能会更好一些。我在网上已经发现有人写了,那就转载过来吧。
原文地址:http://blog.csdn.net/cony100/article/details/8842919
在Unity3d中,场景(scene)多半经过在build settings中点击add current或者把场景拖进面板实现,假如不这么作,你的场景便不会被加载,哪怕你制定了绝对路径。
就是说,一个游戏里要加载多少场景多半都是固定的。
这样的方法会有不少不便,不容易动态加载场景。因此咱们今天要说的,是一种动态加载场景的方法。
首先,你须要一个编辑器文件,放在editor文件夹下。注意,这个文件不能够继承自monobehaviour
C#
1 2 3 4 5 6 7 |
public class BuildSceneEditor{ [@MenuItem("build/BuildWebplayerStreamed")] static void Build(){ string[] levels = new string[]{"Assets/Level1.unity","Assets/Level2.unity"}; BuildPipeline.BuildStreamedSceneAssetBundle(levels,"streamed.unity3d",BuildTarget.WebPlayer); } } |
这样,在你的unity编辑器上出现了一个按钮,你执行这个按钮,则会在你的Assets同级目录下出现你build好的streamed.unity3d文件,你把这个文件放在服务器上,下面一步就是下载这个文件并build了。
C#
1 2 3 |
WWW download = WWW.LoadFromCacheOrDownload("http://xxx/streamed.unity3d",0); yield return download; Application.LoadLevel("Level1"); |
你们注意到了吗。下载好之后就能够直接loadlevel了,不须要手动进行add current的操做了。
这里还有一篇圣典翻译的文章 http://game.ceeger.com/Script/BuildPipeline/BuildPipeline.BuildStreamedSceneAssetBundle.html
最后我在补充一下使用.unity3d确实方便不少,由于它不只会把场景打包进去,而且还会把场景中对应的资源文件打包进去。举个例子,你将美工作好的模型文件放在Project视图中,而后在将模型放在Hierarchy视图中的 100,100,100坐标点中,最后把该场景打包成.unity3d文件。此时你在新建一个工程只需下载刚刚打包的场景文件,他会自动把模型放在 100,100,100坐标点中。
这说明场景文件,包含了该场景中所用到的全部模型,而且还包含了模型资源与Hierarchy视图的关系。它会带来一个弊端,好比你有N个场景,每一个场景中都有相同的模型文件,这样每一个场景都须要重复下载这些相同的模型文件,因此我以为最好仍是使用assetbundle来对同类的资源文件进行分包处理。
转载请注明: 雨松MOMO
2012年12月07日
于 雨松MOMO程序研究院 发表