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

本文介绍如何使用Unity导出场景为XML和JSON格式,以及如何通过代码将导出的XML或JSON文件还原为场景。同时,文章还讨论了二进制文件的导出方法以减少数据量。


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

接着我们编写把场景打包成 XML 的代码,取名 ExportSceneToXml.cs,我在此基础上面进行了优化,全部代码如下:

复制代码

代码如下:

</font>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 就可以了,资源的加载可以看这篇文章(查看详情),加载场景以及预设资源(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;
}
}


导出Unity场景的所有游戏对象信息,一种是XML一种是JSON。本篇文章我们把游戏场景中游戏对象的、旋转、缩放、平移与Prefab的名称导出在XML与JSON中。然后解析刚刚导出的XML或JSON通过脚本把导出的游戏场景还原。将层次视图中的所有游戏对象都封装成Prefab保存在资源路径中,这里注意一下如果你的Prefab绑定的脚本中有public Object 的话 ,需要在代码中改一下。。用 Find() FindTag()这类方法在脚本中Awake()方法中来拿,不然Prefab动态加载的时候无法赋值的,如下图所示,我把封装的Prefab对象都放在了Resources/Prefab文件夹下。

using 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",obj.name);
 
gameObject.SetAttribute("asset",obj.name + ".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(obj.name);
 
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();
}
}

场景导出完毕后,会将xml 与Json 文件保存在StreamingAssets路径下,放在这里的原因是方便移动平台移植,因为它们属于二进制文件,移动平台在读取二进制文件的路径是不一样的。我继续创建两个游戏场景,一个用来解析XML的场景,一个用来解析JSON的场景。 

XML场景中,创建一个空的游戏对象,把XML.cs挂上去。

using UnityEngine;
using System.Collections;
using System.Xml;
using System.IO;
public class XML : MonoBehaviour {
 
// Use this for initialization
void Start ()
{
 
//电脑和iphong上的路径是不一样的,这里用标签判断一下。
#if UNITY_EDITOR
string filepath = Application.dataPath +"/StreamingAssets"+"/my.xml";
#elif UNITY_IPHONE
  string 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 scene  in 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 frame
void Update ()
{
 
}
 
void OnGUI()
{
if(GUI.Button(new Rect(0,0,200,200),"XML WORLD"))
{
Application.LoadLevel("JSONScene");
}
 
}
 
}
接着JSON场景中,创建一个空的游戏对象,把JSON.cs挂上去。
using UnityEngine;
using System.Collections;
using System.IO;
using LitJson;
 
public class JSON : MonoBehaviour {
 
// Use this for initialization
void Start ()
{
#if UNITY_EDITOR
  string filepath = Application.dataPath +"/StreamingAssets"+"/json.txt";
#elif UNITY_IPHONE
  string filepath = Application.dataPath +"/Raw"+"/json.txt";
#endif
 
StreamReader sr  = File.OpenText(filepath);
string  strLine = sr.ReadToEnd();
   JsonData jd = JsonMapper.ToObject(strLine);
   JsonData gameObjectArray = jd["GameObjects"];
int i,j,k;
for (i = 0; i < gameObjectArray.Count; i++)
{
   JsonData senseArray = gameObjectArray[i]["scenes"];
   for (j = 0; j < senseArray.Count; j++)
      {
string sceneName = (string)senseArray[j]["name"];
if(!sceneName.Equals("Assets/StarTrooper.unity"))
{
continue;
}
JsonData gameObjects = senseArray[j]["gameObject"];
 
for (k = 0; k < gameObjects.Count; k++)
{
string objectName = (string)gameObjects[k]["name"];
string asset = "Prefab/" + objectName;
Vector3 pos = Vector3.zero;
Vector3 rot = Vector3.zero;
Vector3 sca = Vector3.zero;
 
JsonData position = gameObjects[k]["position"];
JsonData rotation = gameObjects[k]["rotation"];
JsonData scale = gameObjects[k]["scale"];
 
pos.x = float.Parse((string)position[0]["x"]);
pos.y = float.Parse((string)position[0]["y"]);
pos.z = float.Parse((string)position[0]["z"]);
 
rot.x = float.Parse((string)rotation[0]["x"]);
rot.y = float.Parse((string)rotation[0]["y"]);
rot.z = float.Parse((string)rotation[0]["z"]);
 
sca.x = float.Parse((string)scale[0]["x"]);
sca.y = float.Parse((string)scale[0]["y"]);
sca.z = float.Parse((string)scale[0]["z"]);
 
GameObject ob = (GameObject)Instantiate(Resources.Load(asset),pos,Quaternion.Euler(rot));
ob.transform.localScale = sca;
 
}
 
   }
}
 
}
 
// Update is called once per frame
void Update () {
 
}
 
void OnGUI()
{
if(GUI.Button(new Rect(0,0,200,200),"JSON WORLD"))
{
Application.LoadLevel("XMLScene");
}
 
}
 
}
客户端与服务器的交互,使用JSON 和XML会感觉数据量太大,影响效率。最后使用二进制的方式来完成。如下图所示,使用二进制可以把空间节省到803K ,是不是很不错呢? 下面我们开始学习如何制作吧。导出场景时增加导出二进制文件选项,代码如下。

[MenuItem ("GameObject/BINARY")]

static void XMLJSONTOBinary ()
{
string filepath = Application.dataPath + @"/StreamingAssets/binary.txt";
if(File.Exists (filepath))
{
File.Delete(filepath);
}
FileStream  fs = new FileStream(filepath, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
foreach (UnityEditor.EditorBuildSettingsScene S in UnityEditor.EditorBuildSettings.scenes)
        {
            if (S.enabled)
            {
string name = S.path;
EditorApplication.OpenScene(name);
 
foreach (GameObject obj in Object.FindObjectsOfType(typeof(GameObject)))
{
     if (obj.transform.parent == null)
     {
//注解 直接写入字符串
bw.Write(name);
bw.Write(obj.name);
 
short posx = (short)(obj.transform.position.x * 100);





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值