【unity】将场景导出XML或JSON或二进制并且解析还原场景
http://www.xuanyusong.com/archives/1919 导出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(四十一)代码中我只注释重点的部分,嘿嘿。 MyEditor.cs C#
OK。此时我们就可以导出游戏场景的信息拉,注意游戏场景的需要现在Project Setting 中注册。点击 GameObject ? > Export XML 和 GameObject ? > ExportJson 菜单项即可开始生成。 如下图所示,场景导出完毕后,会将xml 与Json 文件保存在StreamingAssets路径下,放在这里的原因是方便移动平台移植,因为它们属于二进制文件,移动平台在读取二进制文件的路径是不一样的。一定要放在这里喔。 接着,我继续创建两个游戏场景,一个用来解析XML的场景,一个用来解析JSON的场景。 XML场景中,创建一个空的游戏对象,把XML.cs挂上去。
122 |
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)) xmlDoc.Load(filepath); XmlNodeList nodeList=xmlDoc.SelectSingleNode("gameObjects").ChildNodes; foreach(XmlElement scenein 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); case "z": pos.z = float.Parse(position.InnerText); }else if(prs.Name == "rotation") foreach(XmlElement rotation in prs.ChildNodes) switch(rotation.Name) rot.x = float.Parse(rotation.InnerText); rot.y = float.Parse(rotation.InnerText); rot.z = float.Parse(rotation.InnerText); }else if(prs.Name == "scale") foreach(XmlElement scale in prs.ChildNodes) switch(scale.Name) sca.x = float.Parse(scale.InnerText); sca.y = float.Parse(scale.InnerText); sca.z = float.Parse(scale.InnerText); //拿到 旋转 缩放 平移 以后克隆新游戏对象 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,200,200),"XML WORLD")) Application.LoadLevel("JSONScene"); } |
接着JSON场景中,创建一个空的游戏对象,把JSON.cs挂上去。
82
public class JSON : MonoBehaviour {
string filepath = Application.dataPath +"/StreamingAssets"+"/json.txt";
string filepath = Application.dataPath +"/Raw"+"/json.txt";
#endif
StreamReader sr= File.OpenText(filepath);
stringstrLine = 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"))
JsonData gameObjects = senseArray[j]["gameObject"];
for (k = 0; k < gameObjects.Count; k++)
string objectName = (string)gameObjects[k]["name"];
string asset = "Prefab/" + objectName;
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"]);
}
void Update () {
Application.LoadLevel("XMLScene");
本例XML和JSON的解析与还原场景,在IOS真实设备上测试通过。
本例的下载地址:http://vdisk.weibo.com/s/k0_DE
雨松MOMO 祝大家学习愉快,准备睡觉,安 。欢迎讨论与学习 嘿嘿。
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!