因为编辑的游戏场景数量比较多,导出的时候我们需要遍历所有的游戏场景,一个一个的读取场景信息。然后取得游戏场景中所有游戏对象的Prefab的 名称 旋转 缩放 平移。有关XML的使用请大家看我的上一篇文章: Unity3d研究院之使用 C#合成解析XML与JSON(四十一) 代码中我只注释重点的部分,嘿嘿。
MyEditor.cs
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
using
UnityEngine; using
System.Collections; using
UnityEditor; using
System.Collections.Generic; using
System.Xml; using
System.IO; using
System.Text; using
LitJson; public
class MyEditor : Editor { //将所有游戏场景导出为XML格式 [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(); } //将所有游戏场景导出为JSON格式 [MenuItem
( "GameObject/ExportJSON" )] static
void ExportJSON () { string
filepath = Application.dataPath + @ "/StreamingAssets/json.txt" ; FileInfo
t = new
FileInfo(filepath); if (!File.Exists
(filepath)) { File.Delete(filepath); } StreamWriter
sw = t.CreateText(); StringBuilder
sb = new
StringBuilder (); JsonWriter
writer = new
JsonWriter (sb); writer.WriteObjectStart
(); writer.WritePropertyName
( "GameObjects" ); writer.WriteArrayStart
(); foreach
(UnityEditor.EditorBuildSettingsScene S in
UnityEditor.EditorBuildSettings.scenes) { if
(S.enabled) { string
name = S.path; EditorApplication.OpenScene(name); writer.WriteObjectStart(); writer.WritePropertyName( "scenes" ); writer.WriteArrayStart
(); writer.WriteObjectStart(); writer.WritePropertyName( "name" ); writer.Write(name); writer.WritePropertyName( "gameObject" ); writer.WriteArrayStart
(); foreach
(GameObject obj in
Object.FindObjectsOfType( typeof (GameObject))) { if
(obj.transform.parent == null ) { writer.WriteObjectStart(); writer.WritePropertyName( "name" ); writer.Write(obj.name); writer.WritePropertyName( "position" ); writer.WriteArrayStart
(); writer.WriteObjectStart(); writer.WritePropertyName( "x" ); writer.Write(obj.transform.position.x.ToString( "F5" )); writer.WritePropertyName( "y" ); writer.Write(obj.transform.position.y.ToString( "F5" )); writer.WritePropertyName( "z" ); writer.Write(obj.transform.position.z.ToString( "F5" )); writer.WriteObjectEnd(); writer.WriteArrayEnd(); writer.WritePropertyName( "rotation" ); writer.WriteArrayStart
(); writer.WriteObjectStart(); writer.WritePropertyName( "x" ); writer.Write(obj.transform.rotation.eulerAngles.x.ToString( "F5" )); writer.WritePropertyName( "y" ); writer.Write(obj.transform.rotation.eulerAngles.y.ToString( "F5" )); writer.WritePropertyName( "z" ); writer.Write(obj.transform.rotation.eulerAngles.z.ToString( "F5" )); writer.WriteObjectEnd(); writer.WriteArrayEnd(); writer.WritePropertyName( "scale" ); writer.WriteArrayStart
(); writer.WriteObjectStart(); writer.WritePropertyName( "x" ); writer.Write(obj.transform.localScale.x.ToString( "F5" )); writer.WritePropertyName( "y" ); writer.Write(obj.transform.localScale.y.ToString( "F5" )); writer.WritePropertyName( "z" ); writer.Write(obj.transform.localScale.z.ToString( "F5" )); writer.WriteObjectEnd(); writer.WriteArrayEnd(); writer.WriteObjectEnd(); } } writer.WriteArrayEnd(); writer.WriteObjectEnd(); writer.WriteArrayEnd(); writer.WriteObjectEnd(); } } writer.WriteArrayEnd(); writer.WriteObjectEnd
(); sw.WriteLine(sb.ToString()); sw.Close(); sw.Dispose(); AssetDatabase.Refresh(); } } |
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
121
122
|
using
UnityEngine; using
System.Collections; using
System.Xml; using
System.IO; 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; } } } } } //
Update is called once per frame void
Update () { } void
OnGUI() { if (GUI.Button( new
Rect(0,0,200,200), "XML
WORLD" )) { Application.LoadLevel( "JSONScene" ); } } } |
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
|
[MenuItem
( "GameObject/BINARY" )] static
void XMLJSONTOBinary () { string
filepath = Application.dataPath + @ "/StreamingAssets/binary.txt" ; if (File.Exists
(filepath)) { File.Delete(filepath); } FileStream
fs = new
FileStream(filepath, FileMode.Create); BinaryWriter
bw = new
BinaryWriter(fs); foreach
(UnityEditor.EditorBuildSettingsScene S in
UnityEditor.EditorBuildSettings.scenes) { if
(S.enabled) { string
name = S.path; EditorApplication.OpenScene(name); foreach
(GameObject obj in
Object.FindObjectsOfType( typeof (GameObject))) { if
(obj.transform.parent == null ) { //注解
直接写入字符串 bw.Write(name); bw.Write(obj.name); short
posx = (short)(obj.transform.position.x * 100); |
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 () { } } |
1
2
3
4
5
6
7
|
public
class BuildSceneEditor{ [@MenuItem( "build/BuildWebplayerStreamed" )]
static
void Build(){ string[]
levels = new
string[]{ "Assets/Level1.unity" , "Assets/Level2.unity" };
BuildPipeline.BuildStreamedSceneAssetBundle(levels, "streamed.unity3d" ,BuildTarget.WebPlayer);
}
} |
1
2
3
|
yield
return
download; Application.LoadLevel( "Level1" ); |
将场景导出XML或JSON或二进制并且解析还原场景,布布扣,bubuko.com
原文地址:http://blog.csdn.net/mylefteyeisl/article/details/34093055