首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Unity3d资源包加载场景

Unity3d资源包加载场景
EN

Stack Overflow用户
提问于 2015-02-13 23:34:12
回答 2查看 9.3K关注 0票数 4

我想将整个场景加载到我的unity项目中。我已经创建了包含3个场景(scene01,scene02,scene03)的资源包。

assetbundle导出如下所示

代码语言:javascript
复制
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();
    }
}        

}

在那之后,我把我的包上传到我的服务器上。然后,我创建了一个脚本来加载资产包,如下所示。

代码语言:javascript
复制
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设备上工作。有什么想法吗?

EN

回答 2

Stack Overflow用户

发布于 2015-02-18 21:23:58

它使用this example工作

因此,我将通过下面的小代码创建资产包

代码语言:javascript
复制
@MenuItem ("Build/BuildWebplayerStreamed")
    static function MyBuild(){
        var levels : String[] = ["Assets/Level1.unity"];
        BuildPipeline.BuildStreamedSceneAssetBundle( levels, "Streamed-Level1.unity3d", BuildTarget.iPhone); 
}

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

代码语言:javascript
复制
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时,我得到一个错误,说"WWWCached数据只能使用assetBundle属性来访问!“也许有人知道这个问题的答案?

票数 3
EN

Stack Overflow用户

发布于 2015-02-13 23:47:54

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

此后,该函数已被弃用,必须改用LoadAllAssets

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28503085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档