如何在Unity 2018中从Asset Bundle加载场景

时间:2019-02-20 13:50:52

标签: unity3d assetbundle

我正在使用统一2018资产捆绑包工作。在我的项目中,我必须打包整个场景 在AssetBundle中,当我需要时,游戏会从互联网下载AssetBundle,然后将其解压缩。

我已使用此代码从资产捆绑加载场景。

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using System.IO;
using UnityEngine.SceneManagement;

public class AssetBundleSceneLoader : MonoBehaviour 
{
    public string url;
    public int downloaded = 0;
    AssetBundle bundle;
    public System.Object test;
    public Slider progressbar;

    public float progress;

    WWW www;
    void Update() 
    {
        progress = www.progress;
        progressbar.value = progress;
    }

    IEnumerator Start() 
    {
        ClearCacheExample ();
        if (downloaded == 0)
        {
            using ( www = WWW.LoadFromCacheOrDownload (url, 0)) 
            {
                yield return www;
                if (www.error != null)
                    throw new Exception ("WWW download had an error:" + www.error);
                if (www.error == null) 
                {
                    bundle = www.assetBundle;
                }
            }
            if (Caching.ready == true) 
            {
                downloaded = 1;
                string[] scenePath = bundle.GetAllScenePaths();
                Debug.Log(scenePath[0]);
                SceneManager.LoadScene(scenePath[0]);
            }
        }
    }

    void ClearCacheExample()
    {
        Directory.CreateDirectory("Cache1");
        Directory.CreateDirectory("Cache2");
        Directory.CreateDirectory("Cache3");

        Caching.AddCache("Cache1"); 
        Caching.AddCache("Cache2"); 
        Caching.AddCache("Cache3"); 

        bool success = Caching.ClearCache();

        if (!success)
        {
            Debug.Log("Unable to clear cache");
        }
    }
}

我已经打包好场景并将其放入Dropbox。它是从Internet下载的。场景加载良好。我在这里遇到了一些问题:

不是整个屏幕都在加载。屏幕缩小到其大小的四分之一,然后开始播放。

我的代码有什么问题?是否有其他单独的程序可用于从资产捆绑包中加载场景??

如何从资产捆绑包的场景加载?有可用的示例项目吗?

1 个答案:

答案 0 :(得分:-1)

assetbundle 的整个概念是按需加载内容。加载整个场景似乎是一种糟糕的模式。此外,使用资产包时,整个脚本层都会丢失。如果您需要加载一个大环境或更大的东西,只需将其放入预制件中即可。