0
votes

I am working in unity 2018 Asset Bundle. In my project i have to pack the entire scene inside of an AssetBundle and when i needed, the game will download the AssetBundle from the internet and then it should unpack it.

I have used this code.for loading the scene from 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");
        }
    }
}

I have packed my scene and put it in Dropbox. It is downloaded from the internet. The scene is loading fine. I have faced some problems here:

Not the entire screen is loading. The screen reduces to a quarter of its size and then it is playing.

What's wrong in my code? Is there any separate procedure available for loading scene from asset bundle...?

How can I load from a scene from an asset bundle? Is there any sample project available?

1
did you test it without downloading it / without loading it from a bundle? you know .. step by stepderHugo
Also note that if your scene has a . in the file name (not including the .scene) then there is a known bug that was fixed in 2018.3.4Draco18s no longer trusts SE
I have solved this problem.The Reason is Player Setting, Graphics Settings, and Build Settings should be identical on both projects.Once i change my settings the scene is loaded properly. Thanks all for your reply.cloudfire
I have another problem also. While loading the Scene from asset bundle. It lags .. How to solve the lag problem...cloudfire

1 Answers

-1
votes

The whole concept of assetbundle is to load stuff, on-demand. Loading the whole scene seems a bad pattern. Also, the whole script layer is lost, when using asset bundles. If you need to load a big environment or something bigger, just put it in a prefab.