




已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
导出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.csC#123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190using UnityEngine;using System.Collections;using UnityEditor;using System.Collections.Generic;using System.Xml;using System.IO;using System.Text;using LitJson;public class MyEditor : Editor/将所有游戏场景导出为XML格式MenuItem (GameObject/ExportXML)static void ExportXML ()string filepath = Application.dataPath + /StreamingAssets/my.xml;if(!File.Exists (filepath)File.Delete(filepath);XmlDocument xmlDoc = new XmlDocument();XmlElement root = xmlDoc.CreateElement(gameObjects);/遍历所有的游戏场景foreach (UnityEditor.EditorBuildSettingsScene S in UnityEditor.EditorBuildSettings.scenes) /当关卡启用if (S.enabled) /得到关卡的名称string name = S.path;/打开这个关卡EditorApplication.OpenScene(name);XmlElement scenes = xmlDoc.CreateElement(scenes);scenes.SetAttribute(name,name);foreach (GameObject obj in Object.FindObjectsOfType(typeof(GameObject) if (obj.transform.parent = null) XmlElement gameObject = xmlDoc.CreateElement(gameObjects);gameObject.SetAttribute(name,);gameObject.SetAttribute(asset, + .prefab);XmlElement transform = xmlDoc.CreateElement(transform);XmlElement position = xmlDoc.CreateElement(position);XmlElement position_x = xmlDoc.CreateElement(x);position_x.InnerText = obj.transform.position.x+; XmlElement position_y = xmlDoc.CreateElement(y);position_y.InnerText = obj.transform.position.y+;XmlElement position_z = xmlDoc.CreateElement(z);position_z.InnerText = obj.transform.position.z+;position.AppendChild(position_x);position.AppendChild(position_y);position.AppendChild(position_z);XmlElement rotation = xmlDoc.CreateElement(rotation);XmlElement rotation_x = xmlDoc.CreateElement(x);rotation_x.InnerText = obj.transform.rotation.eulerAngles.x+; XmlElement rotation_y = xmlDoc.CreateElement(y);rotation_y.InnerText = obj.transform.rotation.eulerAngles.y+;XmlElement rotation_z = xmlDoc.CreateElement(z);rotation_z.InnerText = obj.transform.rotation.eulerAngles.z+;rotation.AppendChild(rotation_x);rotation.AppendChild(rotation_y);rotation.AppendChild(rotation_z);XmlElement scale = xmlDoc.CreateElement(scale);XmlElement scale_x = xmlDoc.CreateElement(x);scale_x.InnerText = obj.transform.localScale.x+; XmlElement scale_y = xmlDoc.CreateElement(y);scale_y.InnerText = obj.transform.localScale.y+;XmlElement scale_z = xmlDoc.CreateElement(z);scale_z.InnerText = obj.transform.localScale.z+;scale.AppendChild(scale_x);scale.AppendChild(scale_y);scale.AppendChild(scale_z);transform.AppendChild(position);transform.AppendChild(rotation);transform.AppendChild(scale); gameObject.AppendChild(transform); scenes.AppendChild(gameObject);root.AppendChild(scenes); xmlDoc.AppendChild(root); xmlDoc.Save(filepath); /刷新Project视图, 不然需要手动刷新哦AssetDatabase.Refresh();/将所有游戏场景导出为JSON格式MenuItem (GameObject/ExportJSON)static void ExportJSON ()string filepath = Application.dataPath + /StreamingAssets/json.txt; FileInfo t = new FileInfo(filepath);if(!File.Exists (filepath)File.Delete(filepath);StreamWriter sw = t.CreateText();StringBuilder sb = new StringBuilder ();JsonWriter writer = new JsonWriter (sb);writer.WriteObjectStart ();writer.WritePropertyName (GameObjects);writer.WriteArrayStart ();foreach (UnityEditor.EditorBuildSettingsScene S in UnityEditor.EditorBuildSettings.scenes)if (S.enabled)string name = S.path;EditorApplication.OpenScene(name);writer.WriteObjectStart();writer.WritePropertyName(scenes);writer.WriteArrayStart ();writer.WriteObjectStart();writer.WritePropertyName(name);writer.Write(name);writer.WritePropertyName(gameObject);writer.WriteArrayStart ();foreach (GameObject obj in Object.FindObjectsOfType(typeof(GameObject) if (obj.transform.parent = null) writer.WriteObjectStart();writer.WritePropertyName(name);writer.Write();writer.WritePropertyName(position);writer.WriteArrayStart ();writer.WriteObjectStart();writer.WritePropertyName(x);writer.Write(obj.transform.position.x.ToString(F5);writer.WritePropertyName(y);writer.Write(obj.transform.position.y.ToString(F5);writer.WritePropertyName(z);writer.Write(obj.transform.position.z.ToString(F5);writer.WriteObjectEnd();writer.WriteArrayEnd();writer.WritePropertyName(rotation);writer.WriteArrayStart ();writer.WriteObjectStart();writer.WritePropertyName(x);writer.Write(obj.transform.rotation.eulerAngles.x.ToString(F5);writer.WritePropertyName(y);writer.Write(obj.transform.rotation.eulerAngles.y.ToString(F5);writer.WritePropertyName(z);writer.Write(obj.transform.rotation.eulerAngles.z.ToString(F5);writer.WriteObjectEnd();writer.WriteArrayEnd();writer.WritePropertyName(scale);writer.WriteArrayStart ();writer.WriteObjectStart();writer.WritePropertyName(x);writer.Write(obj.transform.localScale.x.ToString(F5);writer.WritePropertyName(y);writer.Write(obj.transform.localScale.y.ToString(F5);writer.WritePropertyName(z);writer.Write(obj.transform.localScale.z.ToString(F5);writer.WriteObjectEnd();writer.WriteArrayEnd();writer.WriteObjectEnd();writer.WriteArrayEnd();writer.WriteObjectEnd();writer.WriteArrayEnd();writer.WriteObjectEnd();writer.WriteArrayEnd();writer.WriteObjectEnd ();sw.WriteLine(sb.ToString();sw.Close();sw.Dispose();AssetDatabase.Refresh(); 此时我们就可以导出游戏场景的信息拉,注意游戏场景的需要现在Project Setting 中注册。点击 GameObject Export XML 和 GameObject ExportJson 菜单项即可开始生成。 如下图所示,场景导出完毕后,会将xml 与Json 文件保存在StreamingAssets路径下,放在这里的原因是方便移动平台移植,因为它们属于二进制文件,移动平台在读取二进制文件的路径是不一样的。一定要放在这里喔。接着,我继续创建两个游戏场景,一个用来解析XML的场景,一个用来解析JSON的场景。XML场景中,创建一个空的游戏对象,把XML.cs挂上去。C#123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122using UnityEngine;using System.Collections;using System.Xml;using System.IO;public class XML : MonoBehaviour / Use this for initializationvoid Start ()/电脑和iphong上的路径是不一样的,这里用标签判断一下。#if UNITY_EDITORstring filepath = Application.dataPath +/StreamingAssets+/my.xml;#elif UNITY_IPHONEstring filepath = Application.dataPath +/Raw+/my.xml;#endif/如果文件存在话开始解析。if(File.Exists (filepath)XmlDocument xmlDoc = new XmlDocument();xmlDoc.Load(filepath);XmlNodeList nodeList=xmlDoc.SelectSingleNode(gameObjects).ChildNodes;foreach(XmlElement scenein nodeList)/因为我的XML是把所有游戏对象全部导出, 所以这里判断一下只解析需要的场景中的游戏对象/JSON和它的原理类似if(!scene.GetAttribute(name).Equals(Assets/StarTrooper.unity)continue;foreach(XmlElement gameObjects in scene.ChildNodes)string asset = Prefab/ + gameObjects.GetAttribute(name);Vector3 pos = Vector3.zero;Vector3 rot = Vector3.zero;Vector3 sca = Vector3.zero;foreach(XmlElement transform in gameObjects.ChildNodes)foreach(XmlElement prs in transform.ChildNodes)if(prs.Name = position)foreach(XmlElement position in prs.ChildNodes)switch(position.Name)case x:pos.x = float.Parse(position.InnerText);break;case y:pos.y = float.Parse(position.InnerText);break;case z:pos.z = float.Parse(position.InnerText);break;else if(prs.Name = rotation)foreach(XmlElement rotation in prs.ChildNodes)switch(rotation.Name)case x:rot.x = float.Parse(rotation.InnerText);break;case y:rot.y = float.Parse(rotation.InnerText);break;case z:rot.z = float.Parse(rotation.InnerText);break;else if(prs.Name = scale)foreach(XmlElement scale in prs.ChildNodes)switch(scale.Name)case x:sca.x = float.Parse(scale.InnerText);break;case y:sca.y = float.Parse(scale.InnerText);break;case z:sca.z = float.Parse(scale.InnerText);break;/拿到 旋转 缩放 平移 以后克隆新游戏对象GameObject ob = (GameObject)Instantiate(Resources.Load(asset),pos,Quaternion.Euler(rot);ob.transform.localScale = sca;/ Update is called once per framevoid Update ()void OnGUI()if(GUI.Button(new Rect(0,0,200,200),XML WORLD)Application.LoadLevel(JSONScene);接着JSON场景中,创建一个空的游戏对象,把JSON.cs挂上去。C#12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182using UnityEngine;using System.Collections;using System.IO;using LitJson;public class JSON : MonoBehaviour / Use this for initializationvoid Start ()#if UNITY_EDITORstring filepath = Application.dataPath +/StreamingAssets+/json.txt;#elif UNITY_IPHONEstring filepath = Application.dataPath +/Raw+/json.txt;#endif StreamReader sr= File.OpenText(filepath);stringstrLine = sr.ReadToEnd(); JsonData jd = JsonMapper.ToObject(strLine); JsonData gameObjectArray = jdGameObjects;int i,j,k;for (i = 0; i gameObjectArray.Count; i+) JsonData senseArray = gameObjectArrayiscenes; for (j = 0; j senseArray.Count; j+) string scen
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 化工工艺安全与环保要求测试卷
- 我的未来之旅想象作文4篇范文
- 未来的科技幻想想象作文(8篇)
- 家用电器销售平台合作协议
- 跨境贸易合作协议之国际贸易业务合作细节
- go与become的区别与用法:初中语法课教案
- 解除劳动关系正式离职证明(5篇)
- 环境科学水污染治理技术知识梳理
- 中医馆与健康管理体系的融合发展模式
- 高管绿色经历对企业ESG表现的影响
- 2024年北京第二次高中学业水平合格考地理试卷真题(含答案详解)
- 计算机网络与信息安全(2024年版)课件全套 李全龙 第01-10章 计算机网络与信息安全概述- 网络安全协议与技术措施
- 广西桂林市(2024年-2025年小学五年级语文)部编版期末考试(上学期)试卷及答案
- 第八届全国医药行业特有职业技能竞赛(中药调剂员)考试题及答案
- 护士进修手册
- 广东版-开心学英语六年级下册教案
- 传感器技术-武汉大学
- 戏剧鉴赏学习通超星期末考试答案章节答案2024年
- 工程造价咨询服务人员配置方案
- 湿法WFGD、CFB半干法、SDS干法脱硫工艺、控制要点、常见问题
- 催促支付预付款的申请函
评论
0/150
提交评论