Unity3D研究院之将场景导出XML或JSON或二进制并且解析还原场景(四十二)(转自雨松MOMO)

前端之家收集整理的这篇文章主要介绍了Unity3D研究院之将场景导出XML或JSON或二进制并且解析还原场景(四十二)(转自雨松MOMO)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

导出Unity场景的所有游戏对象信息,一种是XML一种是JSON。本篇文章我们把游戏场景中游戏对象的、旋转、缩放、平移与Prefab的名称导出在XML与JSON中。然后解析刚刚导出的XML或JSON通过脚本把导出的游戏场景还原。在Unity官网上下载随便下载一个demo Project,如下图所示这是我刚刚在官网上下载的一个范例程序。

接着将层次视图中的所有游戏对象都封装成Prefab保存在资源路径中,这里注意一下如果你的Prefab绑定的脚本中有public Object 的话 ,需要在代码中改一下。。用 Find() FindTag()这类方法在脚本中Awake()方法中来拿,不然Prefab动态加载的时候无法赋值的,如下图所示,我把封装的Prefab对象都放在了Resources/Prefab文件夹下。

OK,现在我们就需要编写我们的导出工具、在Project视图中创建Editor文件夹,接着创建脚本MyEditor 。如下图所示。

因为编辑的游戏场景数量比较多,导出的时候我们需要遍历所有的游戏场景,一个一个的读取场景信息。然后取得游戏场景中所有游戏对象的Prefab的 名称 旋转 缩放 平移。有关XML的使用请大家看我的上一篇文章Unity3D研究院之使用 C#合成解析XML与JSON(四十一)代码中我只注释重点的部分,嘿嘿。

MyEditor.cs
001 using @H_301_43@UnityEngine;
002 using @H_301_43@System.Collections;
003 using @H_301_43@UnityEditor;
004 using @H_301_43@System.Collections.Generic;
005 using @H_301_43@System.Xml;
006 using @H_301_43@System.IO;
007 using @H_301_43@System.Text;
008 using @H_301_43@LitJson;
009 public class @H_301_43@MyEditor : Editor
010 @H_301_43@{
011 //将所有游戏场景导出为XML格式
012 @H_301_43@[MenuItem ("GameObject/ExportXML"@H_301_43@)]
013 static void @H_301_43@ExportXML ()
014 @H_301_43@{
015 string @H_301_43@filepath = Application.dataPath + @"/StreamingAssets/my.xml"@H_301_43@;
016 if@H_301_43@(!File.Exists (filepath))
017 @H_301_43@{
018 @H_301_43@File.Delete(filepath);
019 @H_301_43@}
020 @H_301_43@XmlDocument xmlDoc = new @H_301_43@XmlDocument();
021 @H_301_43@XmlElement root = xmlDoc.CreateElement("gameObjects"@H_301_43@);
022 //遍历所有的游戏场景
023 foreach @H_301_43@(UnityEditor.EditorBuildSettingsScene S in @H_301_43@UnityEditor.EditorBuildSettings.scenes)
024 @H_301_43@{
025 //当关卡启用
026 if @H_301_43@(S.enabled)
027 @H_301_43@{
028 //得到关卡的名称
029 string @H_301_43@name = S.path;
030 //打开这个关卡
031 @H_301_43@EditorApplication.OpenScene(name);
032 @H_301_43@XmlElement scenes = xmlDoc.CreateElement("scenes"@H_301_43@);
033 @H_301_43@scenes.SetAttribute("name"@H_301_43@,name);
034 foreach @H_301_43@(GameObject obj in @H_301_43@ Object.FindObjectsOfType(typeof@H_301_43@(GameObject)))
035 @H_301_43@{
036 if @H_301_43@(obj.transform.parent == null@H_301_43@)
037 @H_301_43@{
038 @H_301_43@XmlElement gameObject = xmlDoc.CreateElement("gameObjects"@H_301_43@);
039 @H_301_43@gameObject.SetAttribute("name"@H_301_43@,obj.name);
040
041 @H_301_43@gameObject.SetAttribute("asset"@H_301_43@,obj.name + ".prefab"@H_301_43@);
042 @H_301_43@XmlElement transform = xmlDoc.CreateElement("transform"@H_301_43@);
043 @H_301_43@XmlElement position = xmlDoc.CreateElement("position"@H_301_43@);
044 @H_301_43@XmlElement position_x = xmlDoc.CreateElement("x"@H_301_43@);
045 @H_301_43@position_x.InnerText = obj.transform.position.x+""@H_301_43@;
046 @H_301_43@XmlElement position_y = xmlDoc.CreateElement("y"@H_301_43@);
047 @H_301_43@position_y.InnerText = obj.transform.position.y+""@H_301_43@;
048 @H_301_43@XmlElement position_z = xmlDoc.CreateElement("z"@H_301_43@);
049 @H_301_43@position_z.InnerText = obj.transform.position.z+""@H_301_43@;
050 @H_301_43@position.AppendChild(position_x);
051 @H_301_43@position.AppendChild(position_y);
052 @H_301_43@position.AppendChild(position_z);
053
054 @H_301_43@XmlElement rotation = xmlDoc.CreateElement("rotation"@H_301_43@);
055 @H_301_43@XmlElement rotation_x = xmlDoc.CreateElement("x"@H_301_43@);
056 @H_301_43@rotation_x.InnerText = obj.transform.rotation.eulerAngles.x+""@H_301_43@;
057 @H_301_43@XmlElement rotation_y = xmlDoc.CreateElement("y"@H_301_43@);
058 @H_301_43@rotation_y.InnerText = obj.transform.rotation.eulerAngles.y+""@H_301_43@;
059 @H_301_43@XmlElement rotation_z = xmlDoc.CreateElement("z"@H_301_43@);
060 @H_301_43@rotation_z.InnerText = obj.transform.rotation.eulerAngles.z+""@H_301_43@;
061 @H_301_43@rotation.AppendChild(rotation_x);
062 @H_301_43@rotation.AppendChild(rotation_y);
063 @H_301_43@rotation.AppendChild(rotation_z);
064
065 @H_301_43@XmlElement scale = xmlDoc.CreateElement("scale"@H_301_43@);
066 @H_301_43@XmlElement scale_x = xmlDoc.CreateElement("x"@H_301_43@);
067 @H_301_43@scale_x.InnerText = obj.transform.localScale.x+""@H_301_43@;
068 @H_301_43@XmlElement scale_y = xmlDoc.CreateElement("y"@H_301_43@);
069 @H_301_43@scale_y.InnerText = obj.transform.localScale.y+""@H_301_43@;
070 @H_301_43@XmlElement scale_z = xmlDoc.CreateElement("z"@H_301_43@);
071 @H_301_43@scale_z.InnerText = obj.transform.localScale.z+""@H_301_43@;
072
073 @H_301_43@scale.AppendChild(scale_x);
074 @H_301_43@scale.AppendChild(scale_y);
075 @H_301_43@scale.AppendChild(scale_z);
076
077 @H_301_43@transform.AppendChild(position);
078 @H_301_43@transform.AppendChild(rotation);
079 @H_301_43@transform.AppendChild(scale);
080
081 @H_301_43@gameObject.AppendChild(transform);
082 @H_301_43@scenes.AppendChild(gameObject);
083 @H_301_43@root.AppendChild(scenes);
084 @H_301_43@xmlDoc.AppendChild(root);
085 @H_301_43@xmlDoc.Save(filepath);
086
087 @H_301_43@}
088 @H_301_43@}
089 @H_301_43@}
090 @H_301_43@}
091 //刷新Project视图, 不然需要手动刷新哦
092 @H_301_43@AssetDatabase.Refresh();
093 @H_301_43@}
094
095 //将所有游戏场景导出为JSON格式
096 @H_301_43@[MenuItem ("GameObject/ExportJSON"@H_301_43@)]
097 static void @H_301_43@ExportJSON ()
098 @H_301_43@{
099 string @H_301_43@filepath = Application.dataPath + @"/StreamingAssets/json.txt"@H_301_43@;
100 @H_301_43@FileInfo t = new @H_301_43@FileInfo(filepath);
101 if@H_301_43@(!File.Exists (filepath))
102 @H_301_43@{
103 @H_301_43@File.Delete(filepath);
104 @H_301_43@}
105 @H_301_43@StreamWriter sw = t.CreateText();
106
107 @H_301_43@StringBuilder sb = new @H_301_43@StringBuilder ();
108 @H_301_43@JsonWriter writer = new @H_301_43@JsonWriter (sb);
109 @H_301_43@writer.WriteObjectStart ();
110 @H_301_43@writer.WritePropertyName ("GameObjects"@H_301_43@);
111 @H_301_43@writer.WriteArrayStart ();
112
113 foreach @H_301_43@(UnityEditor.EditorBuildSettingsScene S in @H_301_43@UnityEditor.EditorBuildSettings.scenes)
114 @H_301_43@{
115 if @H_301_43@(S.enabled)
116 @H_301_43@{
117 string @H_301_43@name = S.path;
118 @H_301_43@EditorApplication.OpenScene(name);
119 @H_301_43@writer.WriteObjectStart();
120 @H_301_43@writer.WritePropertyName("scenes"@H_301_43@);
121 @H_301_43@writer.WriteArrayStart ();
122 @H_301_43@writer.WriteObjectStart();
123 @H_301_43@writer.WritePropertyName("name"@H_301_43@);
124 @H_301_43@writer.Write(name);
125 @H_301_43@writer.WritePropertyName("gameObject"@H_301_43@);
126 @H_301_43@writer.WriteArrayStart ();
127
128 foreach @H_301_43@(GameObject obj in @H_301_43@ Object.FindObjectsOfType(typeof@H_301_43@(GameObject)))
129 @H_301_43@{
130 if @H_301_43@(obj.transform.parent == null@H_301_43@)
131 @H_301_43@{
132 @H_301_43@writer.WriteObjectStart();
133 @H_301_43@writer.WritePropertyName("name"@H_301_43@);
134 @H_301_43@writer.Write(obj.name);
135
136 @H_301_43@writer.WritePropertyName("position"@H_301_43@);
137 @H_301_43@writer.WriteArrayStart ();
138 @H_301_43@writer.WriteObjectStart();
139 @H_301_43@writer.WritePropertyName("x"@H_301_43@);
140 @H_301_43@writer.Write(obj.transform.position.x.ToString("F5"@H_301_43@));
141 @H_301_43@writer.WritePropertyName("y"@H_301_43@);
142 @H_301_43@writer.Write(obj.transform.position.y.ToString("F5"@H_301_43@));
143 @H_301_43@writer.WritePropertyName("z"@H_301_43@);
144 @H_301_43@writer.Write(obj.transform.position.z.ToString("F5"@H_301_43@));
145 @H_301_43@writer.WriteObjectEnd();
146 @H_301_43@writer.WriteArrayEnd();
147
148 @H_301_43@writer.WritePropertyName("rotation"@H_301_43@);
149 @H_301_43@writer.WriteArrayStart ();
150 @H_301_43@writer.WriteObjectStart();
151 @H_301_43@writer.WritePropertyName("x"@H_301_43@);
152 @H_301_43@writer.Write(obj.transform.rotation.eulerAngles.x.ToString("F5"@H_301_43@));
153 @H_301_43@writer.WritePropertyName("y"@H_301_43@);
154 @H_301_43@writer.Write(obj.transform.rotation.eulerAngles.y.ToString("F5"@H_301_43@));
155 @H_301_43@writer.WritePropertyName("z"@H_301_43@);
156 @H_301_43@writer.Write(obj.transform.rotation.eulerAngles.z.ToString("F5"@H_301_43@));
157 @H_301_43@writer.WriteObjectEnd();
158 @H_301_43@writer.WriteArrayEnd();
159
160 @H_301_43@writer.WritePropertyName("scale"@H_301_43@);
161 @H_301_43@writer.WriteArrayStart ();
162 @H_301_43@writer.WriteObjectStart();
163 @H_301_43@writer.WritePropertyName("x"@H_301_43@);
164 @H_301_43@writer.Write(obj.transform.localScale.x.ToString("F5"@H_301_43@));
165 @H_301_43@writer.WritePropertyName("y"@H_301_43@);
166 @H_301_43@writer.Write(obj.transform.localScale.y.ToString("F5"@H_301_43@));
167 @H_301_43@writer.WritePropertyName("z"@H_301_43@);
168 @H_301_43@writer.Write(obj.transform.localScale.z.ToString("F5"@H_301_43@));
169 @H_301_43@writer.WriteObjectEnd();
170 @H_301_43@writer.WriteArrayEnd();
171
172 @H_301_43@writer.WriteObjectEnd();
173 @H_301_43@}
174 @H_301_43@}
175
176 @H_301_43@writer.WriteArrayEnd();
177 @H_301_43@writer.WriteObjectEnd();
178 @H_301_43@writer.WriteArrayEnd();
179 @H_301_43@writer.WriteObjectEnd();
180 @H_301_43@}
181 @H_301_43@}
182 @H_301_43@writer.WriteArrayEnd();
183 @H_301_43@writer.WriteObjectEnd ();
184
185 @H_301_43@sw.WriteLine(sb.ToString());
186 @H_301_43@sw.Close();
187 @H_301_43@sw.Dispose();
188 @H_301_43@AssetDatabase.Refresh();
189 @H_301_43@}
190 @H_301_43@}

OK。此时我们就可以导出游戏场景的信息拉,注意游戏场景的需要现在Project Setting 中注册。点击 GameObject – > Export XML 和 GameObject – > ExportJson 菜单项即可开始生成

如下图所示,场景导出完毕后,会将xml 与Json 文件保存在StreamingAssets路径下,放在这里的原因是方便移动平台移植,因为它们属于二进制文件,移动平台在读取二进制文件的路径是不一样的。一定要放在这里喔。

接着,我继续创建两个游戏场景,一个用来解析XML的场景,一个用来解析JSON的场景。

XML场景中,创建一个空的游戏对象,把XML.cs挂上去。

001 using @H_301_43@UnityEngine;
002 using @H_301_43@System.Collections;
003 using @H_301_43@System.Xml;
004 using @H_301_43@System.IO;
005 public class @H_301_43@XML : MonoBehavIoUr {
006
007 // Use this for initialization
008 void @H_301_43@Start ()
009 @H_301_43@{
010
011 //电脑和iphong上的路径是不一样的,这里用标签判断一下。
012 #if UNITY_EDITOR
013 string @H_301_43@filepath = Application.dataPath +"/StreamingAssets"@H_301_43@+"/my.xml"@H_301_43@;
014 #elif UNITY_IPHONE
015 string @H_301_43@filepath = Application.dataPath +"/Raw"@H_301_43@+"/my.xml"@H_301_43@;
016 #endif
017 //如果文件存在话开始解析。
018 if@H_301_43@(File.Exists (filepath))
019 @H_301_43@{
020 @H_301_43@XmlDocument xmlDoc = new @H_301_43@XmlDocument();
021 @H_301_43@xmlDoc.Load(filepath);
022 @H_301_43@XmlNodeList nodeList=xmlDoc.SelectSingleNode("gameObjects"@H_301_43@).ChildNodes;
023 foreach@H_301_43@(XmlElement scene in @H_301_43@nodeList)
024 @H_301_43@{
025 //因为我的XML是把所有游戏对象全部导出, 所以这里判断一下只解析需要的场景中的游戏对象
026 //JSON和它的原理类似
027 if@H_301_43@(!scene.GetAttribute("name"@H_301_43@).Equals("Assets/StarTrooper.unity"@H_301_43@))
028 @H_301_43@{
029 continue@H_301_43@;
030 @H_301_43@}
031
032 foreach@H_301_43@(XmlElement gameObjects in @H_301_43@scene.ChildNodes)
033 @H_301_43@{
034
035 string @H_301_43@asset = "Prefab/" @H_301_43@ + gameObjects.GetAttribute("name"@H_301_43@);
036 @H_301_43@Vector3 pos = Vector3.zero;
037 @H_301_43@Vector3 rot = Vector3.zero;
038 @H_301_43@Vector3 sca = Vector3.zero;
039 foreach@H_301_43@(XmlElement transform in @H_301_43@gameObjects.ChildNodes)
040 @H_301_43@{
041 foreach@H_301_43@(XmlElement prs in @H_301_43@transform.ChildNodes)
042 @H_301_43@{
043 if@H_301_43@(prs.Name == "position"@H_301_43@)
044 @H_301_43@{
045 foreach@H_301_43@(XmlElement position in @H_301_43@prs.ChildNodes)
046 @H_301_43@{
047 switch@H_301_43@(position.Name)
048 @H_301_43@{
049 case "x"@H_301_43@:
050 @H_301_43@pos.x = float@H_301_43@.Parse(position.InnerText);
051 break@H_301_43@;
052 case "y"@H_301_43@:
053 @H_301_43@pos.y = float@H_301_43@.Parse(position.InnerText);
054 break@H_301_43@;
055 case "z"@H_301_43@:
056 @H_301_43@pos.z = float@H_301_43@.Parse(position.InnerText);
057 break@H_301_43@;
058 @H_301_43@}
059 @H_301_43@}
060 @H_301_43@}else if@H_301_43@(prs.Name == "rotation"@H_301_43@)
061 @H_301_43@{
062 foreach@H_301_43@(XmlElement rotation in @H_301_43@prs.ChildNodes)
063 @H_301_43@{
064 switch@H_301_43@(rotation.Name)
065 @H_301_43@{
066 case "x"@H_301_43@:
067 @H_301_43@rot.x = float@H_301_43@.Parse(rotation.InnerText);
068 break@H_301_43@;
069 case "y"@H_301_43@:
070 @H_301_43@rot.y = float@H_301_43@.Parse(rotation.InnerText);
071 break@H_301_43@;
072 case "z"@H_301_43@:
073 @H_301_43@rot.z = float@H_301_43@.Parse(rotation.InnerText);
074 break@H_301_43@;
075 @H_301_43@}
076 @H_301_43@}
077 @H_301_43@}else if@H_301_43@(prs.Name == "scale"@H_301_43@)
078 @H_301_43@{
079 foreach@H_301_43@(XmlElement scale in @H_301_43@prs.ChildNodes)
080 @H_301_43@{
081 switch@H_301_43@(scale.Name)
082 @H_301_43@{
083 case "x"@H_301_43@:
084 @H_301_43@sca.x = float@H_301_43@.Parse(scale.InnerText);
085 break@H_301_43@;
086 case "y"@H_301_43@:
087 @H_301_43@sca.y = float@H_301_43@.Parse(scale.InnerText);
088 break@H_301_43@;
089 case "z"@H_301_43@:
090 @H_301_43@sca.z = float@H_301_43@.Parse(scale.InnerText);
091 break@H_301_43@;
092 @H_301_43@}
093 @H_301_43@}
094 @H_301_43@}
095 @H_301_43@}
096
097 //拿到 旋转 缩放 平移 以后克隆新游戏对象
098 @H_301_43@GameObject ob = (GameObject)Instantiate(Resources.Load(asset),pos,Quaternion.Euler(rot));
099 @H_301_43@ob.transform.localScale = sca;
100
101 @H_301_43@}
102 @H_301_43@}
103 @H_301_43@}
104 @H_301_43@}
105 @H_301_43@}
106
107 // Update is called once per frame
108 void @H_301_43@Update ()
109 @H_301_43@{
110
111 @H_301_43@}
112
113 void @H_301_43@OnGUI()
114 @H_301_43@{
115 if@H_301_43@(GUI.Button(new @H_301_43@Rect(0,200,200),"XML WORLD"@H_301_43@))
116 @H_301_43@{
117 @H_301_43@Application.LoadLevel("JSONScene"@H_301_43@);
118 @H_301_43@}
119
120 @H_301_43@}
121
122 @H_301_43@}

接着JSON场景中,创建一个空的游戏对象,把JSON.cs挂上去。

01 using @H_301_43@UnityEngine;
02 using @H_301_43@System.Collections;
03 using @H_301_43@System.IO;
04 using @H_301_43@LitJson;
05
06 public class @H_301_43@JSON : MonoBehavIoUr {
07
08 // Use this for initialization
09 void @H_301_43@Start ()
10 @H_301_43@{
11 #if UNITY_EDITOR
12 string @H_301_43@filepath = Application.dataPath +"/StreamingAssets"@H_301_43@+"/json.txt"@H_301_43@;
13 #elif UNITY_IPHONE
14 string @H_301_43@filepath = Application.dataPath +"/Raw"@H_301_43@+"/json.txt"@H_301_43@;
15 #endif
16
17 @H_301_43@StreamReader sr = File.OpenText(filepath);
18 string @H_301_43@strLine = sr.ReadToEnd();
19 @H_301_43@JsonData jd = JsonMapper.ToObject(strLine);
20 @H_301_43@JsonData gameObjectArray = jd["GameObjects"@H_301_43@];
21 int @H_301_43@i,j,k;
22 for @H_301_43@(i = 0; i < gameObjectArray.Count; i++)
23 @H_301_43@{
24 @H_301_43@JsonData senseArray = gameObjectArray[i]["scenes"@H_301_43@];
25 for @H_301_43@(j = 0; j < senseArray.Count; j++)
26 @H_301_43@{
27 string @H_301_43@sceneName = (string@H_301_43@)senseArray[j]["name"@H_301_43@];
28 if@H_301_43@(!sceneName.Equals("Assets/StarTrooper.unity"@H_301_43@))
29 @H_301_43@{
30 continue@H_301_43@;
31 @H_301_43@}
32 @H_301_43@JsonData gameObjects = senseArray[j]["gameObject"@H_301_43@];
33
34 for @H_301_43@(k = 0; k < gameObjects.Count; k++)
35 @H_301_43@{
36 string @H_301_43@objectName = (string@H_301_43@)gameObjects[k]["name"@H_301_43@];
37 string @H_301_43@asset = "Prefab/" @H_301_43@ + objectName;
38 @H_301_43@Vector3 pos = Vector3.zero;
39 @H_301_43@Vector3 rot = Vector3.zero;
40 @H_301_43@Vector3 sca = Vector3.zero;
41
42 @H_301_43@JsonData position = gameObjects[k]["position"@H_301_43@];
43 @H_301_43@JsonData rotation = gameObjects[k]["rotation"@H_301_43@];
44 @H_301_43@JsonData scale = gameObjects[k]["scale"@H_301_43@];
45
46 @H_301_43@pos.x = float@H_301_43@.Parse((string@H_301_43@)position[0]["x"@H_301_43@]);
47 @H_301_43@pos.y = float@H_301_43@.Parse((string@H_301_43@)position[0]["y"@H_301_43@]);
48 @H_301_43@pos.z = float@H_301_43@.Parse((string@H_301_43@)position[0]["z"@H_301_43@]);
49
50 @H_301_43@rot.x = float@H_301_43@.Parse((string@H_301_43@)rotation[0]["x"@H_301_43@]);
51 @H_301_43@rot.y = float@H_301_43@.Parse((string@H_301_43@)rotation[0]["y"@H_301_43@]);
52 @H_301_43@rot.z = float@H_301_43@.Parse((string@H_301_43@)rotation[0]["z"@H_301_43@]);
53
54 @H_301_43@sca.x = float@H_301_43@.Parse((string@H_301_43@)scale[0]["x"@H_301_43@]);
55 @H_301_43@sca.y = float@H_301_43@.Parse((string@H_301_43@)scale[0]["y"@H_301_43@]);
56 @H_301_43@sca.z = float@H_301_43@.Parse((string@H_301_43@)scale[0]["z"@H_301_43@]);
57
58 @H_301_43@GameObject ob = (GameObject)Instantiate(Resources.Load(asset),Quaternion.Euler(rot));
59 @H_301_43@ob.transform.localScale = sca;
60
61 @H_301_43@}
62
63 @H_301_43@}
64 @H_301_43@}
65
66 @H_301_43@}
67
68 // Update is called once per frame
69 void @H_301_43@Update () {
70
71 @H_301_43@}
72
73 void @H_301_43@OnGUI()
74 @H_301_43@{
75 if@H_301_43@(GUI.Button(new @H_301_43@Rect(0,"JSON WORLD"@H_301_43@))
76 @H_301_43@{
77 @H_301_43@Application.LoadLevel("XMLScene"@H_301_43@);
78 @H_301_43@}
79
80 @H_301_43@}
81
82 @H_301_43@}

本例XML和JSON的解析与还原场景,在IOS真实设备上测试通过。

本例的下载地址:http://vdisk.weibo.com/s/k0_DE

雨松MOMO 祝大家学习愉快,准备睡觉,安 。欢迎讨论与学习 嘿嘿。

补充

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

导出场景时增加导出二进制文件选项,代码如下。

01 @H_301_43@[MenuItem ("GameObject/BINARY"@H_301_43@)]
02 static void @H_301_43@XMLJSONTOBinary ()
03 @H_301_43@{
04 string @H_301_43@filepath = Application.dataPath + @"/StreamingAssets/binary.txt"@H_301_43@;
05 if@H_301_43@(File.Exists (filepath))
06 @H_301_43@{
07 @H_301_43@File.Delete(filepath);
08 @H_301_43@}
09 @H_301_43@FileStream fs = new @H_301_43@FileStream(filepath,FileMode.Create);
10 @H_301_43@BinaryWriter bw = new @H_301_43@BinaryWriter(fs);
11 foreach @H_301_43@(UnityEditor.EditorBuildSettingsScene S in @H_301_43@UnityEditor.EditorBuildSettings.scenes)
12 @H_301_43@{
13 if @H_301_43@(S.enabled)
14 @H_301_43@{
15 string @H_301_43@name = S.path;
16 @H_301_43@EditorApplication.OpenScene(name);
17
18 foreach @H_301_43@(GameObject obj in @H_301_43@ Object.FindObjectsOfType(typeof@H_301_43@(GameObject)))
19 @H_301_43@{
20 if @H_301_43@(obj.transform.parent == null@H_301_43@)
21 @H_301_43@{
22 //注解 直接写入字符串
23 @H_301_43@bw.Write(name);
24 @H_301_43@bw.Write(obj.name);
25
26 short @H_301_43@posx = (short@H_301_43@)(obj.transform.position.x * 100);
01 @H_301_43@bw.Write(posx);
02 @H_301_43@bw.Write((short@H_301_43@)(obj.transform.position.y * 100.0f));
03 @H_301_43@bw.Write((short@H_301_43@)(obj.transform.position.z * 100.0f));
04 @H_301_43@bw.Write((short@H_301_43@)(obj.transform.rotation.eulerAngles.x * 100.0f));
05 @H_301_43@bw.Write((short@H_301_43@)(obj.transform.rotation.eulerAngles.y * 100.0f));
06 @H_301_43@bw.Write((short@H_301_43@)(obj.transform.rotation.eulerAngles.z * 100.0f));
07 @H_301_43@bw.Write((short@H_301_43@)(obj.transform.localScale.x * 100.0f));
08 @H_301_43@bw.Write((short@H_301_43@)(obj.transform.localScale.y * 100.0f));
09 @H_301_43@bw.Write((short@H_301_43@)(obj.transform.localScale.z * 100.0f));
10
11 @H_301_43@}
12 @H_301_43@}
13
14 @H_301_43@}
15 @H_301_43@}
16
17 @H_301_43@bw.Flush();
18 @H_301_43@bw.Close();
19 @H_301_43@fs.Close();
20 @H_301_43@}

注解

在写入二进制数据时用到的核心类就是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

001 using @H_301_43@UnityEngine;
002 using @H_301_43@System.Collections;
003 using @H_301_43@System.IO;
004 using @H_301_43@System.Text;
005 using @H_301_43@System;
006 public class @H_301_43@Binary : MonoBehavIoUr
007 @H_301_43@{
008
009 void @H_301_43@Start ()
010 @H_301_43@{
011 string @H_301_43@filepath = Application.dataPath + @"/StreamingAssets/binary.txt"@H_301_43@;
012
013 if@H_301_43@(File.Exists (filepath))
014 @H_301_43@{
015 @H_301_43@FileStream fs = new @H_301_43@FileStream (filepath,FileMode.Open);
016 @H_301_43@BinaryReader br = new @H_301_43@BinaryReader(fs);
017
018 int @H_301_43@index = 0;
019 //将二进制字节流全部读取在这个byte数组当中
020 //ReadBytes传递的参数是一个长度,也就是流的长度
021 byte@H_301_43@[] tempall = br.ReadBytes((int@H_301_43@)fs.Length);
022
023 //开始解析这个字节数组
024 while@H_301_43@(true@H_301_43@)
025 @H_301_43@{
026 //当超过流长度,跳出循环
027 if@H_301_43@(index >= tempall.Length)
028 @H_301_43@{
029 break@H_301_43@;
030 @H_301_43@}
031
032 //得到第一个byte 也就是得到字符串的长度
033 int @H_301_43@scenelength = tempall[index];
034 byte @H_301_43@[]sceneName = new byte @H_301_43@[scenelength];
035 @H_301_43@index += 1;
036 //根据长度拷贝出对应长度的字节数组
037 @H_301_43@System.Array.Copy(tempall,index,sceneName,sceneName.Length);
038 //然后把字节数组对应转换成字符串
039 string @H_301_43@sname = System.Text.Encoding.Default.GetString(sceneName);
040
041 //这里和上面原理一样就不赘述
042 int @H_301_43@objectLength = tempall[index + sceneName.Length];
043 byte @H_301_43@[]objectName = new byte @H_301_43@[objectLength];
044
045 @H_301_43@index += sceneName.Length + 1;
046 @H_301_43@System.Array.Copy(tempall,objectName,objectName.Length);
047 string @H_301_43@oname = System.Text.Encoding.Default.GetString(objectName);
048
049 //下面就是拿short 每一个short的长度是2字节。
050
051 @H_301_43@index += objectName.Length;
052 byte@H_301_43@[] posx = new byte@H_301_43@[2];
053 @H_301_43@System.Array.Copy(tempall,posx,posx.Length);
054 //取得对应的数值 然后 除以100 就是float拉。
055 float @H_301_43@x = System.BitConverter.ToInt16(posx,0) /100.0f;
056
057 //下面都差不多
058 @H_301_43@index += posx.Length;
059 byte@H_301_43@[] posy = new byte@H_301_43@[2];
060 @H_301_43@System.Array.Copy(tempall,posy,posy.Length);
061 float @H_301_43@y = System.BitConverter.ToInt16(posy,0) /100.0f;
062
063 @H_301_43@index += posy.Length;
064 byte@H_301_43@[] posz = new byte@H_301_43@[2];
065 @H_301_43@System.Array.Copy(tempall,posz,posz.Length);
066 float @H_301_43@z = System.BitConverter.ToInt16(posz,0) /100.0f;
067
068 @H_301_43@index += posz.Length;
069 byte@H_301_43@[] rotx = new byte@H_301_43@[2];
070 @H_301_43@System.Array.Copy(tempall,rotx,rotx.Length);
071 float @H_301_43@rx = System.BitConverter.ToInt16(rotx,0) /100.0f;
072
073 @H_301_43@index += rotx.Length;
074 byte@H_301_43@[] roty = new byte@H_301_43@[2];
075 @H_301_43@System.Array.Copy(tempall,roty,roty.Length);
076 float @H_301_43@ry = System.BitConverter.ToInt16(roty,0) /100.0f;
077
078 @H_301_43@index += roty.Length;
079 byte@H_301_43@[] rotz = new byte@H_301_43@[2];
080 @H_301_43@System.Array.Copy(tempall,rotz,rotz.Length);
081 float @H_301_43@rz = System.BitConverter.ToInt16(rotz,0) /100.0f;
082
083 @H_301_43@index += rotz.Length;
084 byte@H_301_43@[] scax = new byte@H_301_43@[2];
085 @H_301_43@System.Array.Copy(tempall,scax,scax.Length);
086 float @H_301_43@sx = System.BitConverter.ToInt16(scax,0) /100.0f;
087
088 @H_301_43@index += scax.Length;
089 byte@H_301_43@[] scay = new byte@H_301_43@[2];
090 @H_301_43@System.Array.Copy(tempall,scay,scay.Length);
091 float @H_301_43@sy = System.BitConverter.ToInt16(scay,0) /100.0f;
092
093 @H_301_43@index += scay.Length;
094 byte@H_301_43@[] scaz = new byte@H_301_43@[2];
095 @H_301_43@System.Array.Copy(tempall,scaz,scaz.Length);
096 float @H_301_43@sz = System.BitConverter.ToInt16(scaz,0) /100.0f;
097
098 @H_301_43@index+=scaz.Length;
099
100 if@H_301_43@(sname.Equals("Assets/StarTrooper.unity"@H_301_43@))
101 @H_301_43@{
102 //最后在这里把场景生成出来
103 string @H_301_43@asset = "Prefab/" @H_301_43@ + oname;
104 @H_301_43@Vector3 pos = new @H_301_43@Vector3 (x,y,z);
105 @H_301_43@Vector3 rot = new @H_301_43@Vector3(rx,ry,rz);
106 @H_301_43@Vector3 sca = new @H_301_43@Vector3(sx,sy,sz);
107 @H_301_43@GameObject ob = (GameObject)Instantiate(Resources.Load(asset),Quaternion.Euler(rot));
108 @H_301_43@ob.transform.localScale = sca;
109 @H_301_43@}
110
111 @H_301_43@}
112 @H_301_43@}
113 @H_301_43@}
114
115 // Update is called once per frame
116 void @H_301_43@Update ()
117 @H_301_43@{
118
119 @H_301_43@}
120 @H_301_43@}

运行一下,场景依然生成的非常完美,在处理二进制解析的时候需要特别注意的就是字节对齐,因为你的所有数据其实就是一个byte[]字节数组,需要有理有序的把字节数组拆分,然后在转换成对应的数据,所以一定要对齐不然肯定会出错的。

最后把代码放出来,晚安 Good Ngith 哇咔咔。

下载地址 :http://vdisk.weibo.com/s/la_QE

留言中刚好有人讨论到这块。另外还有一种方式也可以实现动态增加建立场景,使用.unity 来实现场景的加载,我觉得这种方式可能会更好一些。我在网上已经发现有人写了,那就转载过来吧。

原文地址:http://www.jb51.cc/article/p-hrpeonhx-bmp.html

在Unity3d中,场景(scene)多半通过在build settings中点击add current或者把场景拖进面板实现,假如不这么做,你的场景便不会被加载,哪怕你制定了绝对路径

就是说,一个游戏里要加载多少场景多半都是固定的。

这样的方法会有很多不便,不容易动态加载场景。所以我们今天要说的,是一种动态加载场景的方法

首先,你需要一个编辑器文件,放在editor文件夹下。注意,这个文件不可以继承自monobehavIoUr

1 public class @H_301_43@BuildSceneEditor{
2 @H_301_43@[@MenuItem("build/BuildWebplayerStreamed"@H_301_43@)]
3 static void @H_301_43@Build(){
4 string@H_301_43@[] levels = new string@H_301_43@[]{"Assets/Level1.unity"@H_301_43@,"Assets/Level2.unity"@H_301_43@};
5 @H_301_43@BuildPipeline.BuildStreamedSceneAssetBundle(levels,"streamed.unity3d"@H_301_43@,BuildTarget.WebPlayer);
6 @H_301_43@}
7 @H_301_43@}

这样,在你的unity编辑器上出现了一个按钮,你执行这个按钮,则会在你的Assets同级目录下出现你build好的streamed.unity3d文件,你把这个文件放在服务器上,下面一步就是下载这个文件并build了。

1 @H_301_43@WWW download = WWW.LoadFromCacheOrDownload("http://xxx/streamed.unity3d"@H_301_43@,0);
2 yield return @H_301_43@download;
3 @H_301_43@Application.LoadLevel("Level1"@H_301_43@);

大家注意到了吗。下载好以后就可以直接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来对同类的资源文件进行分包处理。

原文链接:https://www.f2er.com/xml/300293.html

猜你在找的XML相关文章