导出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@H_502_38@ |
using@H_502_38@ UnityEngine;@H_502_38@ |
002@H_502_38@ |
using@H_502_38@ System.Collections;@H_502_38@ |
003@H_502_38@ |
using@H_502_38@ UnityEditor;@H_502_38@ |
004@H_502_38@ |
using@H_502_38@ System.Collections.Generic;@H_502_38@ |
005@H_502_38@ |
using@H_502_38@ System.Xml;@H_502_38@ |
006@H_502_38@ |
using@H_502_38@ System.IO;@H_502_38@ |
007@H_502_38@ |
using@H_502_38@ System.Text;@H_502_38@ |
008@H_502_38@ |
using@H_502_38@ LitJson;@H_502_38@ |
009@H_502_38@ |
public@H_502_38@ class@H_502_38@ MyEditor : Editor@H_502_38@ |
012@H_502_38@ |
@H_502_38@[MenuItem (@H_502_38@"GameObject/ExportXML"@H_502_38@)]@H_502_38@ |
013@H_502_38@ |
@H_502_38@static@H_502_38@ void@H_502_38@ ExportXML ()@H_502_38@ |
015@H_502_38@ |
@H_502_38@string@H_502_38@ filepath = Application.dataPath + @H_502_38@@"/StreamingAssets/my.xml"@H_502_38@;@H_502_38@ |
016@H_502_38@ |
@H_502_38@if@H_502_38@(!File.Exists (filepath))@H_502_38@ |
018@H_502_38@ |
@H_502_38@File.Delete(filepath);@H_502_38@ |
020@H_502_38@ |
@H_502_38@XmlDocument xmlDoc = @H_502_38@new@H_502_38@ XmlDocument();@H_502_38@ |
021@H_502_38@ |
@H_502_38@XmlElement root = xmlDoc.CreateElement(@H_502_38@"gameObjects"@H_502_38@);@H_502_38@ |
023@H_502_38@ |
@H_502_38@foreach@H_502_38@ (UnityEditor.EditorBuildSettingsScene S @H_502_38@in@H_502_38@ UnityEditor.EditorBuildSettings.scenes)@H_502_38@ |
029@H_502_38@ |
@H_502_38@string@H_502_38@ name = S.path;@H_502_38@ |
031@H_502_38@ |
@H_502_38@EditorApplication.OpenScene(name);@H_502_38@ |
032@H_502_38@ |
@H_502_38@XmlElement scenes = xmlDoc.CreateElement(@H_502_38@"scenes"@H_502_38@);@H_502_38@ |
033@H_502_38@ |
@H_502_38@scenes.SetAttribute(@H_502_38@"name"@H_502_38@,name);@H_502_38@ |
034@H_502_38@ |
@H_502_38@foreach@H_502_38@ (GameObject obj @H_502_38@in@H_502_38@ Object.FindObjectsOfType(@H_502_38@typeof@H_502_38@(GameObject)))@H_502_38@ |
036@H_502_38@ |
@H_502_38@if@H_502_38@ (obj.transform.parent == @H_502_38@null@H_502_38@)@H_502_38@ |
038@H_502_38@ |
@H_502_38@XmlElement gameObject = xmlDoc.CreateElement(@H_502_38@"gameObjects"@H_502_38@);@H_502_38@ |
039@H_502_38@ |
@H_502_38@gameObject.SetAttribute(@H_502_38@"name"@H_502_38@,obj.name);@H_502_38@ |
041@H_502_38@ |
@H_502_38@gameObject.SetAttribute(@H_502_38@"asset"@H_502_38@,obj.name + @H_502_38@".prefab"@H_502_38@);@H_502_38@ |
042@H_502_38@ |
@H_502_38@XmlElement transform = xmlDoc.CreateElement(@H_502_38@"transform"@H_502_38@);@H_502_38@ |
043@H_502_38@ |
@H_502_38@XmlElement position = xmlDoc.CreateElement(@H_502_38@"position"@H_502_38@);@H_502_38@ |
044@H_502_38@ |
@H_502_38@XmlElement position_x = xmlDoc.CreateElement(@H_502_38@"x"@H_502_38@);@H_502_38@ |
045@H_502_38@ |
@H_502_38@position_x.InnerText = obj.transform.position.x+@H_502_38@""@H_502_38@;@H_502_38@ |
046@H_502_38@ |
@H_502_38@XmlElement position_y = xmlDoc.CreateElement(@H_502_38@"y"@H_502_38@);@H_502_38@ |
047@H_502_38@ |
@H_502_38@position_y.InnerText = obj.transform.position.y+@H_502_38@""@H_502_38@;@H_502_38@ |
048@H_502_38@ |
@H_502_38@XmlElement position_z = xmlDoc.CreateElement(@H_502_38@"z"@H_502_38@);@H_502_38@ |
049@H_502_38@ |
@H_502_38@position_z.InnerText = obj.transform.position.z+@H_502_38@""@H_502_38@;@H_502_38@ |
050@H_502_38@ |
@H_502_38@position.AppendChild(position_x);@H_502_38@ |
051@H_502_38@ |
@H_502_38@position.AppendChild(position_y);@H_502_38@ |
052@H_502_38@ |
@H_502_38@position.AppendChild(position_z);@H_502_38@ |
054@H_502_38@ |
@H_502_38@XmlElement rotation = xmlDoc.CreateElement(@H_502_38@"rotation"@H_502_38@);@H_502_38@ |
055@H_502_38@ |
@H_502_38@XmlElement rotation_x = xmlDoc.CreateElement(@H_502_38@"x"@H_502_38@);@H_502_38@ |
056@H_502_38@ |
@H_502_38@rotation_x.InnerText = obj.transform.rotation.eulerAngles.x+@H_502_38@""@H_502_38@;@H_502_38@ |
057@H_502_38@ |
@H_502_38@XmlElement rotation_y = xmlDoc.CreateElement(@H_502_38@"y"@H_502_38@);@H_502_38@ |
058@H_502_38@ |
@H_502_38@rotation_y.InnerText = obj.transform.rotation.eulerAngles.y+@H_502_38@""@H_502_38@;@H_502_38@ |
059@H_502_38@ |
@H_502_38@XmlElement rotation_z = xmlDoc.CreateElement(@H_502_38@"z"@H_502_38@);@H_502_38@ |
060@H_502_38@ |
@H_502_38@rotation_z.InnerText = obj.transform.rotation.eulerAngles.z+@H_502_38@""@H_502_38@;@H_502_38@ |
061@H_502_38@ |
@H_502_38@rotation.AppendChild(rotation_x);@H_502_38@ |
062@H_502_38@ |
@H_502_38@rotation.AppendChild(rotation_y);@H_502_38@ |
063@H_502_38@ |
@H_502_38@rotation.AppendChild(rotation_z);@H_502_38@ |
065@H_502_38@ |
@H_502_38@XmlElement scale = xmlDoc.CreateElement(@H_502_38@"scale"@H_502_38@);@H_502_38@ |
066@H_502_38@ |
@H_502_38@XmlElement scale_x = xmlDoc.CreateElement(@H_502_38@"x"@H_502_38@);@H_502_38@ |
067@H_502_38@ |
@H_502_38@scale_x.InnerText = obj.transform.localScale.x+@H_502_38@""@H_502_38@;@H_502_38@ |
068@H_502_38@ |
@H_502_38@XmlElement scale_y = xmlDoc.CreateElement(@H_502_38@"y"@H_502_38@);@H_502_38@ |
069@H_502_38@ |
@H_502_38@scale_y.InnerText = obj.transform.localScale.y+@H_502_38@""@H_502_38@;@H_502_38@ |
070@H_502_38@ |
@H_502_38@XmlElement scale_z = xmlDoc.CreateElement(@H_502_38@"z"@H_502_38@);@H_502_38@ |
071@H_502_38@ |
@H_502_38@scale_z.InnerText = obj.transform.localScale.z+@H_502_38@""@H_502_38@;@H_502_38@ |
073@H_502_38@ |
@H_502_38@scale.AppendChild(scale_x);@H_502_38@ |
074@H_502_38@ |
@H_502_38@scale.AppendChild(scale_y);@H_502_38@ |
075@H_502_38@ |
@H_502_38@scale.AppendChild(scale_z);@H_502_38@ |
077@H_502_38@ |
@H_502_38@transform.AppendChild(position);@H_502_38@ |
078@H_502_38@ |
@H_502_38@transform.AppendChild(rotation);@H_502_38@ |
079@H_502_38@ |
@H_502_38@transform.AppendChild(scale); @H_502_38@ |
081@H_502_38@ |
@H_502_38@gameObject.AppendChild(transform);@H_502_38@ |
082@H_502_38@ |
@H_502_38@scenes.AppendChild(gameObject);@H_502_38@ |
083@H_502_38@ |
@H_502_38@root.AppendChild(scenes);@H_502_38@ |
084@H_502_38@ |
@H_502_38@xmlDoc.AppendChild(root);@H_502_38@ |
085@H_502_38@ |
@H_502_38@xmlDoc.Save(filepath);@H_502_38@ |
092@H_502_38@ |
@H_502_38@AssetDatabase.Refresh();@H_502_38@ |
096@H_502_38@ |
@H_502_38@[MenuItem (@H_502_38@"GameObject/ExportJSON"@H_502_38@)]@H_502_38@ |
097@H_502_38@ |
@H_502_38@static@H_502_38@ void@H_502_38@ ExportJSON ()@H_502_38@ |
099@H_502_38@ |
@H_502_38@string@H_502_38@ filepath = Application.dataPath + @H_502_38@@"/StreamingAssets/json.txt"@H_502_38@;@H_502_38@ |
100@H_502_38@ |
@H_502_38@FileInfo t = @H_502_38@new@H_502_38@ FileInfo(filepath);@H_502_38@ |
101@H_502_38@ |
@H_502_38@if@H_502_38@(!File.Exists (filepath))@H_502_38@ |
103@H_502_38@ |
@H_502_38@File.Delete(filepath);@H_502_38@ |
105@H_502_38@ |
@H_502_38@StreamWriter sw = t.CreateText();@H_502_38@ |
107@H_502_38@ |
@H_502_38@StringBuilder sb = @H_502_38@new@H_502_38@ StringBuilder ();@H_502_38@ |
108@H_502_38@ |
@H_502_38@JsonWriter writer = @H_502_38@new@H_502_38@ JsonWriter (sb);@H_502_38@ |
109@H_502_38@ |
@H_502_38@writer.WriteObjectStart ();@H_502_38@ |
110@H_502_38@ |
@H_502_38@writer.WritePropertyName (@H_502_38@"GameObjects"@H_502_38@);@H_502_38@ |
111@H_502_38@ |
@H_502_38@writer.WriteArrayStart ();@H_502_38@ |
113@H_502_38@ |
@H_502_38@foreach@H_502_38@ (UnityEditor.EditorBuildSettingsScene S @H_502_38@in@H_502_38@ UnityEditor.EditorBuildSettings.scenes)@H_502_38@ |
117@H_502_38@ |
@H_502_38@string@H_502_38@ name = S.path;@H_502_38@ |
118@H_502_38@ |
@H_502_38@EditorApplication.OpenScene(name);@H_502_38@ |
119@H_502_38@ |
@H_502_38@writer.WriteObjectStart();@H_502_38@ |
120@H_502_38@ |
@H_502_38@writer.WritePropertyName(@H_502_38@"scenes"@H_502_38@);@H_502_38@ |
121@H_502_38@ |
@H_502_38@writer.WriteArrayStart ();@H_502_38@ |
122@H_502_38@ |
@H_502_38@writer.WriteObjectStart();@H_502_38@ |
123@H_502_38@ |
@H_502_38@writer.WritePropertyName(@H_502_38@"name"@H_502_38@);@H_502_38@ |
124@H_502_38@ |
@H_502_38@writer.Write(name);@H_502_38@ |
125@H_502_38@ |
@H_502_38@writer.WritePropertyName(@H_502_38@"gameObject"@H_502_38@);@H_502_38@ |
126@H_502_38@ |
@H_502_38@writer.WriteArrayStart ();@H_502_38@ |
128@H_502_38@ |
@H_502_38@foreach@H_502_38@ (GameObject obj @H_502_38@in@H_502_38@ Object.FindObjectsOfType(@H_502_38@typeof@H_502_38@(GameObject)))@H_502_38@ |
130@H_502_38@ |
@H_502_38@if@H_502_38@ (obj.transform.parent == @H_502_38@null@H_502_38@)@H_502_38@ |
132@H_502_38@ |
@H_502_38@writer.WriteObjectStart();@H_502_38@ |
133@H_502_38@ |
@H_502_38@writer.WritePropertyName(@H_502_38@"name"@H_502_38@);@H_502_38@ |
134@H_502_38@ |
@H_502_38@writer.Write(obj.name);@H_502_38@ |
136@H_502_38@ |
@H_502_38@writer.WritePropertyName(@H_502_38@"position"@H_502_38@);@H_502_38@ |
137@H_502_38@ |
@H_502_38@writer.WriteArrayStart ();@H_502_38@ |
138@H_502_38@ |
@H_502_38@writer.WriteObjectStart();@H_502_38@ |
139@H_502_38@ |
@H_502_38@writer.WritePropertyName(@H_502_38@"x"@H_502_38@);@H_502_38@ |
140@H_502_38@ |
@H_502_38@writer.Write(obj.transform.position.x.ToString(@H_502_38@"F5"@H_502_38@));@H_502_38@ |
141@H_502_38@ |
@H_502_38@writer.WritePropertyName(@H_502_38@"y"@H_502_38@);@H_502_38@ |
142@H_502_38@ |
@H_502_38@writer.Write(obj.transform.position.y.ToString(@H_502_38@"F5"@H_502_38@));@H_502_38@ |
143@H_502_38@ |
@H_502_38@writer.WritePropertyName(@H_502_38@"z"@H_502_38@);@H_502_38@ |
144@H_502_38@ |
@H_502_38@writer.Write(obj.transform.position.z.ToString(@H_502_38@"F5"@H_502_38@));@H_502_38@ |
145@H_502_38@ |
@H_502_38@writer.WriteObjectEnd();@H_502_38@ |
146@H_502_38@ |
@H_502_38@writer.WriteArrayEnd();@H_502_38@ |
148@H_502_38@ |
@H_502_38@writer.WritePropertyName(@H_502_38@"rotation"@H_502_38@);@H_502_38@ |
149@H_502_38@ |
@H_502_38@writer.WriteArrayStart ();@H_502_38@ |
150@H_502_38@ |
@H_502_38@writer.WriteObjectStart();@H_502_38@ |
151@H_502_38@ |
@H_502_38@writer.WritePropertyName(@H_502_38@"x"@H_502_38@);@H_502_38@ |
152@H_502_38@ |
@H_502_38@writer.Write(obj.transform.rotation.eulerAngles.x.ToString(@H_502_38@"F5"@H_502_38@));@H_502_38@ |
153@H_502_38@ |
@H_502_38@writer.WritePropertyName(@H_502_38@"y"@H_502_38@);@H_502_38@ |
154@H_502_38@ |
@H_502_38@writer.Write(obj.transform.rotation.eulerAngles.y.ToString(@H_502_38@"F5"@H_502_38@));@H_502_38@ |
155@H_502_38@ |
@H_502_38@writer.WritePropertyName(@H_502_38@"z"@H_502_38@);@H_502_38@ |
156@H_502_38@ |
@H_502_38@writer.Write(obj.transform.rotation.eulerAngles.z.ToString(@H_502_38@"F5"@H_502_38@));@H_502_38@ |
157@H_502_38@ |
@H_502_38@writer.WriteObjectEnd();@H_502_38@ |
158@H_502_38@ |
@H_502_38@writer.WriteArrayEnd();@H_502_38@ |
160@H_502_38@ |
@H_502_38@writer.WritePropertyName(@H_502_38@"scale"@H_502_38@);@H_502_38@ |
161@H_502_38@ |
@H_502_38@writer.WriteArrayStart ();@H_502_38@ |
162@H_502_38@ |
@H_502_38@writer.WriteObjectStart();@H_502_38@ |
163@H_502_38@ |
@H_502_38@writer.WritePropertyName(@H_502_38@"x"@H_502_38@);@H_502_38@ |
164@H_502_38@ |
@H_502_38@writer.Write(obj.transform.localScale.x.ToString(@H_502_38@"F5"@H_502_38@));@H_502_38@ |
165@H_502_38@ |
@H_502_38@writer.WritePropertyName(@H_502_38@"y"@H_502_38@);@H_502_38@ |
166@H_502_38@ |
@H_502_38@writer.Write(obj.transform.localScale.y.ToString(@H_502_38@"F5"@H_502_38@));@H_502_38@ |
167@H_502_38@ |
@H_502_38@writer.WritePropertyName(@H_502_38@"z"@H_502_38@);@H_502_38@ |
168@H_502_38@ |
@H_502_38@writer.Write(obj.transform.localScale.z.ToString(@H_502_38@"F5"@H_502_38@));@H_502_38@ |
169@H_502_38@ |
@H_502_38@writer.WriteObjectEnd();@H_502_38@ |
170@H_502_38@ |
@H_502_38@writer.WriteArrayEnd();@H_502_38@ |
172@H_502_38@ |
@H_502_38@writer.WriteObjectEnd();@H_502_38@ |
176@H_502_38@ |
@H_502_38@writer.WriteArrayEnd();@H_502_38@ |
177@H_502_38@ |
@H_502_38@writer.WriteObjectEnd();@H_502_38@ |
178@H_502_38@ |
@H_502_38@writer.WriteArrayEnd();@H_502_38@ |
179@H_502_38@ |
@H_502_38@writer.WriteObjectEnd();@H_502_38@ |
182@H_502_38@ |
@H_502_38@writer.WriteArrayEnd();@H_502_38@ |
183@H_502_38@ |
@H_502_38@writer.WriteObjectEnd ();@H_502_38@ |
185@H_502_38@ |
@H_502_38@sw.WriteLine(sb.ToString());@H_502_38@ |
186@H_502_38@ |
@H_502_38@sw.Close();@H_502_38@ |
187@H_502_38@ |
@H_502_38@sw.Dispose();@H_502_38@ |
188@H_502_38@ |
@H_502_38@AssetDatabase.Refresh();@H_502_38@ |
OK。此时我们就可以导出游戏场景的信息拉,注意游戏场景的需要现在Project Setting 中注册。点击 GameObject – > Export XML 和 GameObject – > ExportJson 菜单项即可开始生成。
如下图所示,场景导出完毕后,会将xml 与Json 文件保存在StreamingAssets路径下,放在这里的原因是方便移动平台移植,因为它们属于二进制文件,移动平台在读取二进制文件的路径是不一样的。一定要放在这里喔。
接着,我继续创建两个游戏场景,一个用来解析XML的场景,一个用来解析JSON的场景。
XML场景中,创建一个空的游戏对象,把XML.cs挂上去。
001@H_502_38@ |
using@H_502_38@ UnityEngine;@H_502_38@ |
002@H_502_38@ |
using@H_502_38@ System.Collections;@H_502_38@ |
003@H_502_38@ |
using@H_502_38@ System.Xml;@H_502_38@ |
004@H_502_38@ |
using@H_502_38@ System.IO;@H_502_38@ |
005@H_502_38@ |
public@H_502_38@ class@H_502_38@ XML : MonoBehavIoUr {@H_502_38@ |
012@H_502_38@ |
#if UNITY_EDITOR@H_502_38@ |
013@H_502_38@ |
@H_502_38@string@H_502_38@ filepath = Application.dataPath +@H_502_38@"/StreamingAssets"@H_502_38@+@H_502_38@"/my.xml"@H_502_38@;@H_502_38@ |
014@H_502_38@ |
#elif UNITY_IPHONE@H_502_38@ |
015@H_502_38@ |
@H_502_38@string@H_502_38@ filepath = Application.dataPath +@H_502_38@"/Raw"@H_502_38@+@H_502_38@"/my.xml"@H_502_38@;@H_502_38@ |
016@H_502_38@ |
#endif@H_502_38@ |
018@H_502_38@ |
@H_502_38@if@H_502_38@(File.Exists (filepath))@H_502_38@ |
020@H_502_38@ |
@H_502_38@XmlDocument xmlDoc = @H_502_38@new@H_502_38@ XmlDocument();@H_502_38@ |
021@H_502_38@ |
@H_502_38@xmlDoc.Load(filepath);@H_502_38@ |
022@H_502_38@ |
@H_502_38@XmlNodeList nodeList=xmlDoc.SelectSingleNode(@H_502_38@"gameObjects"@H_502_38@).ChildNodes;@H_502_38@ |
023@H_502_38@ |
@H_502_38@foreach@H_502_38@(XmlElement scene @H_502_38@in@H_502_38@ nodeList)@H_502_38@ |
027@H_502_38@ |
@H_502_38@if@H_502_38@(!scene.GetAttribute(@H_502_38@"name"@H_502_38@).Equals(@H_502_38@"Assets/StarTrooper.unity"@H_502_38@))@H_502_38@ |
032@H_502_38@ |
@H_502_38@foreach@H_502_38@(XmlElement gameObjects @H_502_38@in@H_502_38@ scene.ChildNodes)@H_502_38@ |
035@H_502_38@ |
@H_502_38@string@H_502_38@ asset = @H_502_38@"Prefab/"@H_502_38@ + gameObjects.GetAttribute(@H_502_38@"name"@H_502_38@);@H_502_38@ |
036@H_502_38@ |
@H_502_38@Vector3 pos = Vector3.zero;@H_502_38@ |
037@H_502_38@ |
@H_502_38@Vector3 rot = Vector3.zero;@H_502_38@ |
038@H_502_38@ |
@H_502_38@Vector3 sca = Vector3.zero;@H_502_38@ |
039@H_502_38@ |
@H_502_38@foreach@H_502_38@(XmlElement transform @H_502_38@in@H_502_38@ gameObjects.ChildNodes)@H_502_38@ |
041@H_502_38@ |
@H_502_38@foreach@H_502_38@(XmlElement prs @H_502_38@in@H_502_38@ transform.ChildNodes)@H_502_38@ |
043@H_502_38@ |
@H_502_38@if@H_502_38@(prs.Name == @H_502_38@"position"@H_502_38@)@H_502_38@ |
045@H_502_38@ |
@H_502_38@foreach@H_502_38@(XmlElement position @H_502_38@in@H_502_38@ prs.ChildNodes)@H_502_38@ |
047@H_502_38@ |
@H_502_38@switch@H_502_38@(position.Name)@H_502_38@ |
050@H_502_38@ |
@H_502_38@pos.x = @H_502_38@float@H_502_38@.Parse(position.InnerText);@H_502_38@ |
053@H_502_38@ |
@H_502_38@pos.y = @H_502_38@float@H_502_38@.Parse(position.InnerText);@H_502_38@ |
056@H_502_38@ |
@H_502_38@pos.z = @H_502_38@float@H_502_38@.Parse(position.InnerText);@H_502_38@ |
062@H_502_38@ |
@H_502_38@foreach@H_502_38@(XmlElement rotation @H_502_38@in@H_502_38@ prs.ChildNodes)@H_502_38@ |
064@H_502_38@ |
@H_502_38@switch@H_502_38@(rotation.Name)@H_502_38@ |
067@H_502_38@ |
@H_502_38@rot.x = @H_502_38@float@H_502_38@.Parse(rotation.InnerText);@H_502_38@ |
070@H_502_38@ |
@H_502_38@rot.y = @H_502_38@float@H_502_38@.Parse(rotation.InnerText);@H_502_38@ |
073@H_502_38@ |
@H_502_38@rot.z = @H_502_38@float@H_502_38@.Parse(rotation.InnerText);@H_502_38@ |
079@H_502_38@ |
@H_502_38@foreach@H_502_38@(XmlElement scale @H_502_38@in@H_502_38@ prs.ChildNodes)@H_502_38@ |
081@H_502_38@ |
@H_502_38@switch@H_502_38@(scale.Name)@H_502_38@ |
084@H_502_38@ |
@H_502_38@sca.x = @H_502_38@float@H_502_38@.Parse(scale.InnerText);@H_502_38@ |
087@H_502_38@ |
@H_502_38@sca.y = @H_502_38@float@H_502_38@.Parse(scale.InnerText);@H_502_38@ |
090@H_502_38@ |
@H_502_38@sca.z = @H_502_38@float@H_502_38@.Parse(scale.InnerText);@H_502_38@ |
098@H_502_38@ |
@H_502_38@GameObject ob = (GameObject)Instantiate(Resources.Load(asset),pos,Quaternion.Euler(rot));@H_502_38@ |
099@H_502_38@ |
@H_502_38@ob.transform.localScale = sca;@H_502_38@ |
115@H_502_38@ |
@H_502_38@if@H_502_38@(GUI.Button(@H_502_38@new@H_502_38@ Rect(0,200,200),@H_502_38@"XML WORLD"@H_502_38@))@H_502_38@ |
117@H_502_38@ |
@H_502_38@Application.LoadLevel(@H_502_38@"JSONScene"@H_502_38@);@H_502_38@ |
接着JSON场景中,创建一个空的游戏对象,把JSON.cs挂上去。
01@H_502_38@ |
using@H_502_38@ UnityEngine;@H_502_38@ |
02@H_502_38@ |
using@H_502_38@ System.Collections;@H_502_38@ |
03@H_502_38@ |
using@H_502_38@ System.IO;@H_502_38@ |
04@H_502_38@ |
using@H_502_38@ LitJson;@H_502_38@ |
06@H_502_38@ |
public@H_502_38@ class@H_502_38@ JSON : MonoBehavIoUr {@H_502_38@ |
11@H_502_38@ |
#if UNITY_EDITOR@H_502_38@ |
12@H_502_38@ |
@H_502_38@string@H_502_38@ filepath = Application.dataPath +@H_502_38@"/StreamingAssets"@H_502_38@+@H_502_38@"/json.txt"@H_502_38@;@H_502_38@ |
13@H_502_38@ |
#elif UNITY_IPHONE@H_502_38@ |
14@H_502_38@ |
@H_502_38@string@H_502_38@ filepath = Application.dataPath +@H_502_38@"/Raw"@H_502_38@+@H_502_38@"/json.txt"@H_502_38@;@H_502_38@ |
15@H_502_38@ |
#endif @H_502_38@ |
17@H_502_38@ |
@H_502_38@StreamReader sr = File.OpenText(filepath);@H_502_38@ |
18@H_502_38@ |
@H_502_38@string@H_502_38@ strLine = sr.ReadToEnd();@H_502_38@ |
19@H_502_38@ |
@H_502_38@JsonData jd = JsonMapper.ToObject(strLine);@H_502_38@ |
20@H_502_38@ |
@H_502_38@JsonData gameObjectArray = jd[@H_502_38@"GameObjects"@H_502_38@];@H_502_38@ |
22@H_502_38@ |
@H_502_38@for@H_502_38@ (i = 0; i < gameObjectArray.Count; i++)@H_502_38@ |
24@H_502_38@ |
@H_502_38@JsonData senseArray = gameObjectArray[i][@H_502_38@"scenes"@H_502_38@];@H_502_38@ |
25@H_502_38@ |
@H_502_38@for@H_502_38@ (j = 0; j < senseArray.Count; j++)@H_502_38@ |
27@H_502_38@ |
@H_502_38@string@H_502_38@ sceneName = (@H_502_38@string@H_502_38@)senseArray[j][@H_502_38@"name"@H_502_38@];@H_502_38@ |
28@H_502_38@ |
@H_502_38@if@H_502_38@(!sceneName.Equals(@H_502_38@"Assets/StarTrooper.unity"@H_502_38@))@H_502_38@ |
32@H_502_38@ |
@H_502_38@JsonData gameObjects = senseArray[j][@H_502_38@"gameObject"@H_502_38@];@H_502_38@ |
34@H_502_38@ |
@H_502_38@for@H_502_38@ (k = 0; k < gameObjects.Count; k++)@H_502_38@ |
36@H_502_38@ |
@H_502_38@string@H_502_38@ objectName = (@H_502_38@string@H_502_38@)gameObjects[k][@H_502_38@"name"@H_502_38@];@H_502_38@ |
37@H_502_38@ |
@H_502_38@string@H_502_38@ asset = @H_502_38@"Prefab/"@H_502_38@ + objectName;@H_502_38@ |
38@H_502_38@ |
@H_502_38@Vector3 pos = Vector3.zero;@H_502_38@ |
39@H_502_38@ |
@H_502_38@Vector3 rot = Vector3.zero;@H_502_38@ |
40@H_502_38@ |
@H_502_38@Vector3 sca = Vector3.zero;@H_502_38@ |
42@H_502_38@ |
@H_502_38@JsonData position = gameObjects[k][@H_502_38@"position"@H_502_38@];@H_502_38@ |
43@H_502_38@ |
@H_502_38@JsonData rotation = gameObjects[k][@H_502_38@"rotation"@H_502_38@];@H_502_38@ |
44@H_502_38@ |
@H_502_38@JsonData scale = gameObjects[k][@H_502_38@"scale"@H_502_38@];@H_502_38@ |
46@H_502_38@ |
@H_502_38@pos.x = @H_502_38@float@H_502_38@.Parse((@H_502_38@string@H_502_38@)position[0][@H_502_38@"x"@H_502_38@]);@H_502_38@ |
47@H_502_38@ |
@H_502_38@pos.y = @H_502_38@float@H_502_38@.Parse((@H_502_38@string@H_502_38@)position[0][@H_502_38@"y"@H_502_38@]);@H_502_38@ |
48@H_502_38@ |
@H_502_38@pos.z = @H_502_38@float@H_502_38@.Parse((@H_502_38@string@H_502_38@)position[0][@H_502_38@"z"@H_502_38@]);@H_502_38@ |
50@H_502_38@ |
@H_502_38@rot.x = @H_502_38@float@H_502_38@.Parse((@H_502_38@string@H_502_38@)rotation[0][@H_502_38@"x"@H_502_38@]);@H_502_38@ |
51@H_502_38@ |
@H_502_38@rot.y = @H_502_38@float@H_502_38@.Parse((@H_502_38@string@H_502_38@)rotation[0][@H_502_38@"y"@H_502_38@]);@H_502_38@ |
52@H_502_38@ |
@H_502_38@rot.z = @H_502_38@float@H_502_38@.Parse((@H_502_38@string@H_502_38@)rotation[0][@H_502_38@"z"@H_502_38@]);@H_502_38@ |
54@H_502_38@ |
@H_502_38@sca.x = @H_502_38@float@H_502_38@.Parse((@H_502_38@string@H_502_38@)scale[0][@H_502_38@"x"@H_502_38@]);@H_502_38@ |
55@H_502_38@ |
@H_502_38@sca.y = @H_502_38@float@H_502_38@.Parse((@H_502_38@string@H_502_38@)scale[0][@H_502_38@"y"@H_502_38@]);@H_502_38@ |
56@H_502_38@ |
@H_502_38@sca.z = @H_502_38@float@H_502_38@.Parse((@H_502_38@string@H_502_38@)scale[0][@H_502_38@"z"@H_502_38@]);@H_502_38@ |
58@H_502_38@ |
@H_502_38@GameObject ob = (GameObject)Instantiate(Resources.Load(asset),Quaternion.Euler(rot));@H_502_38@ |
59@H_502_38@ |
@H_502_38@ob.transform.localScale = sca;@H_502_38@ |
75@H_502_38@ |
@H_502_38@if@H_502_38@(GUI.Button(@H_502_38@new@H_502_38@ Rect(0,@H_502_38@"JSON WORLD"@H_502_38@))@H_502_38@ |
77@H_502_38@ |
@H_502_38@Application.LoadLevel(@H_502_38@"XMLScene"@H_502_38@);@H_502_38@ |
本例XML和JSON的解析与还原场景,在IOS真实设备上测试通过。
本例的下载地址:http://vdisk.weibo.com/s/k0_DE
雨松MOMO 祝大家学习愉快,准备睡觉,安 。欢迎讨论与学习 嘿嘿。
补充
最近在做客户端与服务器的交互,使用JSON 和XML会感觉数据量太大,影响效率。最后使用二进制的方式来完成。如下图所示,使用二进制可以把空间节省到803K ,是不是很不错呢? 下面我们开始学习如何制作吧。
导出场景时增加导出二进制文件选项,代码如下。
01@H_502_38@ |
@H_502_38@[MenuItem (@H_502_38@"GameObject/BINARY"@H_502_38@)]@H_502_38@ |
02@H_502_38@ |
@H_502_38@static@H_502_38@ void@H_502_38@ XMLJSONTOBinary ()@H_502_38@ |
04@H_502_38@ |
@H_502_38@string@H_502_38@ filepath = Application.dataPath + @H_502_38@@"/StreamingAssets/binary.txt"@H_502_38@;@H_502_38@ |
05@H_502_38@ |
@H_502_38@if@H_502_38@(File.Exists (filepath))@H_502_38@ |
07@H_502_38@ |
@H_502_38@File.Delete(filepath);@H_502_38@ |
09@H_502_38@ |
@H_502_38@FileStream fs = @H_502_38@new@H_502_38@ FileStream(filepath,FileMode.Create);@H_502_38@ |
10@H_502_38@ |
@H_502_38@BinaryWriter bw = @H_502_38@new@H_502_38@ BinaryWriter(fs);@H_502_38@ |
11@H_502_38@ |
@H_502_38@foreach@H_502_38@ (UnityEditor.EditorBuildSettingsScene S @H_502_38@in@H_502_38@ UnityEditor.EditorBuildSettings.scenes)@H_502_38@ |
15@H_502_38@ |
@H_502_38@string@H_502_38@ name = S.path;@H_502_38@ |
16@H_502_38@ |
@H_502_38@EditorApplication.OpenScene(name);@H_502_38@ |
18@H_502_38@ |
@H_502_38@foreach@H_502_38@ (GameObject obj @H_502_38@in@H_502_38@ Object.FindObjectsOfType(@H_502_38@typeof@H_502_38@(GameObject)))@H_502_38@ |
20@H_502_38@ |
@H_502_38@if@H_502_38@ (obj.transform.parent == @H_502_38@null@H_502_38@)@H_502_38@ |
23@H_502_38@ |
@H_502_38@bw.Write(name);@H_502_38@ |
24@H_502_38@ |
@H_502_38@bw.Write(obj.name);@H_502_38@ |
26@H_502_38@ |
@H_502_38@short@H_502_38@ posx = (@H_502_38@short@H_502_38@)(obj.transform.position.x * 100);@H_502_38@ |
01@H_502_38@ |
@H_502_38@bw.Write(posx);@H_502_38@ |
02@H_502_38@ |
@H_502_38@bw.Write((@H_502_38@short@H_502_38@)(obj.transform.position.y * 100.0f));@H_502_38@ |
03@H_502_38@ |
@H_502_38@bw.Write((@H_502_38@short@H_502_38@)(obj.transform.position.z * 100.0f));@H_502_38@ |
04@H_502_38@ |
@H_502_38@bw.Write((@H_502_38@short@H_502_38@)(obj.transform.rotation.eulerAngles.x * 100.0f));@H_502_38@ |
05@H_502_38@ |
@H_502_38@bw.Write((@H_502_38@short@H_502_38@)(obj.transform.rotation.eulerAngles.y * 100.0f));@H_502_38@ |
06@H_502_38@ |
@H_502_38@bw.Write((@H_502_38@short@H_502_38@)(obj.transform.rotation.eulerAngles.z * 100.0f));@H_502_38@ |
07@H_502_38@ |
@H_502_38@bw.Write((@H_502_38@short@H_502_38@)(obj.transform.localScale.x * 100.0f));@H_502_38@ |
08@H_502_38@ |
@H_502_38@bw.Write((@H_502_38@short@H_502_38@)(obj.transform.localScale.y * 100.0f));@H_502_38@ |
09@H_502_38@ |
@H_502_38@bw.Write((@H_502_38@short@H_502_38@)(obj.transform.localScale.z * 100.0f));@H_502_38@ |
注解
在写入二进制数据时用到的核心类就是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@H_502_38@ |
using@H_502_38@ UnityEngine;@H_502_38@ |
002@H_502_38@ |
using@H_502_38@ System.Collections;@H_502_38@ |
003@H_502_38@ |
using@H_502_38@ System.IO;@H_502_38@ |
004@H_502_38@ |
using@H_502_38@ System.Text;@H_502_38@ |
005@H_502_38@ |
using@H_502_38@ System;@H_502_38@ |
006@H_502_38@ |
public@H_502_38@ class@H_502_38@ Binary : MonoBehavIoUr @H_502_38@ |
011@H_502_38@ |
@H_502_38@string@H_502_38@ filepath = Application.dataPath + @H_502_38@@"/StreamingAssets/binary.txt"@H_502_38@;@H_502_38@ |
013@H_502_38@ |
@H_502_38@if@H_502_38@(File.Exists (filepath))@H_502_38@ |
015@H_502_38@ |
@H_502_38@FileStream fs = @H_502_38@new@H_502_38@ FileStream (filepath,FileMode.Open);@H_502_38@ |
016@H_502_38@ |
@H_502_38@BinaryReader br = @H_502_38@new@H_502_38@ BinaryReader(fs);@H_502_38@ |
021@H_502_38@ |
@H_502_38@byte@H_502_38@[] tempall = br.ReadBytes((@H_502_38@int@H_502_38@)fs.Length);@H_502_38@ |
027@H_502_38@ |
@H_502_38@if@H_502_38@(index >= tempall.Length)@H_502_38@ |
033@H_502_38@ |
@H_502_38@int@H_502_38@ scenelength = tempall[index];@H_502_38@ |
034@H_502_38@ |
@H_502_38@byte@H_502_38@ []sceneName = @H_502_38@new@H_502_38@ byte@H_502_38@ [scenelength];@H_502_38@ |
035@H_502_38@ |
@H_502_38@index += 1;@H_502_38@ |
037@H_502_38@ |
@H_502_38@System.Array.Copy(tempall,index,sceneName,sceneName.Length); @H_502_38@ |
039@H_502_38@ |
@H_502_38@string@H_502_38@ sname = System.Text.Encoding.Default.GetString(sceneName);@H_502_38@ |
042@H_502_38@ |
@H_502_38@int@H_502_38@ objectLength = tempall[index + sceneName.Length];@H_502_38@ |
043@H_502_38@ |
@H_502_38@byte@H_502_38@ []objectName = @H_502_38@new@H_502_38@ byte@H_502_38@ [objectLength];@H_502_38@ |
045@H_502_38@ |
@H_502_38@index += sceneName.Length + 1;@H_502_38@ |
046@H_502_38@ |
@H_502_38@System.Array.Copy(tempall,objectName,objectName.Length); @H_502_38@ |
047@H_502_38@ |
@H_502_38@string@H_502_38@ oname = System.Text.Encoding.Default.GetString(objectName);@H_502_38@ |
051@H_502_38@ |
@H_502_38@index += objectName.Length;@H_502_38@ |
053@H_502_38@ |
@H_502_38@System.Array.Copy(tempall,posx,posx.Length);@H_502_38@ |
055@H_502_38@ |
@H_502_38@float@H_502_38@ x = System.BitConverter.ToInt16(posx,0) /100.0f;@H_502_38@ |
058@H_502_38@ |
@H_502_38@index += posx.Length;@H_502_38@ |
060@H_502_38@ |
@H_502_38@System.Array.Copy(tempall,posy,posy.Length); @H_502_38@ |
061@H_502_38@ |
@H_502_38@float@H_502_38@ y = System.BitConverter.ToInt16(posy,0) /100.0f;@H_502_38@ |
063@H_502_38@ |
@H_502_38@index += posy.Length;@H_502_38@ |
065@H_502_38@ |
@H_502_38@System.Array.Copy(tempall,posz,posz.Length); @H_502_38@ |
066@H_502_38@ |
@H_502_38@float@H_502_38@ z = System.BitConverter.ToInt16(posz,0) /100.0f; @H_502_38@ |
068@H_502_38@ |
@H_502_38@index += posz.Length;@H_502_38@ |
070@H_502_38@ |
@H_502_38@System.Array.Copy(tempall,rotx,rotx.Length); @H_502_38@ |
071@H_502_38@ |
@H_502_38@float@H_502_38@ rx = System.BitConverter.ToInt16(rotx,0) /100.0f; @H_502_38@ |
073@H_502_38@ |
@H_502_38@index += rotx.Length;@H_502_38@ |
075@H_502_38@ |
@H_502_38@System.Array.Copy(tempall,roty,roty.Length); @H_502_38@ |
076@H_502_38@ |
@H_502_38@float@H_502_38@ ry = System.BitConverter.ToInt16(roty,0) /100.0f; @H_502_38@ |
078@H_502_38@ |
@H_502_38@index += roty.Length;@H_502_38@ |
080@H_502_38@ |
@H_502_38@System.Array.Copy(tempall,rotz,rotz.Length); @H_502_38@ |
081@H_502_38@ |
@H_502_38@float@H_502_38@ rz = System.BitConverter.ToInt16(rotz,0) /100.0f; @H_502_38@ |
083@H_502_38@ |
@H_502_38@index += rotz.Length;@H_502_38@ |
085@H_502_38@ |
@H_502_38@System.Array.Copy(tempall,scax,scax.Length); @H_502_38@ |
086@H_502_38@ |
@H_502_38@float@H_502_38@ sx = System.BitConverter.ToInt16(scax,0) /100.0f; @H_502_38@ |
088@H_502_38@ |
@H_502_38@index += scax.Length;@H_502_38@ |
090@H_502_38@ |
@H_502_38@System.Array.Copy(tempall,scay,scay.Length); @H_502_38@ |
091@H_502_38@ |
@H_502_38@float@H_502_38@ sy = System.BitConverter.ToInt16(scay,0) /100.0f; @H_502_38@ |
093@H_502_38@ |
@H_502_38@index += scay.Length;@H_502_38@ |
095@H_502_38@ |
@H_502_38@System.Array.Copy(tempall,scaz,scaz.Length); @H_502_38@ |
096@H_502_38@ |
@H_502_38@float@H_502_38@ sz = System.BitConverter.ToInt16(scaz,0) /100.0f; @H_502_38@ |
098@H_502_38@ |
@H_502_38@index+=scaz.Length;@H_502_38@ |
100@H_502_38@ |
@H_502_38@if@H_502_38@(sname.Equals(@H_502_38@"Assets/StarTrooper.unity"@H_502_38@))@H_502_38@ |
103@H_502_38@ |
@H_502_38@string@H_502_38@ asset = @H_502_38@"Prefab/"@H_502_38@ + oname;@H_502_38@ |
104@H_502_38@ |
@H_502_38@Vector3 pos = @H_502_38@new@H_502_38@ Vector3 (x,y,z);@H_502_38@ |
105@H_502_38@ |
@H_502_38@Vector3 rot = @H_502_38@new@H_502_38@ Vector3(rx,ry,rz);@H_502_38@ |
106@H_502_38@ |
@H_502_38@Vector3 sca = @H_502_38@new@H_502_38@ Vector3(sx,sy,sz);@H_502_38@ |
107@H_502_38@ |
@H_502_38@GameObject ob = (GameObject)Instantiate(Resources.Load(asset),Quaternion.Euler(rot));@H_502_38@ |
108@H_502_38@ |
@H_502_38@ob.transform.localScale = sca;@H_502_38@ |
运行一下,场景依然生成的非常完美,在处理二进制解析的时候需要特别注意的就是字节对齐,因为你的所有数据其实就是一个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@H_502_38@ |
public@H_502_38@ class@H_502_38@ BuildSceneEditor{ @H_502_38@ |
2@H_502_38@ |
@H_502_38@[@MenuItem(@H_502_38@"build/BuildWebplayerStreamed"@H_502_38@)] @H_502_38@ |
4@H_502_38@ |
@H_502_38@string@H_502_38@[] levels = @H_502_38@new@H_502_38@ string@H_502_38@[]{@H_502_38@"Assets/Level1.unity"@H_502_38@,@H_502_38@"Assets/Level2.unity"@H_502_38@}; @H_502_38@ |
5@H_502_38@ |
@H_502_38@BuildPipeline.BuildStreamedSceneAssetBundle(levels,@H_502_38@"streamed.unity3d"@H_502_38@,BuildTarget.WebPlayer); @H_502_38@ |
这样,在你的unity编辑器上出现了一个按钮,你执行这个按钮,则会在你的Assets同级目录下出现你build好的streamed.unity3d文件,你把这个文件放在服务器上,下面一步就是下载这个文件并build了。
2@H_502_38@ |
yield@H_502_38@ return@H_502_38@ download; @H_502_38@ |
3@H_502_38@ |
Application.LoadLevel(@H_502_38@"Level1"@H_502_38@);@H_502_38@ |
大家注意到了吗。下载好以后就可以直接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来对同类的资源文件进行分包处理。