为了尽可能加快从网络加载场景,我们通常可以把场景先导出成 XML,把优先级高的资源优先加载并显示(地形等),把可以进入场景之后再加载的对象放到最后(比如场景里面的怪物等)导出场景部分在原作者的代码基础进行了优化,并且整理成了更加方便,容易使用的类库。
接着我们编写把场景打包成 XML 的代码,取名 ExportSceneToXml.cs,我在此基础上面进行了优化,全部代码如下:
复制代码
代码如下:
</font>using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.IO;
using System.Text;
public class ExportSceneToXml : Editor
{
[MenuItem("Assets/Export Scene To XML From Selection")]
static void ExportXML()
{
string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "xml");
if (path.Length != 0)
{
Object[] selectedAssetList = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);
//遍历所有的游戏对象
foreach (Object selectObject in selectedAssetList)
{
// 场景名称
string sceneName = selectObject.name;
// 场景路径
string scenePath = AssetDatabase.GetAssetPath(selectObject);
// 场景文件
//string xmlPath = path; //Application.dataPath + "/AssetBundles/Prefab/Scenes/" + sceneName + ".xml";
// 如果存在场景文件,删除
if(File.Exists(path)) File.Delete(path);
// 打开这个关卡
EditorApplication.OpenScene(scenePath);
XmlDocument xmlDocument = new XmlDocument();
// 创建XML属性
XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
xmlDocument.AppendChild(xmlDeclaration);
// 创建XML根标志
XmlElement rootXmlElement = xmlDocument.CreateElement("root");
// 创建场景标志
XmlElement sceneXmlElement = xmlDocument.CreateElement("scene");
sceneXmlElement.SetAttribute("sceneName", sceneName);
foreach (GameObject sceneObject in Object.FindObjectsOfType(typeof(GameObject)))
{
// 如果对象是激活状态
if (sceneObject.transform.parent == null && sceneObject.activeSelf)
{
// 判断是否是预设
if(PrefabUtility.GetPrefabType(sceneObject) == PrefabType.PrefabInstance)
{
// 获取引用预设对象
Object prefabObject = EditorUtility.GetPrefabParent(sceneObject);
if(prefabObject != null)
{
XmlElement gameObjectXmlElement = xmlDocument.CreateElement("gameObject");
gameObjectXmlElement.SetAttribute("objectName", sceneObject.name);
gameObjectXmlElement.SetAttribute("objectAsset", prefabObject.name);
XmlElement transformXmlElement = xmlDocument.CreateElement("transform");
// 位置信息
XmlElement positionXmlElement = xmlDocument.CreateElement("position");
positionXmlElement.SetAttribute("x", sceneObject.transform.position.x.ToString());
positionXmlElement.SetAttribute("y", sceneObject.transform.position.y.ToString());
positionXmlElement.SetAttribute("z", sceneObject.transform.position.z.ToString());
// 旋转信息
XmlElement rotationXmlElement = xmlDocument.CreateElement("rotation");
rotationXmlElement.SetAttribute("x", sceneObject.transform.rotation.eulerAngles.x.ToString());
rotationXmlElement.SetAttribute("y", sceneObject.transform.rotation.eulerAngles.y.ToString());
rotationXmlElement.SetAttribute("z", sceneObject.transform.rotation.eulerAngles.z.ToString());
// 缩放信息
XmlElement scaleXmlElement = xmlDocument.CreateElement("scale");
scaleXmlElement.SetAttribute("x", sceneObject.transform.localScale.x.ToString());
scaleXmlElement.SetAttribute("y", sceneObject.transform.localScale.y.ToString());
scaleXmlElement.SetAttribute("z", sceneObject.transform.localScale.z.ToString());
transformXmlElement.AppendChild(positionXmlElement);
transformXmlElement.AppendChild(rotationXmlElement);
transformXmlElement.AppendChild(scaleXmlElement);
gameObjectXmlElement.AppendChild(transformXmlElement);
sceneXmlElement.AppendChild(gameObjectXmlElement);
}
}
}
}
rootXmlElement.AppendChild(sceneXmlElement);
xmlDocument.AppendChild(rootXmlElement);
// 保存场景数据
xmlDocument.Save(path);
// 刷新Project视图
AssetDatabase.Refresh();
}
}
}
}
下面我们来看如何还原场景,有了 XML,我们解析 XML 就可以了,资源的加载可以看这篇文章(查看详情),加载场景以及预设资源(assetbundle)的代码如下:
复制代码
代码如下:
using UnityEngine;
using System.Collections.Generic;
public class LoaderScene : MonoBehaviour
{
public UISlider progressBar;
public UILabel lblStatus;
private string scenePath;
void Awake()
{
string prefabPath = "file:///" + Application.dataPath + "/Assets/{0}.assetbundle";
this.scenePath = "file:///" + Application.dataPath + "/Assets/MainScene.unity3d";
IList<WwwLoaderPath> pathList = new List<WwwLoaderPath> ();
pathList.Add (new WwwLoaderPath (this.scenePath, Random.Range (0, 100), WwwLoaderTypeEnum.UNITY_3D));
pathList.Add (new WwwLoaderPath (string.Format(prefabPath, "Lights"), Random.Range (0, 100), WwwLoaderTypeEnum.ASSET_BUNDLE));
pathList.Add (new WwwLoaderPath (string.Format(prefabPath, "Particles"), Random.Range (0, 100), WwwLoaderTypeEnum.ASSET_BUNDLE));
pathList.Add (new WwwLoaderPath (string.Format(prefabPath, "PhysicsCube"), Random.Range (0, 100), WwwLoaderTypeEnum.ASSET_BUNDLE));
pathList.Add (new WwwLoaderPath (string.Format(prefabPath, "Player"), Random.Range (0, 100), WwwLoaderTypeEnum.ASSET_BUNDLE));
pathList.Add (new WwwLoaderPath (string.Format(prefabPath, "Stamps"), Random.Range (0, 100), WwwLoaderTypeEnum.ASSET_BUNDLE));
pathList.Add (new WwwLoaderPath (string.Format(prefabPath, "Statics"), Random.Range (0, 100), WwwLoaderTypeEnum.ASSET_BUNDLE));
pathList.Add (new WwwLoaderPath (string.Format(prefabPath, "Terrain"), Random.Range (0, 100), WwwLoaderTypeEnum.ASSET_BUNDLE));
pathList.Add (new WwwLoaderPath (string.Format(prefabPath, "Trees"), Random.Range (0, 100), WwwLoaderTypeEnum.ASSET_BUNDLE));
this.lblStatus.text = "场景加载中,请稍候。。。";
WwwLoaderManager.instance.Loader (pathList, onLoaderProgress, onLoaderComplete, "MainScene");
}
private void onLoaderProgress(string path, float currentValue, float totalValue)
{
this.progressBar.value = currentValue;
}
private void onLoaderComplete()
{
this.lblStatus.text = "场景正在初始化,请等待。。。";
Application.LoadLevelAsync("MainScene");
}
}
然后新建立一个 C# 文件,取名:InitObject.cs,代码如下:
复制代码
代码如下:
using UnityEngine;
using System.Collections;
using System.Xml;
public class InitObject : MonoBehaviour
{
void Awake()
{
string xmlPath = Application.dataPath + "/Assets/MainScene.xml";
string prefabPath = "file:///" + Application.dataPath + "/Assets/{0}.assetbundle";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load (xmlPath);
// 使用 XPATH 获取所有 gameObject 节点
XmlNodeList xmlNodeList = xmlDocument.SelectNodes("//gameObject");
foreach(XmlNode xmlNode in xmlNodeList)
{
string gameObjectName = xmlNode.Attributes["objectName"].Value;
string prefabName = xmlNode.Attributes["objectAsset"].Value;
AssetBundle assetBundle = WwwDataManager.instance.GetDataAssetBundle(string.Format(prefabPath, prefabName));
if(assetBundle != null)
{
GameObject assetObject = (GameObject)assetBundle.Load(prefabName, typeof(GameObject));
if(assetObject != null)
{
GameObject gameObject = (GameObject)Instantiate(assetObject);
// 使用 XPATH 获取 位置、旋转、缩放数据
XmlNode positionXmlNode = xmlNode.SelectSingleNode("descendant::position");
XmlNode rotationXmlNode = xmlNode.SelectSingleNode("descendant::rotation");
XmlNode scaleXmlNode = xmlNode.SelectSingleNode("descendant::scale");
if(positionXmlNode != null && rotationXmlNode != null && scaleXmlNode != null)
{
gameObject.transform.position = new Vector3(float.Parse(positionXmlNode.Attributes["x"].Value), float.Parse(positionXmlNode.Attributes["y"].Value), float.Parse(positionXmlNode.Attributes["z"].Value));
gameObject.transform.rotation = Quaternion.Euler(new Vector3(float.Parse(rotationXmlNode.Attributes["x"].Value), float.Parse(rotationXmlNode.Attributes["y"].Value), float.Parse(rotationXmlNode.Attributes["z"].Value)));
gameObject.transform.localScale = new Vector3(float.Parse(scaleXmlNode.Attributes["x"].Value), float.Parse(scaleXmlNode.Attributes["y"].Value), float.Parse(scaleXmlNode.Attributes["z"].Value));
}
}
// 卸载引用的加载资源,释放内存
assetBundle.Unload(false);
}
}
xmlDocument = null;
}
}
导出Unity场景的所有游戏对象信息,一种是XML一种是JSON。本篇文章我们把游戏场景中游戏对象的、旋转、缩放、平移与Prefab的名称导出在XML与JSON中。然后解析刚刚导出的XML或JSON通过脚本把导出的游戏场景还原。将层次视图中的所有游戏对象都封装成Prefab保存在资源路径中,这里注意一下如果你的Prefab绑定的脚本中有public Object 的话 ,需要在代码中改一下。。用 Find() FindTag()这类方法在脚本中Awake()方法中来拿,不然Prefab动态加载的时候无法赋值的,如下图所示,我把封装的Prefab对象都放在了Resources/Prefab文件夹下。
场景导出完毕后,会将xml 与Json 文件保存在StreamingAssets路径下,放在这里的原因是方便移动平台移植,因为它们属于二进制文件,移动平台在读取二进制文件的路径是不一样的。我继续创建两个游戏场景,一个用来解析XML的场景,一个用来解析JSON的场景。
XML场景中,创建一个空的游戏对象,把XML.cs挂上去。
[MenuItem ("GameObject/BINARY")]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
bw.Write(posx);
bw.Write((short)(obj.transform.position.y
* 100.0f));
bw.Write((short)(obj.transform.position.z
* 100.0f));
bw.Write((short)(obj.transform.rotation.eulerAngles.x
* 100.0f));
bw.Write((short)(obj.transform.rotation.eulerAngles.y
* 100.0f));
bw.Write((short)(obj.transform.rotation.eulerAngles.z
* 100.0f));
bw.Write((short)(obj.transform.localScale.x
* 100.0f));
bw.Write((short)(obj.transform.localScale.y
* 100.0f));
bw.Write((short)(obj.transform.localScale.z
* 100.0f));
}
}
}
}
bw.Flush();
bw.Close();
fs.Close();
}
注解 在写入二进制数据时用到的核心类就是BinaryWriter ,Binary是二进制的意思 ,可见操作二进制写入就用BinaryWriter了。 常用的数据类型会分配固定的字节数量,假设BinaryWriter 写入一个short 那么就占2字节,写一个 int 就占4字节,如果是数组的话需要数组类型字节长度在乘以数组长度。 byte:一个字节(8位)
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
using
UnityEngine;
using System.Collections;
using
System.IO;
using System.Text;
using
System;
public class
Binary :
MonoBehaviour
{
void
Start ()
{
string
filepath =
Application.dataPath
+ @"/StreamingAssets/binary.txt";
if(File.Exists
(filepath))
{
FileStream
fs =
new FileStream
(filepath,FileMode.Open);
BinaryReader br
= new
BinaryReader(fs);
int
index =
0;
//将二进制字节流全部读取在这个byte数组当中
//ReadBytes传递的参数是一个长度,也就是流的长度
byte[]
tempall =
br.ReadBytes((int)fs.Length);
//开始解析这个字节数组
while(true)
{
//当超过流长度,跳出循环
if(index
>= tempall.Length)
{
break;
}
//得到第一个byte 也就是得到字符串的长度
int
scenelength =
tempall[index];
byte
[]sceneName
= new
byte [scenelength];
index
+= 1;
//根据长度拷贝出对应长度的字节数组
System.Array.Copy(tempall,index,sceneName,0,sceneName.Length);
//然后把字节数组对应转换成字符串
string
sname =
System.Text.Encoding.Default.GetString(sceneName);
//这里和上面原理一样就不赘述
int
objectLength =
tempall[index
+ sceneName.Length];
byte
[]objectName
= new
byte [objectLength];
index
+= sceneName.Length
+ 1;
System.Array.Copy(tempall,index,objectName,0,objectName.Length);
string
oname =
System.Text.Encoding.Default.GetString(objectName);
//下面就是拿short 每一个short的长度是2字节。
index +=
objectName.Length;
byte[]
posx =
new byte[2];
System.Array.Copy(tempall,index,posx,0,posx.Length);
//取得对应的数值 然后 除以100 就是float拉。
float
x =
System.BitConverter.ToInt16(posx,0)
/100.0f;
//下面都差不多
index
+= posx.Length;
byte[]
posy =
new byte[2];
System.Array.Copy(tempall,index,posy,0,posy.Length);
float
y =
System.BitConverter.ToInt16(posy,0)
/100.0f;
index
+= posy.Length;
byte[]
posz =
new byte[2];
System.Array.Copy(tempall,index,posz,0,posz.Length);
float
z =
System.BitConverter.ToInt16(posz,0)
/100.0f;
index
+= posz.Length;
byte[]
rotx =
new byte[2];
System.Array.Copy(tempall,index,rotx,0,rotx.Length);
float
rx =
System.BitConverter.ToInt16(rotx,0)
/100.0f;
index
+= rotx.Length;
byte[]
roty =
new byte[2];
System.Array.Copy(tempall,index,roty,0,roty.Length);
float
ry =
System.BitConverter.ToInt16(roty,0)
/100.0f;
index
+= roty.Length;
byte[]
rotz =
new byte[2];
System.Array.Copy(tempall,index,rotz,0,rotz.Length);
float
rz =
System.BitConverter.ToInt16(rotz,0)
/100.0f;
index
+= rotz.Length;
byte[]
scax =
new byte[2];
System.Array.Copy(tempall,index,scax,0,scax.Length);
float
sx =
System.BitConverter.ToInt16(scax,0)
/100.0f;
index
+= scax.Length;
byte[]
scay =
new byte[2];
System.Array.Copy(tempall,index,scay,0,scay.Length);
float
sy =
System.BitConverter.ToInt16(scay,0)
/100.0f;
index
+= scay.Length;
byte[]
scaz =
new byte[2];
System.Array.Copy(tempall,index,scaz,0,scaz.Length);
float
sz =
System.BitConverter.ToInt16(scaz,0)
/100.0f;
index+=scaz.Length;
if(sname.Equals("Assets/StarTrooper.unity"))
{
//最后在这里把场景生成出来
string
asset =
"Prefab/" +
oname;
Vector3 pos
= new
Vector3 (x,y,z);
Vector3
rot =
new Vector3(rx,ry,rz);
Vector3 sca
= new
Vector3(sx,sy,sz);
GameObject
ob =
(GameObject)Instantiate(Resources.Load(asset),pos,Quaternion.Euler(rot));
ob.transform.localScale
= sca;
}
}
}
}
// Update is called once per frame
void
Update ()
{
}
}
另外还有一种方式也可以实现动态增加建立场景,使用.unity 来实现场景的加载,
首先,你需要一个编辑器文件,放在editor文件夹下。注意,这个文件不可以继承自monobehaviour 这样,在你的unity编辑器上出现了一个按钮,你执行这个按钮,则会在你的Assets同级目录下出现你build好的streamed.unity3d文件,你把这个文件放在服务器上,下面一步就是下载这个文件并build了。 |
本文介绍如何使用Unity导出场景为XML和JSON格式,以及如何通过代码将导出的XML或JSON文件还原为场景。同时,文章还讨论了二进制文件的导出方法以减少数据量。
941

被折叠的 条评论
为什么被折叠?



