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

为了尽可能加快从网络加载场景,我们通常可以把场景先导出成 XML,把优先级高的资源优先加载并显示(地形等),把可以进入场景之后再加载的对象放到最后(比如场景里面的怪物等),本篇一部分代码引用自:http://www.xuanyusong.com/archives/1919,导出场景部分在原作者的代码基础进行了优化,并且整理成了更加方便,容易使用的类库。

先来搭建测试场景(测试场景来源网络),并整理场景中的对象,如图:

然后把场景中的对象都设置成预设,方便打包成 assetbundle 文件(如何打包预设请查看),如图:

接着我们编写把场景打包成 XML 的代码,取名 ExportSceneToXml.cs,大家可以先看这篇文章(http://www.xuanyusong.com/archives/1919),我在此基础上面进行了优化,全部代码如下:

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

public class ExportSceneToXml : Editor 
{
	[MenuItem("Assets/Export Scene To XML From Selection")]
	static void ExportXML()
	{
		string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "xml");
		if (path.Length != 0) 
		{
			Object[] selectedAssetList = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);
			
			//遍历所有的游戏对象
			foreach (Object selectObject in selectedAssetList) 
			{
				// 场景名称
				string sceneName = selectObject.name;
				// 场景路径
				string scenePath = AssetDatabase.GetAssetPath(selectObject);
				// 场景文件
				//string xmlPath = path; //Application.dataPath + "/AssetBundles/Prefab/Scenes/" + sceneName + ".xml";
				// 如果存在场景文件,删除
				if(File.Exists(path)) File.Delete(path);
				// 打开这个关卡
				EditorApplication.OpenScene(scenePath);

				XmlDocument xmlDocument = new XmlDocument();
				// 创建XML属性
				XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
				xmlDocument.AppendChild(xmlDeclaration);
				// 创建XML根标志
				XmlElement rootXmlElement = xmlDocument.CreateElement("root");
				// 创建场景标志
				XmlElement sceneXmlElement = xmlDocument.CreateElement("scene");
				sceneXmlElement.SetAttribute("sceneName", sceneName);
				
				foreach (GameObject sceneObject in Object.FindObjectsOfType(typeof(GameObject)))
				{
					// 如果对象是激活状态
					if (sceneObject.transform.parent == null && sceneObject.activeSelf)
					{
						// 判断是否是预设
						if(PrefabUtility.GetPrefabType(sceneObject) == PrefabType.PrefabInstance)
						{
							// 获取引用预设对象
							Object prefabObject = EditorUtility.GetPrefabParent(sceneObject);
							if(prefabObject != null)
							{
								XmlElement gameObjectXmlElement = xmlDocument.CreateElement("gameObject");
								gameObjectXmlElement.SetAttribute("objectName", sceneObject.name);
								gameObjectXmlElement.SetAttribute("objectAsset",  prefabObject.name);
								
								XmlElement transformXmlElement = xmlDocument.CreateElement("transform");
								
								// 位置信息
								XmlElement positionXmlElement = xmlDocument.CreateElement("position");
								positionXmlElement.SetAttribute("x", sceneObject.transform.position.x.ToString());
								positionXmlElement.SetAttribute("y", sceneObject.transform.position.y.ToString());
								positionXmlElement.SetAttribute("z", sceneObject.transform.position.z.ToString());
								
								// 旋转信息
								XmlElement rotationXmlElement = xmlDocument.CreateElement("rotation");
								rotationXmlElement.SetAttribute("x", sceneObject.transform.rotation.eulerAngles.x.ToString());
								rotationXmlElement.SetAttribute("y", sceneObject.transform.rotation.eulerAngles.y.ToString());
								rotationXmlElement.SetAttribute("z", sceneObject.transform.rotation.eulerAngles.z.ToString());
								
								// 缩放信息
								XmlElement scaleXmlElement = xmlDocument.CreateElement("scale");
								scaleXmlElement.SetAttribute("x", sceneObject.transform.localScale.x.ToString());
								scaleXmlElement.SetAttribute("y", sceneObject.transform.localScale.y.ToString());
								scaleXmlElement.SetAttribute("z", sceneObject.transform.localScale.z.ToString());
								
								transformXmlElement.AppendChild(positionXmlElement);
								transformXmlElement.AppendChild(rotationXmlElement);
								transformXmlElement.AppendChild(scaleXmlElement);    
								
								gameObjectXmlElement.AppendChild(transformXmlElement);
								sceneXmlElement.AppendChild(gameObjectXmlElement);
							}
						}
					}
				}
				rootXmlElement.AppendChild(sceneXmlElement);
				xmlDocument.AppendChild(rootXmlElement);
				// 保存场景数据
				xmlDocument.Save(path);
				// 刷新Project视图
				AssetDatabase.Refresh();
			}
		}
	}
}

然后我们选中需要打包的场景,选择把场景打包成 XML 的选项,如图:

生成完成,我们可以查看生成出的 XML 内容,如图:

这儿为什么说是对原作者的代码进行了优化,下面我们可以把场景中的一个对象名称改成与预设名称不同,如图:

然后再次导出成 XML 文件,查看 XML 生成的内容我们可以发现,我们可以正确找到预设的名称,如图:

另外,我们还可以选择场景中的哪些文件不用导出,方法很简单,我们可以先把场景中的对象禁用,再导出,如图:

再次查看新导出的 XML 文件,我们会发现 XML 中已经不包括了被禁用对象的配置信息,如图:

以上两点是对原作者代码的优化,而且我也改成了使用右键导出,个人感觉这样更加方便、实用。

现在回到场景中,我们可以把场景里面的对象全部删除,因为场景中已经不需要这些对象了,我们需要通过代码创建这些对象,如图:

下面我们来看如何还原场景,有了 XML,我们解析 XML 就可以了,资源的加载可以看这篇文章(查看详情),加载场景以及预设资源(assetbundle)的代码如下:

using UnityEngine;
using System.Collections.Generic;

public class LoaderScene : MonoBehaviour 
{
	public UISlider progressBar;
	public UILabel lblStatus;

	private string scenePath;
	
	void Awake()
	{
		string prefabPath = "file:///" + Application.dataPath + "/Assets/{0}.assetbundle";

		this.scenePath = "file:///" + Application.dataPath + "/Assets/MainScene.unity3d";

		IList<WwwLoaderPath> pathList = new List<WwwLoaderPath> ();
		pathList.Add (new WwwLoaderPath (this.scenePath, Random.Range (0, 100), WwwLoaderTypeEnum.UNITY_3D));
		pathList.Add (new WwwLoaderPath (string.Format(prefabPath, "Lights"), Random.Range (0, 100), WwwLoaderTypeEnum.ASSET_BUNDLE));
		pathList.Add (new WwwLoaderPath (string.Format(prefabPath, "Particles"), Random.Range (0, 100), WwwLoaderTypeEnum.ASSET_BUNDLE));
		pathList.Add (new WwwLoaderPath (string.Format(prefabPath, "PhysicsCube"), Random.Range (0, 100), WwwLoaderTypeEnum.ASSET_BUNDLE));
		pathList.Add (new WwwLoaderPath (string.Format(prefabPath, "Player"), Random.Range (0, 100), WwwLoaderTypeEnum.ASSET_BUNDLE));
		pathList.Add (new WwwLoaderPath (string.Format(prefabPath, "Stamps"), Random.Range (0, 100), WwwLoaderTypeEnum.ASSET_BUNDLE));
		pathList.Add (new WwwLoaderPath (string.Format(prefabPath, "Statics"), Random.Range (0, 100), WwwLoaderTypeEnum.ASSET_BUNDLE));
		pathList.Add (new WwwLoaderPath (string.Format(prefabPath, "Terrain"), Random.Range (0, 100), WwwLoaderTypeEnum.ASSET_BUNDLE));
		pathList.Add (new WwwLoaderPath (string.Format(prefabPath, "Trees"), Random.Range (0, 100), WwwLoaderTypeEnum.ASSET_BUNDLE));


		this.lblStatus.text = "场景加载中,请稍候。。。";

		WwwLoaderManager.instance.Loader (pathList, onLoaderProgress, onLoaderComplete, "MainScene");
	}

	private void onLoaderProgress(string path, float currentValue, float totalValue)
	{
		this.progressBar.value = currentValue;
	}

	private void onLoaderComplete()
	{
		this.lblStatus.text = "场景正在初始化,请等待。。。";
		Application.LoadLevelAsync("MainScene");
	}
}

然后新建立一个 C# 文件,取名:InitObject.cs,代码如下:

using UnityEngine;
using System.Collections;
using System.Xml;

public class InitObject : MonoBehaviour 
{
	void Awake()
	{
		string xmlPath = Application.dataPath + "/Assets/MainScene.xml";
		string prefabPath = "file:///" + Application.dataPath + "/Assets/{0}.assetbundle";

		XmlDocument xmlDocument = new XmlDocument();
		xmlDocument.Load (xmlPath);
		
		// 使用 XPATH 获取所有 gameObject 节点
		XmlNodeList xmlNodeList = xmlDocument.SelectNodes("//gameObject");
		foreach(XmlNode xmlNode in xmlNodeList)
		{
			string gameObjectName = xmlNode.Attributes["objectName"].Value;
			string prefabName = xmlNode.Attributes["objectAsset"].Value;

			AssetBundle assetBundle = WwwDataManager.instance.GetDataAssetBundle(string.Format(prefabPath, prefabName));
			if(assetBundle != null)
			{
				GameObject assetObject = (GameObject)assetBundle.Load(prefabName, typeof(GameObject));
				if(assetObject != null)
				{
					GameObject gameObject = (GameObject)Instantiate(assetObject);
					// 使用 XPATH 获取 位置、旋转、缩放数据
					XmlNode positionXmlNode = xmlNode.SelectSingleNode("descendant::position");
					XmlNode rotationXmlNode = xmlNode.SelectSingleNode("descendant::rotation");
					XmlNode scaleXmlNode = xmlNode.SelectSingleNode("descendant::scale");
					
					if(positionXmlNode != null && rotationXmlNode != null && scaleXmlNode != null)
					{
						gameObject.transform.position = new Vector3(float.Parse(positionXmlNode.Attributes["x"].Value), float.Parse(positionXmlNode.Attributes["y"].Value), float.Parse(positionXmlNode.Attributes["z"].Value));
						gameObject.transform.rotation = Quaternion.Euler(new Vector3(float.Parse(rotationXmlNode.Attributes["x"].Value), float.Parse(rotationXmlNode.Attributes["y"].Value), float.Parse(rotationXmlNode.Attributes["z"].Value)));
						gameObject.transform.localScale = new Vector3(float.Parse(scaleXmlNode.Attributes["x"].Value), float.Parse(scaleXmlNode.Attributes["y"].Value), float.Parse(scaleXmlNode.Attributes["z"].Value));
					}
				}
				// 卸载引用的加载资源,释放内存
				assetBundle.Unload(false);
			}
		}
		xmlDocument = null;
	}
}

然后我们在空的场景中新建立一个空对象,并且把代码挂载到这个空对象上面,如图:

再然后我们把这个场景打包成 .unity3d 文件,方便从网络上面加载(详情可以查看这篇文章),这样所有的准备工作都已经做好了,全部的配置文件以及资源文件如下:

我们从加载场景运行项目,我们可以先看到依次在加载主场景资源,加载完成之后进入主场景,根据 XML 的内容,原场景被还原了回来,如图:

原文链接:http://www.omuying.com/article/48.aspx

原文地址:https://www.cnblogs.com/snipen/p/4036383.html