unity 保存场景数据,读取场景
·
原文链接:http://www.cnblogs.com/qq2351194611/p/11310159.html
一共两个脚本
一个保存的,一个读取的,下面会附上代码,网上都有,这个只是方便自己用,
这是保存的脚本,放到Editor文件夹下即可
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
// <summary>
/// 保存所有的游戏场景,每个场景的数据都会保存到XML,网上都有,只是方便自己用.
/// </summary>
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;
/// <summary>
/// 动态获取每个场景,
/// </summary>
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");
}
}
}
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐
所有评论(0)