Unity3d资产包加载场景

4

我希望能够在我的Unity项目中加载整个场景。

我已经创建了同时包含三个场景(scene01,scene02,scene03)的资源包。

资源包导出如下:

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设备上使用它。有什么想法吗?
2个回答

3

这个功能是使用这个示例实现的。

因此,我通过以下代码创建了资源包:

@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数据!”也许有人知道答案?


2

你已经仔细检查了场景是否确实在资产包中,对吧?在这种情况下,它是为iOS还是Android并没有任何区别。 - Gary Riches
这一行代码:foreach (var scene in EditorBuildSettings.scenes) 是问题所在。你需要按照我最初的建议,在构建资源包时将场景包含在构建设置中。运行时,请务必检查控制台是否显示:“Add scene : YOUR SCENE NAME”。 - Gary Riches
你确认过场景名称和大小写是否正确吗? - Gary Riches
是的,我将三个场景放在了一个包中,它们分别被称为scene01、scene02和scene03。我正在尝试通过Application.LoadLevel加载"scene01"。我需要添加".unity"或其他后缀吗? - Jenny
在Unity5中,是否仍需要使用LoadAll? - Fabricio
显示剩余8条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接