unity3D技术之导出Unity场景的所有游戏对象信息.doc

上传人:灯火****19 文档编号:134959318 上传时间:2020-06-10 格式:DOC 页数:12 大小:218.88KB
返回 下载 相关 举报
unity3D技术之导出Unity场景的所有游戏对象信息.doc_第1页
第1页 / 共12页
unity3D技术之导出Unity场景的所有游戏对象信息.doc_第2页
第2页 / 共12页
unity3D技术之导出Unity场景的所有游戏对象信息.doc_第3页
第3页 / 共12页
unity3D技术之导出Unity场景的所有游戏对象信息.doc_第4页
第4页 / 共12页
unity3D技术之导出Unity场景的所有游戏对象信息.doc_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《unity3D技术之导出Unity场景的所有游戏对象信息.doc》由会员分享,可在线阅读,更多相关《unity3D技术之导出Unity场景的所有游戏对象信息.doc(12页珍藏版)》请在金锄头文库上搜索。

1、 导出Unity场景的所有游戏对象信息,一种是XML一种是JSON。本篇文章我们把游戏场景中游戏对象的、旋转、缩放、平移与Prefab的名称导出在XML与JSON中。然后解析刚刚导出的XML或JSON通过脚本把导出的游戏场景还原。在Unity官网上下载随便下载一个demo Project,如下图所示这是我刚刚在官网上下载的一个范例程序。 接着将层次视图中的所有游戏对象都封装成Prefab保存在资源路径中,这里注意一下如果你的Prefab绑定的脚本中有public Object 的话 ,需要在代码中改一下。用 Find() FindTag()这类方法在脚本中Awake()方法中来拿,不然Pref

2、ab动态加载的时候无法赋值的,如下图所示,我把封装的Prefab对象都放在了Resources/Prefab文件夹下。 OK,现在我们就需要编写我们的导出工具、在Project视图中创建Editor文件夹,接着创建脚本MyEditor 。如下图所示。 因为编辑的游戏场景数量比较多,导出的时候我们需要遍历所有的游戏场景,一个一个的读取场景信息。然后取得游戏场景中所有游戏对象的Prefab的 名称 旋转 缩放 平移。有关XML的使用请大家看我的上一篇文章:Unity3D研究院之使用 C#合成解析XML与JSON(四十一)代码中我只注释重点的部分,嘿嘿。MyEditor.csC#1234567891

3、011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391

4、40141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190using UnityEngine;using System.Collections;using UnityEditor;using System.Collections.Generic;using System.Xml;using System.IO;using System.Text;usi

5、ng 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.CreateEleme

6、nt(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 (Gam

7、eObject obj in Object.FindObjectsOfType(typeof(GameObject) if (obj.transform.parent = null) XmlElement gameObject = xmlDoc.CreateElement(gameObjects);gameObject.SetAttribute(name,obj.name);gameObject.SetAttribute(asset,obj.name + .prefab);XmlElement transform = xmlDoc.CreateElement(transform);XmlEle

8、ment 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

9、.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+;

10、 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.Append

11、Child(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);sca

12、le_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.AppendChi

13、ld(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.WriteObje

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 幼儿/小学教育 > 幼儿教育

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号