首页 > 代码库 > 将场景导出XML或JSON或二进制而且解析还原场景

将场景导出XML或JSON或二进制而且解析还原场景

导出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

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 = newXmlDocument();
                XmlElement root = xmlDoc.CreateElement("gameObjects");
                //遍历全部的游戏场景
                foreach (UnityEditor.EditorBuildSettingsScene S inUnityEditor.EditorBuildSettings.scenes)
        {
                //当关卡启用
            if(S.enabled)
            {
                    //得到关卡的名称
                string name = S.path;
                //打开这个关卡
                                EditorApplication.OpenScene(name);
                                XmlElement scenes = xmlDoc.CreateElement("scenes");
                        scenes.SetAttribute("name",name);
                                foreach (GameObject obj inObject.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 = newFileInfo(filepath);
                if(!File.Exists (filepath))
                {
                        File.Delete(filepath);
                }
        StreamWriter sw = t.CreateText();
  
                StringBuilder sb = newStringBuilder ();
        JsonWriter writer = newJsonWriter (sb);
                writer.WriteObjectStart ();
                writer.WritePropertyName ("GameObjects");
                writer.WriteArrayStart ();
  
                foreach (UnityEditor.EditorBuildSettingsScene S inUnityEditor.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 inObject.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();
        }
}
OK。此时我们就能够导出游戏场景的信息拉,注意游戏场景的须要如今Project Setting 中注冊。

点击 GameObject – > Export    XML 和 GameObject – > ExportJson 菜单项就可以開始生成。

技术分享
例如以下图所看到的,场景导出完成后。会将xml 与Json 文件保存在StreamingAssets路径下。放在这里的原因是方便移动平台移植,由于它们属于二进制文件,移动平台在读取二进制文件的路径是不一样的。一定要放在这里喔。
技术分享
接着。我继续创建两个游戏场景,一个用来解析XML的场景,一个用来解析JSON的场景。

 
XML场景中,创建一个空的游戏对象,把XML.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
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 = newXmlDocument();
                         xmlDoc.Load(filepath);
                         XmlNodeList nodeList=xmlDoc.SelectSingleNode("gameObjects").ChildNodes;
                        foreach(XmlElement scene  innodeList)
                        {
                                //由于我的XML是把所有游戏对象所有导出, 所以这里推断一下仅仅解析须要的场景中的游戏对象
                                //JSON和它的原理类似
                                if(!scene.GetAttribute("name").Equals("Assets/StarTrooper.unity"))
                                {
                                        continue;
                                }
  
                                foreach(XmlElement gameObjects inscene.ChildNodes)
                                {
  
                                        string asset = "Prefab/"+ gameObjects.GetAttribute("name");
                                        Vector3 pos = Vector3.zero;
                                        Vector3 rot = Vector3.zero;
                                        Vector3 sca = Vector3.zero;
                                        foreach(XmlElement transform ingameObjects.ChildNodes)
                                        {
                                                foreach(XmlElement prs intransform.ChildNodes)
                                                {
                                                        if(prs.Name == "position")
                                                        {
                                                                foreach(XmlElement position inprs.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;
                                                                }
                                                        }
                                                }elseif(prs.Name == "rotation")
                                                {
                                                        foreach(XmlElement rotation inprs.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;
                                                                }
                                                        }
                                                }elseif(prs.Name == "scale")
                                                {
                                                        foreach(XmlElement scale inprs.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(newRect(0,0,200,200),"XML WORLD"))
                {
                        Application.LoadLevel("JSONScene");
                }
  
        }
  
}
本例XML和JSON的解析与还原场景,在IOS真实设备上測试通过。
技术分享
本例的下载地址:http://vdisk.weibo.com/s/k0_DE
雨松MOMO 祝大家学习愉快,准备睡觉。安 。欢迎讨论与学习 嘿嘿。
补充
近期在做client与server的交互。使用JSON 和XML会感觉数据量太大。影响效率。最后使用二进制的方式来完毕。例如以下图所看到的。使用二进制能够把空间节省到803K 。是不是非常不错呢? 以下我们開始学习怎样制作吧。



技术分享
导出场景时添加导出二进制文件选项,代码例如以下。
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 = newFileStream(filepath, FileMode.Create);
                BinaryWriter bw = newBinaryWriter(fs);
                foreach (UnityEditor.EditorBuildSettingsScene S inUnityEditor.EditorBuildSettings.scenes)
        {
            if(S.enabled)
            {
                                string name = S.path;
                                EditorApplication.OpenScene(name);
  
                                foreach (GameObject obj inObject.FindObjectsOfType(typeof(GameObject)))
                                {
                                    if(obj.transform.parent == null)
                                    {
//注解 直接写入字符串
                                                bw.Write(name);
                                                bw.Write(obj.name);
  
                                                short posx = (short)(obj.transform.position.x * 100);


注解
在写入二进制数据时用到的核心类就是BinaryWriter 。Binary是二进制的意思 ,可见操作二进制写入就用BinaryWriter了。 经常使用的数据类型会分配固定的字节数量,如果BinaryWriter 写入一个short 那么就占2字节,写一个 int 就占4字节,如果是数组的话须要数组类型字节长度在乘以数组长度。
byte:一个字节(8位) 
short:两个字节(16位) 
int:四个字节(32位)(一个字长) 
long:八个字节(64位)
float:四个字节(32位)
double:八个字节(64位)然后在说说string。字符串它并非标准的数据类型,它是一个对象 object 那么它的字节长度就是可变的。開始我也在string 上纠结了一小会儿。还有BinaryWriter 在写入string 的时候会现将字符串的长度以byte的形式储存。然后在储存字符串的字节长度。那么在解析字符串的时候须要先解析字符串长度。然后在依据长度取得后面相应长度的字节数组,再把这个字节数组转换成string即可啦。

还有,上面我用的是short x 100 事实上上为了节省长度, 由于short是2字节。float是4字节。

我在解析的时候用short 在除以100 就能够 换算成float拉。
然后我们在看看解析的代码。写入的时候我们用的是BinaryWriter 那么读取的时候应该是 BinaryReader。 Binary.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
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 = newFileStream (filepath,FileMode.Open);
                        BinaryReader br = newBinaryReader(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 = newbyte [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 = newbyte [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 = newbyte[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 = newbyte[2];
                                System.Array.Copy(tempall,index,posy,0,posy.Length);       
                                float y = System.BitConverter.ToInt16(posy,0) /100.0f;
  
                                index += posy.Length;
                                byte[] posz = newbyte[2];
                                System.Array.Copy(tempall,index,posz,0,posz.Length);       
                                float z = System.BitConverter.ToInt16(posz,0) /100.0f;       
  
                                index += posz.Length;
                                byte[] rotx = newbyte[2];
                                System.Array.Copy(tempall,index,rotx,0,rotx.Length);       
                                float rx = System.BitConverter.ToInt16(rotx,0) /100.0f;       
  
                                index += rotx.Length;
                                byte[] roty = newbyte[2];
                                System.Array.Copy(tempall,index,roty,0,roty.Length);       
                                float ry = System.BitConverter.ToInt16(roty,0) /100.0f;       
  
                                index += roty.Length;
                                byte[] rotz = newbyte[2];
                                System.Array.Copy(tempall,index,rotz,0,rotz.Length);       
                                float rz = System.BitConverter.ToInt16(rotz,0) /100.0f;                       
  
                                index += rotz.Length;
                                byte[] scax = newbyte[2];
                                System.Array.Copy(tempall,index,scax,0,scax.Length);       
                                float sx = System.BitConverter.ToInt16(scax,0) /100.0f;       
  
                                index += scax.Length;
                                byte[] scay = newbyte[2];
                                System.Array.Copy(tempall,index,scay,0,scay.Length);       
                                float sy = System.BitConverter.ToInt16(scay,0) /100.0f;       
  
                                index += scay.Length;
                                byte[] scaz = newbyte[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 = newVector3 (x,y,z);
                                        Vector3 rot = newVector3(rx,ry,rz);
                                        Vector3 sca = newVector3(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 ()
        {
  
        }
}
执行一下,场景依旧生成的很完美,在处理二进制解析的时候须要特别注意的就是字节对齐,由于你的全部数据事实上就是一个byte[]字节数组,须要有理有序的把字节数组拆分,然后在转换成相应的数据,所以一定要对齐不然肯定会出错的。
最后把代码放出来,晚安 Good Ngith 哇咔咔。


下载地址 :http://vdisk.weibo.com/s/la_QE 
留言中刚好有人讨论到这块。

另外另一种方式也能够实现动态添加建立场景,使用.unity 来实现场景的载入,我认为这样的方式可能会更好一些。我在网上已经发现有人写了。那就转载过来吧。


在unity中,场景(scene)多半通过在build settings中点击add current或者把场景拖进面板实现,假如不这么做。你的场景便不会被载入,哪怕你制定了绝对路径。
就是说。一个游戏里要载入多少场景多半都是固定的。


这个方案会有非常多不便。不easy动态载入场景。所以我们今天要说的,是一种动态载入场景的方法。
首先。你须要一个编辑器文件。放在editor目录下。注意。这个文件不能够继承自monobehaviour

1
2
3
4
5
6
7
public class BuildSceneEditor{
    [@MenuItem("build/BuildWebplayerStreamed")] 
    static void Build(){
        string[] levels = newstring[]{"Assets/Level1.unity","Assets/Level2.unity"}; 
        BuildPipeline.BuildStreamedSceneAssetBundle(levels,"streamed.unity3d",BuildTarget.WebPlayer); 
    }
}
这样,在你的unity编辑器上出现了一个button。你运行这个button,则会在你的Assets同级文件夹下出现你build好的streamed.unity3d文件。你把这个文件放在server上。以下一步就是下载这个文件并build了。
1
2
3
WWW download = WWW.LoadFromCacheOrDownload("http://xxx/streamed.unity3d",0); 
yieldreturndownload; 
Application.LoadLevel("Level1");
大家注意到了吗。下载好以后就能够直接loadlevel了。不须要手动进行add current的操作了。


最后我在补充一下使用.unity3d确实方便非常多,由于它不仅会把场景打包进去,而且还会把场景中相应的资源文件打包进去。举个样例,你将美工做好的模型文件放在Project视图中,然后在将模型放在Hierarchy视图中的 100,100,100坐标点中,最后把该场景打包成.unity3d文件。

此时你在新建一个project仅仅需下载刚刚打包的场景文件,他会自己主动把模型放在 100,100,100坐标点中。
这说明场景文件,包括了该场景中所用到的全部模型,而且还包括了模型资源与Hierarchy视图的关系。它会带来一个弊端。比方你有N个场景。每一个场景中都有同样的模型文件,这样每一个场景都须要反复下载这些同样的模型文件,所以我认为不妨使用assetbundle来对同类的资源文件进行分包处理。

将场景导出XML或JSON或二进制而且解析还原场景