Unity3D研究院之将场景导出XML或JSON或二进制并且解析还原场景
来源:互联网 发布:神通数据库hibernate 编辑:程序博客网 时间:2024/05/05 05:45
转载:http://www.apkbus.com/android-496-1.html
1. 场景导出XML
using UnityEngine;using System.Collections;using UnityEditor;using System.IO;using System.Xml;public class MyEditor : Editor{ [MenuItem("GameObject/Export Scenes To XML")] static void ExportXML() { string filepath = Application.dataPath + "/StreamingAssets" + "/my.xml"; if (File.Exists(filepath)) { File.Delete(filepath); } Debug.Log("exprot scene " + 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; Debug.Log("exprot scene " + name); 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("gameObject"); gameObject.SetAttribute("name", obj.name); gameObject.SetAttribute("asset", obj.name + ".prefab"); XmlElement transform = xmlDoc.CreateElement("transform"); //position XmlElement position = xmlDoc.CreateElement("position"); XmlElement pos_x = xmlDoc.CreateElement("x"); pos_x.InnerText = obj.transform.position.x + ""; XmlElement pos_y = xmlDoc.CreateElement("y"); pos_y.InnerText = obj.transform.position.y + ""; XmlElement pos_z = xmlDoc.CreateElement("z"); pos_z.InnerText = obj.transform.position.z + ""; position.AppendChild(pos_x); position.AppendChild(pos_y); position.AppendChild(pos_z); //rotation XmlElement rotation = xmlDoc.CreateElement("rotation"); XmlElement rot_x = xmlDoc.CreateElement("x"); rot_x.InnerText = obj.transform.rotation.eulerAngles.x + ""; XmlElement rot_y = xmlDoc.CreateElement("y"); rot_y.InnerText = obj.transform.rotation.eulerAngles.y + ""; XmlElement rot_z = xmlDoc.CreateElement("z"); rot_z.InnerText = obj.transform.rotation.eulerAngles.z + ""; rotation.AppendChild(rot_x); rotation.AppendChild(rot_y); rotation.AppendChild(rot_z); //scale; 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); } } AssetDatabase.Refresh(); Debug.Log("export success"); }}
2,导入代码。创建一个空gameobject,绑定LoadXmlScene.cs即可
using UnityEngine;using System.Collections;using System.IO;using System.Xml;public class LoadXmlScene : MonoBehaviour { public GameObject prefab;// Use this for initializationvoid Start () { loadScene();}// Update is called once per framevoid Update () {} private void loadScene() {#if UNITY_EDITOR string filepath = Application.dataPath + "/StreamingAssets/my.xml";#elif UNITY_IPHONE string filepath = Application.dataPath + "/Raw" + "my.xml";#endif Debug.Log("filepath " + filepath); if (File.Exists(filepath)) { Debug.Log("filepath " + filepath); XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(filepath); XmlNodeList nodelist = xmldoc.SelectSingleNode("gameObjects").ChildNodes; foreach (XmlElement scene in nodelist) { Debug.Log("scene " + scene.Name ); foreach (XmlElement gameobject in scene.ChildNodes) { string asset = "Prefabs/" + gameobject.GetAttribute("name"); Vector3 pos = Vector3.zero; Vector3 rot = Vector3.zero; Vector3 scale = Vector3.zero; foreach (XmlElement transform in gameobject.ChildNodes) { foreach (XmlElement node in transform.ChildNodes) { if (node.Name == "position") { pos = getVector3(node); } else if (node.Name == "rotation") { rot = getVector3(node); } else if( node.Name == "scale") { scale = getVector3(node); } } } GameObject obj = (GameObject)Instantiate(Resources.Load(asset), pos, Quaternion.Euler(rot)); obj.transform.localScale = scale; } } Debug.Log("Load over!"); } } private Vector3 getVector3(XmlElement transform ) { Vector3 vec = Vector3.zero; foreach (XmlElement node in transform.ChildNodes) { switch (node.Name) { case "x": vec.x = float.Parse(node.InnerText); break; case "y": vec.y = float.Parse(node.InnerText); break; case "z": vec.z = float.Parse(node.InnerText); break; } } return vec; }}
0 0
- Unity3D研究院之将场景导出XML或JSON或二进制并且解析还原场景
- Unity3D研究院之将场景导出XML或JSON或二进制并且解析还原场景《二》
- Unity3D研究院之将场景导出XML或JSON或二进制并且解析还原场景(四十二)
- Unity3D研究院之将场景导出XML或JSON或二进制并且解析还原场景(四十二)
- Unity3D研究院之将场景导出XML或JSON或二进制并且解析还原场景(四十二)
- Unity3D 游戏引擎之将场景导出XML或JSON或二进制并且解析还原场景
- Unity3D研究院之将场景导出XML或JSON或二进制并且解析还原场景(四十二)(转自雨松MOMO)
- Unity3D将场景导出XML或JSON或二进制并且解析还原场景(四十二)
- Unity3D 将场景导出XML或JSON或二进制并且解析还原场景
- unity3d场景导出XML或JSON并且解析还原场景
- 将场景导出XML或JSON或二进制并且解析还原场景
- 将场景导出XML或JSON或二进制并且解析还原场景
- 将场景导出XML或JSON并且解析还原场景
- Unity3D 场景导出成 XML或JSON 并解析还原场景
- 将场景导出二进制文件并且解析还原场景
- Unity3D之场景导出(json格式或xml格式)--(1)
- Unity3D研究院之使用 C#合成解析XML与JSON
- Unity3D研究院之使用 C#合成解析XML与JSON
- 只有仅限于文件系统的 SWF 文件和可信的本地 SWF 文件可以访问本地资源
- 跨机器登陆mysql-ssh隧道
- php switch语句多个值匹配同一代码块
- Java版图片与base64位编码互转
- 求高精度幂 POJ1001
- Unity3D研究院之将场景导出XML或JSON或二进制并且解析还原场景
- R语言学习笔记——基础
- 类、对象、抽象类、接口和内部类存在的意义
- Java 类加载器
- PostgreSQL 返回多个结果集
- Java自带的性能监测工具用法简介——jstack、jconsole、jinfo、jmap、jdb、jsta、jvisualvm
- Android MediaPlayer
- java 的接口到底起什么作用
- __block typeof(self) bself = self;