这是上一篇文章的补充
最近在做客户端与服务器的交互,使用JSON 和XML会感觉数据量太大,影响效率。最后使用二进制的方式来完成。如下图所示,使用二进制可以把空间节省到803K ,是不是很不错呢? 下面我们开始学习如何制作吧。

导出场景时增加导出二进制文件选项,代码如下。
01 | [MenuItem ( "GameObject/BINARY" )] |
02 | static void XMLJSONTOBinary () |
04 | string filepath = Application.dataPath + @"/StreamingAssets/binary.txt" ; |
05 | if (File.Exists (filepath)) |
07 | File.Delete(filepath); |
09 | FileStream fs = new FileStream(filepath, FileMode.Create); |
10 | BinaryWriter bw = new BinaryWriter(fs); |
11 | foreach (UnityEditor.EditorBuildSettingsScene S in UnityEditor.EditorBuildSettings.scenes) |
16 | EditorApplication.OpenScene(name); |
18 | foreach (GameObject obj in Object.FindObjectsOfType( typeof (GameObject))) |
20 | if (obj.transform.parent == null ) |
26 | short posx = ( short )(obj.transform.position.x * 100); |
02 | bw.Write(( short )(obj.transform.position.y * 100.0f)); |
03 | bw.Write(( short )(obj.transform.position.z * 100.0f)); |
04 | bw.Write(( short )(obj.transform.rotation.eulerAngles.x * 100.0f)); |
05 | bw.Write(( short )(obj.transform.rotation.eulerAngles.y * 100.0f)); |
06 | bw.Write(( short )(obj.transform.rotation.eulerAngles.z * 100.0f)); |
07 | bw.Write(( short )(obj.transform.localScale.x * 100.0f)); |
08 | bw.Write(( short )(obj.transform.localScale.y * 100.0f)); |
09 | bw.Write(( short )(obj.transform.localScale.z * 100.0f)); |
注解
在写入二进制数据时用到的核心类就是BinaryWriter ,Binary是二进制的意思 ,可见操作二进制写入就用BinaryWriter了。 常用的数据类型会分配固定的字节数量,假设BinaryWriter 写入一个short 那么就占2字节,写一个 int 就占4字节,如果是数组的话需要数组类型字节长度在乘以数组长度。
byte:一个字节(8位)
short:两个字节(16位)
int:四个字节(32位)(一个字长)
long:八个字节(64位)
float:四个字节(32位)
double:八个字节(64位)
然后在说说string,字符串它并不是标准的数据类型,它是一个对象 object 那么它的字节长度就是可变的。开始我也在string 上纠结了一小会儿。还有BinaryWriter 在写入string 的时候会现将字符串的长度以byte的形式储存,然后在储存字符串的字节长度。那么在解析字符串的时候需要先解析字符串长度,然后在根据长度取得后面对应长度的字节数组,再把这个字节数组转换成string就行啦。还有,上面我用的是short x 100 其实上为了节省长度, 因为short是2字节,float是4字节。我在解析的时候用short 在除以100 就可以 换算成float拉。
然后我们在看看解析的代码,写入的时候我们用的是BinaryWriter 那么读取的时候应该是 BinaryReader。
Binary.cs
002 | using System.Collections; |
006 | public class Binary : MonoBehaviour |
011 | string filepath = Application.dataPath + @"/StreamingAssets/binary.txt" ; |
013 | if (File.Exists (filepath)) |
015 | FileStream fs = new FileStream (filepath,FileMode.Open); |
016 | BinaryReader br = new BinaryReader(fs); |
021 | byte [] tempall = br.ReadBytes(( int )fs.Length); |
027 | if (index >= tempall.Length) |
033 | int scenelength = tempall[index]; |
034 | byte []sceneName = new byte [scenelength]; |
037 | System.Array.Copy(tempall,index,sceneName,0,sceneName.Length); |
039 | string sname = System.Text.Encoding.Default.GetString(sceneName); |
042 | int objectLength = tempall[index + sceneName.Length]; |
043 | byte []objectName = new byte [objectLength]; |
045 | index += sceneName.Length + 1; |
046 | System.Array.Copy(tempall,index,objectName,0,objectName.Length); |
047 | string oname = System.Text.Encoding.Default.GetString(objectName); |
051 | index += objectName.Length; |
052 | byte [] posx = new byte [2]; |
053 | System.Array.Copy(tempall,index,posx,0,posx.Length); |
055 | float x = System.BitConverter.ToInt16(posx,0) /100.0f; |
058 | index += posx.Length; |
059 | byte [] posy = new byte [2]; |
060 | System.Array.Copy(tempall,index,posy,0,posy.Length); |
061 | float y = System.BitConverter.ToInt16(posy,0) /100.0f; |
063 | index += posy.Length; |
064 | byte [] posz = new byte [2]; |
065 | System.Array.Copy(tempall,index,posz,0,posz.Length); |
066 | float z = System.BitConverter.ToInt16(posz,0) /100.0f; |
068 | index += posz.Length; |
069 | byte [] rotx = new byte [2]; |
070 | System.Array.Copy(tempall,index,rotx,0,rotx.Length); |
071 | float rx = System.BitConverter.ToInt16(rotx,0) /100.0f; |
073 | index += rotx.Length; |
074 | byte [] roty = new byte [2]; |
075 | System.Array.Copy(tempall,index,roty,0,roty.Length); |
076 | float ry = System.BitConverter.ToInt16(roty,0) /100.0f; |
078 | index += roty.Length; |
079 | byte [] rotz = new byte [2]; |
080 | System.Array.Copy(tempall,index,rotz,0,rotz.Length); |
081 | float rz = System.BitConverter.ToInt16(rotz,0) /100.0f; |
083 | index += rotz.Length; |
084 | byte [] scax = new byte [2]; |
085 | System.Array.Copy(tempall,index,scax,0,scax.Length); |
086 | float sx = System.BitConverter.ToInt16(scax,0) /100.0f; |
088 | index += scax.Length; |
089 | byte [] scay = new byte [2]; |
090 | System.Array.Copy(tempall,index,scay,0,scay.Length); |
091 | float sy = System.BitConverter.ToInt16(scay,0) /100.0f; |
093 | index += scay.Length; |
094 | byte [] scaz = new byte [2]; |
095 | System.Array.Copy(tempall,index,scaz,0,scaz.Length); |
096 | float sz = System.BitConverter.ToInt16(scaz,0) /100.0f; |
100 | if (sname.Equals( "Assets/StarTrooper.unity" )) |
103 | string asset = "Prefab/" + oname; |
104 | Vector3 pos = new Vector3 (x,y,z); |
105 | Vector3 rot = new Vector3(rx,ry,rz); |
106 | Vector3 sca = new Vector3(sx,sy,sz); |
107 | GameObject ob = (GameObject)Instantiate(Resources.Load(asset),pos,Quaternion.Euler(rot)); |
108 | ob.transform.localScale = sca; |
运行一下,场景依然生成的非常完美,在处理二进制解析的时候需要特别注意的就是字节对齐,因为你的所有数据其实就是一个byte[]字节数组,需要有理有序的把字节数组拆分,然后在转换成对应的数据,所以一定要对齐不然肯定会出错的。

最后把代码放出来,晚安 Good Ngith 哇咔咔。
下载地址 :http://vdisk.weibo.com/s/la_QE
留言中刚好有人讨论到这块。另外还有一种方式也可以实现动态增加建立场景,使用.unity 来实现场景的加载,我觉得这种方式可能会更好一些。我在网上已经发现有人写了,那就转载过来吧。
原文地址:http://blog.youkuaiyun.com/cony100/article/details/8842919
在Unity3d中,场景(scene)多半通过在build settings中点击add current或者把场景拖进面板实现,假如不这么做,你的场景便不会被加载,哪怕你制定了绝对路径。
就是说,一个游戏里要加载多少场景多半都是固定的。
这样的方法会有很多不便,不容易动态加载场景。所以我们今天要说的,是一种动态加载场景的方法。
首先,你需要一个编辑器文件,放在editor文件夹下。注意,这个文件不可以继承自monobehaviour
1 | public class BuildSceneEditor{ |
2 | [@MenuItem( "build/BuildWebplayerStreamed" )] |
4 | string [] levels = new string []{ "Assets/Level1.unity" , "Assets/Level2.unity" }; |
5 | BuildPipeline.BuildStreamedSceneAssetBundle(levels, "streamed.unity3d" ,BuildTarget.WebPlayer); |
这样,在你的unity编辑器上出现了一个按钮,你执行这个按钮,则会在你的Assets同级目录下出现你build好的streamed.unity3d文件,你把这个文件放在服务器上,下面一步就是下载这个文件并build了。
3 | 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来对同类的资源文件进行分包处理。