【问题标题】:Unity3d Assetbundles load sceneUnity3d Assetbundles 加载场景
【发布时间】: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


    【解决方案1】:

    它使用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 时,我收到一条错误消息,提示“WWWCached 数据只能使用assetBundle 属性访问!” 也许有人知道这个问题的答案?

    【讨论】:

      【解决方案2】:

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


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

      【讨论】:

      • 感谢您的回答。但是当我通过assetbundle加载场景时,它已经不能在构建设置中了。因为我不需要资产包对吗?
      • 用不同的答案编辑。
      • 我把 bundle.LoadAll();在调试之前。它仍然给我同样的错误。调试返回“假”和缺少场景。难道是我试图将它用于 iphone 和 android 的问题?
      • 您已经仔细检查了场景实际上是否在资产包中,是吗?在这种情况下,它适用于 iOS 或 Android 的事实没有任何区别。
      • 其实我不知道如何测试。这是我第一次尝试使用资产包。我使用上面的脚本来创建它。但我不确定现在是否所有东西都在那个捆绑包中。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多