如何从Unity 2018中从资产bundle中加载场景



我正在Unity 2018 Asset Bundle工作。在我的项目中,我必须打包整个场景在AssetBundle的内部,当我需要时,游戏将从Internet下载资产箱,然后将其拆开。

我已经使用了此代码。用于从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中。它是从互联网下载的。场景加载正常。我在这里遇到了一些问题:

不是整个屏幕正在加载。屏幕减少到其大小的四分之一,然后播放。

我的代码中有什么问题?是否有任何单独的程序可从资产捆绑中加载场景...?

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

assetbundle的整个概念是按需加载东西。加载整个场景似乎是一个不好的模式。另外,使用资产捆绑包时,整个脚本层都会丢失。如果您需要加载大环境或更大的东西,只需将其放入预制。

最新更新