0

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?

derHugo
  • 83,094
  • 9
  • 75
  • 115
cloudfire
  • 27
  • 3
  • 7
  • did you test it without downloading it / without loading it from a bundle? you know .. step by step – derHugo Feb 20 '19 at 15:36
  • 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.4 – Draco18s no longer trusts SE Feb 20 '19 at 19:44
  • 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 Feb 23 '19 at 06:39
  • I have another problem also. While loading the Scene from asset bundle. It lags .. How to solve the lag problem... – cloudfire Feb 23 '19 at 06:41

1 Answers1

-4

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.

  • You are wrong, if you want to add scene new scene of your game like a level that use old scripts with a new level design, you don't need to update your game, you just can upload this scene as Asset bundle and let your users download it and it's make your game more scaleable. – Noam Riahi May 03 '23 at 15:41