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
原创粉丝点击