【发布时间】:2015-02-13 15:34:11
【问题描述】:
我想将整个场景加载到我的统一项目中。 我已经创建了包含 3 个场景(scene01、scene02、scene03)的资产包
assetbundle 导出如下所示
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
public class CreateAssetBundle : EditorWindow {
public const string bundlePath = "AssetBundle.unity3D";
[MenuItem("Bundle/Create")]
static void Open()
{
var w = EditorWindow.GetWindow <CreateAssetBundle>("Create bundle");
w.MyInit();
w.Show();
}
private Dictionary<string, bool> ScenesSelection;
void MyInit()
{
Debug.Log("Init window");
this.ScenesSelection = new Dictionary<string, bool>();
foreach (var scene in EditorBuildSettings.scenes)
{
Debug.Log("Add scene : " + scene.path);
this.ScenesSelection.Add(scene.path, false);
}
}
void OnGUI()
{
if (this.ScenesSelection == null)
{
this.MyInit();
}
foreach (var scene in EditorBuildSettings.scenes)
{
if (this.ScenesSelection.ContainsKey(scene.path))
{
this.ScenesSelection[scene.path] = EditorGUILayout.Toggle(scene.path, this.ScenesSelection[scene.path]);
}
}
if (GUILayout.Button("Create bundle"))
{
List<string> selectedScenes = new List<string>();
foreach (var scene in EditorBuildSettings.scenes)
{
if (this.ScenesSelection[scene.path])
{
selectedScenes.Add(scene.path);
}
}
BuildPipeline.PushAssetDependencies();
BuildPipeline.BuildPlayer(selectedScenes.ToArray(), bundlePath, BuildTarget.iPhone,
BuildOptions.UncompressedAssetBundle | BuildOptions.BuildAdditionalStreamedScenes
);
BuildPipeline.PopAssetDependencies();
}
}
}
之后,我将我的捆绑包上传到我的服务器上。 然后我创建了一个脚本来加载如下所示的资产包。
using UnityEngine;
using System.Collections;
public class LoadBundleScene : MonoBehaviour {
public string bundlePath = "AssetBundle.unity3D";
public string url;
IEnumerator Start ()
{
var download = WWW.LoadFromCacheOrDownload (url, 1);
yield return download;
// Handle error
if (download.error != null)
{
Debug.LogError(download.error);
return true;
}
var bundle = download.assetBundle;
Debug.LogWarning(bundle.Contains("scene01"));
Application.LoadLevelAdditive ("scene01");
}
}
我的最后一次调试返回“false”。 Unity 说“无法加载级别‘scene01’ (-1),因为它尚未添加到构建设置中。”
我做错了什么。我需要这个在 ios 和 android 设备上工作。 有什么想法吗?
【问题讨论】:
标签: unity3d