您的位置:首页 > 移动开发 > Unity3D

Unity3D 游戏引擎之将场景导出XML或JSON或二进制并且解析还原场景

2015-01-27 11:37 453 查看
导出unity场景的所有游戏对象信息,一种是XML一种是JSON。本篇文章我们把游戏场景中游戏对象的、旋转、缩放、平移与Prefab的名称导出在XML与JSON中。然后解析刚刚导出的XML或JSON通过脚本把导出的游戏场景还原。在Unity官网上下载随便下载一个demo Project,如下图所示这是我刚刚在官网上下载的一个范例程序。



接着将层次视图中的所有游戏对象都封装成Prefab保存在资源路径中,这里注意一下如果你的Prefab绑定的脚本中有public Object 的话 ,需要在代码中改一下。。用 Find() FindTag()这类方法在脚本中Awake()方法中来拿,不然Prefab动态加载的时候无法赋值的,如下图所示,我把封装的Prefab对象都放在了Resources/Prefab文件夹下。



OK,现在我们就需要编写我们的导出工具、在Project视图中创建Editor文件夹,接着创建脚本MyEditor 。如下图所示。



因为编辑的游戏场景数量比较多,导出的时候我们需要遍历所有的游戏场景,一个一个的读取场景信息。然后取得游戏场景中所有游戏对象的Prefab的 名称 旋转 缩放 平移。有关XML的使用请大家看我的上一篇文章: unity3d研究院之使用 C#合成解析XML与JSON(四十一) 代码中我只注释重点的部分,嘿嘿。

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();

  }

  }


原文链接:http://www.unitymanual.com/thread-36576-1-1.html?_dsign=ad7267e6
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: