博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
unity 保存场景数据,读取场景
阅读量:4692 次
发布时间:2019-06-09

本文共 9540 字,大约阅读时间需要 31 分钟。

一共两个脚本

一个保存的,一个读取的,下面会附上代码,网上都有,这个只是方便自己用,

这是保存的脚本,放到Editor文件夹下即可

using System.Collections;using System.Collections.Generic;using System.IO;using System.Xml;using UnityEditor;using UnityEditor.SceneManagement;using UnityEngine;// /// 保存所有的游戏场景,每个场景的数据都会保存到XML,网上都有,只是方便自己用./// public class ExportSceneInfoToXML : Editor {    [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();    }}

 

这是读取场景的脚本,挂在场景的空物体上即可

using System.Collections;using System.Collections.Generic;using System.IO;using System.Xml;using UnityEngine;using UnityEngine.SceneManagement;/// /// 动态获取每个场景,/// 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;                    }                }            }        }    }      void OnGUI()    {        if (GUI.Button(new Rect(0, 0, 200, 200), "XML WORLD"))        {           // Application.LoadLevel("JSONScene");            SceneManager.LoadScene("1");        }        if (GUI.Button(new Rect(0, 200, 500, 200), "XML skfnakgn"))   //这几个按钮只是测试,一个就行了,不需要可以删除,亲们自己测试        {            // Application.LoadLevel("JSONScene");            SceneManager.LoadScene("SampleScene");        }        if (GUI.Button(new Rect(0, 400, 500, 200), "XML dfdf"))        {            Application.LoadLevel("1234");           // SceneManager.LoadScene("1234");        }    }}

 

转载于:https://www.cnblogs.com/qq2351194611/p/11310159.html

你可能感兴趣的文章
入门Webpack,看这篇就够了
查看>>
短信拦截马”黑色产业链与溯源取证研究
查看>>
Mac Xdebug安装时遇到了Zend Engine API 不一致的问题
查看>>
最小公倍数
查看>>
asp.net如何定时执行任务
查看>>
在github上实现页面托管预览功能
查看>>
css选择器
查看>>
prim
查看>>
给陌生人写一封信
查看>>
noip2013花匠
查看>>
[CF]Equalize Them All
查看>>
React Ant design table表单与pagination分页配置
查看>>
重大发现: windows下C++ UI库 UI神器-SOUI(转载)
查看>>
linux 压缩文件的命令总结
查看>>
linux tail 命令详解
查看>>
BZOJ-3207 花神的嘲讽计划Ⅰ
查看>>
BZOJ-1069 [SCOI2007]最大土地面积
查看>>
进程与线程的一个简单解释【摘】
查看>>
COJ976 WZJ的数据结构(负二十四)
查看>>
slid.es – 创建在线幻灯片和演示文稿的最佳途径
查看>>