unity场景的结构和物体的属性导出xml文件

SceneXML.cs  这个脚本不用给物体上挂,它会生成按钮。

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Xml;
using System.IO;
using System.Text;
using System;

public class SceneXML : Editor
{
	// 写当前obj,node的信息  ok!
    static void ExportGameObjectAsXML(XmlDocument xmlDoc, GameObject objParent, XmlElement nodeParent)
    {
        // 0. 将当前obj转化为预制体并保存到指定位置。注意:不要把子节点的内容包括进来
        // 不着急。先手工存成预制体,然后一个个处理

        // 1. 写入node的属性
        nodeParent.SetAttribute("name", objParent.name);                    // node Name
        nodeParent.SetAttribute("ID", objParent.name);                      //node ID
        nodeParent.SetAttribute("FBX", objParent.name);      //node Prefab

                float objposition_x = objParent.GetComponent<Transform> ().position.x;
		float objposition_y = objParent.GetComponent<Transform> ().position.y;
		float objposition_z = objParent.GetComponent<Transform> ().position.z;
        nodeParent.SetAttribute ("Position", objposition_x + " " + objposition_y + " " + objposition_z);  //Position

		float objrotation_x = objParent.GetComponent<Transform> ().eulerAngles.x;
		float objrotation_y = objParent.GetComponent<Transform> ().eulerAngles.y;
		float objrotation_z = objParent.GetComponent<Transform> ().eulerAngles.z;
        nodeParent.SetAttribute ("Rotation", objrotation_x + " " + objrotation_y + " " + objrotation_z);  //Rotation

		float objscale_x = objParent.GetComponent<Transform> ().localScale.x;
		float objscale_y = objParent.GetComponent<Transform> ().localScale.y;
		float objscale_z = objParent.GetComponent<Transform> ().localScale.z;
        nodeParent.SetAttribute ("Scale", objscale_x + " " + objscale_y + " " + objscale_z);  //Scale

        // 2. 遍历obj的子节点,写入相关node
		for (int i = 0; i < objParent.transform.childCount; i++) 
		{
			GameObject childObj = objParent.transform.GetChild (i).gameObject;
            string nodeName = "Part";
            if (childObj.gameObject.transform.childCount > 0)
                nodeName = "Assembly";

			XmlElement childNode = xmlDoc.CreateElement (nodeName);
            nodeParent.AppendChild(childNode);	
			ExportGameObjectAsXML (xmlDoc, childObj, childNode);
		}
    }

    //将所有游戏场景导出为XML格式 ok!
    [MenuItem("ProcessPlan/ExportScene")]
    static void ExportXML()
    {
		if (Selection.activeGameObject) 
		{
			// 1. 选择存储xml文件的路径
			string filepath = Application.dataPath + @"/StreamingAssets/SimpleScene.xml";  
			if (!File.Exists (filepath)) {
				File.Delete (filepath);
			}
			// 2. 定义xml文档
			XmlDocument xmlDoc = new XmlDocument ();                                    // XML Define
			XmlDeclaration dec = xmlDoc.CreateXmlDeclaration ("1.0", "utf-8", null);    // XML Declaration
			xmlDoc.AppendChild (dec);

			// 3. 定义xml的根节点
			XmlElement root = xmlDoc.CreateElement ("World");           // Root note 
			root.SetAttribute ("Speed", "20.1245215500000010");         // ??
			root.SetAttribute ("Delay", "30000.0000000000000000");      // ??
			xmlDoc.AppendChild (root);

            // 4. 定义当前选中的场景节点
			GameObject sceneObj = Selection.activeGameObject;           //找到选中节点/高亮节点
			XmlElement sceneNode = xmlDoc.CreateElement("Scene");       // 定义场景节点
            root.AppendChild(sceneNode);
            ExportGameObjectAsXML(xmlDoc, sceneObj, sceneNode);         //写入note信息
            // 5.保存
            xmlDoc.Save (filepath);
			// 6.刷新与声明
			AssetDatabase.Refresh ();
			Debug.Log ("XML file has been generated!");
		}
		else
		{
			Debug.Log ("You must select one target!");
		}
    }


}


导出的xml文件

<?xml version="1.0" encoding="utf-8"?>
<World Speed="20.1245215500000010" Delay="30000.0000000000000000">
  <Scene name="Scene" ID="Scene" FBX="Scene" Position="0 0 -0.688" Rotation="0 0 0" Scale="0.5 0.5 0.5">
    <Assembly name="Cube" ID="Cube" FBX="Cube" Position="0 -0.28 0" Rotation="0 0 0" Scale="6 0.1 6">
      <Part name="Cube2" ID="Cube2" FBX="Cube2" Position="1.089 0 -0.363" Rotation="0 0 0" Scale="0.1666667 10 0.1666667" />
      <Part name="Cube1" ID="Cube1" FBX="Cube1" Position="0 0 -0.363" Rotation="0 0 0" Scale="0.1666667 10 0.1666667" />
    </Assembly>
    <Part name="Cube3" ID="Cube3" FBX="Cube3" Position="0 0 1.394" Rotation="0 0 0" Scale="1 1 1" />
    <Part name="Cube4" ID="Cube4" FBX="Cube4" Position="0.646 -0.2695 0.3695" Rotation="0 0 0" Scale="1 1 1" />
  </Scene>
</World>

Unity3D 场景导出成 XML 并解析还原场景。


  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值