Unity3d Assetbundles加载场景

时间:2015-02-13 15:34:12

标签: unity3d

我想将整个场景加载到我的团结项目中。 我已经创建了包含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表示“Level”scene01'( - 1)无法加载,因为它尚未添加到构建设置中。“

我做错了什么。我需要这个在ios和android设备上工作。 有什么想法吗?

2 个答案:

答案 0 :(得分:3)

使用this example

所以我通过这个小代码

创建资产包
@MenuItem ("Build/BuildWebplayerStreamed")
    static function MyBuild(){
        var levels : String[] = ["Assets/Level1.unity"];
        BuildPipeline.BuildStreamedSceneAssetBundle( levels, "Streamed-Level1.unity3d", BuildTarget.iPhone); 
}

之后我通过以下方式加载我的场景:

IEnumerator Start () 
{
    // Wait for the Caching system to be ready
    while (!Caching.ready)
        yield return null;

    // Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache
    using(WWW www = WWW.LoadFromCacheOrDownload (url, 1)){
        yield return www;
        if (www.error != null)
            throw new Exception("WWW download had an error:" + www.error);

        AssetBundle bundle = www.assetBundle;
        bundle.LoadAll();

    } 

    Application.LoadLevel ("scene01");
}

就是这样。我想补充一点,就是进度条。但是当我使用www.bytesDownloaded时,我得到一个错误,说“只能使用assetBundle属性访问WWWCached数据!” 也许有人知道答案吗?

答案 1 :(得分:2)

在Unity 5之前,您应该在尝试加载场景之前调用yourAssetBundle.LoadAll()


之后,该函数已被弃用,而且必须使用LoadAllAssets