為了盡可能加快從網(wǎng)絡(luò)加載場景,我們通常可以把場景先導(dǎo)出成 XML,把優(yōu)先級高的資源優(yōu)先加載并顯示(地形等),把可以進(jìn)入場景之后再加載的對象放到最后(比如場景里面的怪物等),本篇一部分代碼引用自:http://www.xuanyusong.com/archives/1919,導(dǎo)出場景部分在原作者的代碼基礎(chǔ)進(jìn)行了優(yōu)化,并且整理成了更加方便,容易使用的類庫。
先來搭建測試場景(測試場景來源網(wǎng)絡(luò)),并整理場景中的對象,如圖:
然后把場景中的對象都設(shè)置成預(yù)設(shè),方便打包成 assetbundle 文件(如何打包預(yù)設(shè)請查看),如圖:
接著我們編寫把場景打包成 XML 的代碼,取名 ExportSceneToXml.cs,大家可以先看這篇文章(http://www.xuanyusong.com/archives/1919),我在此基礎(chǔ)上面進(jìn)行了優(yōu)化,全部代碼如下:
復(fù)制代碼代碼如下:
</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);
//遍歷所有的游戲?qū)ο?br /> 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);
// 打開這個關(guān)卡
EditorApplication.OpenScene(scenePath);
XmlDocument xmlDocument = new XmlDocument();
// 創(chuàng)建XML屬性
XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
xmlDocument.AppendChild(xmlDeclaration);
// 創(chuàng)建XML根標(biāo)志
XmlElement rootXmlElement = xmlDocument.CreateElement("root");
// 創(chuàng)建場景標(biāo)志
XmlElement sceneXmlElement = xmlDocument.CreateElement("scene");
sceneXmlElement.SetAttribute("sceneName", sceneName);
foreach (GameObject sceneObject in Object.FindObjectsOfType(typeof(GameObject)))
{
// 如果對象是激活狀態(tài)
if (sceneObject.transform.parent == null && sceneObject.activeSelf)
{
// 判斷是否是預(yù)設(shè)
新聞熱點
疑難解答
圖片精選