4

I would like to load a whole scene into my unity project. I already created the asset bundle with 3 scenes (scene01,scene02,scene03)

The assetbundle export looks like this

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();
    }
}        

}

After that I uploaded my bundle on my server. Than I created a script to load the asset bundle which looks like this.

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");
}
}

My last Debug returns "false". And Unity says "Level 'scene01' (-1) couldn't be loaded because it has not been added to the build settings."

What am I doing wrong. I need this to work on ios and android devices. Any ideas?

Gary Riches
  • 2,847
  • 1
  • 22
  • 19
Jenny
  • 469
  • 2
  • 11
  • 25

2 Answers2

3

It works using this example

So I am creating the asset bundle through this little code

@MenuItem ("Build/BuildWebplayerStreamed")
    static function MyBuild(){
        var levels : String[] = ["Assets/Level1.unity"];
        BuildPipeline.BuildStreamedSceneAssetBundle( levels, "Streamed-Level1.unity3d", BuildTarget.iPhone); 
}

after that I load my Scene through the following:

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");
}

That's it. What I would like to add, would be a progress bar. But when I use the www.bytesDownloaded, I get an error saying that "WWWCached data can only be accessed using the assetBundle property!" Maybe someone knows an answer to this?

Jenny
  • 469
  • 2
  • 11
  • 25
2

Prior to Unity 5, you should call yourAssetBundle.LoadAll() before trying to load the scene:


After that the function has been deprecated and LoadAllAssets must be use instead.

ForceMagic
  • 6,230
  • 12
  • 66
  • 88
Gary Riches
  • 2,847
  • 1
  • 22
  • 19
  • Thank you for your answer. But when I load a scene through the assetbundle, it can't be in the build settings already. Because than I would not need an assetbundle right? – Jenny Feb 13 '15 at 15:50
  • Edited with a different answer. – Gary Riches Feb 13 '15 at 15:55
  • I put bundle.LoadAll(); before the debug. It still gives me the same error. Debug returns "false" and missing scene. Could it be the problem that I am trying to use it for iphone and android? – Jenny Feb 13 '15 at 16:03
  • You have double checked that the scene IS actually in the asset bundle yeah? The fact it's for iOS or Android makes no difference in this instance. – Gary Riches Feb 13 '15 at 16:07
  • Actually I am not sure how to test that. This is my first attempt with asset bundles. I used the script above to create it. But I am not sure if everything is in that bundle now. – Jenny Feb 13 '15 at 16:09
  • This line here: `foreach (var scene in EditorBuildSettings.scenes)` is the issue. You need to do what I initially said, include the scenes in the build settings when you build the asset bundle. When you run it, be sure to check the console for: "Add scene : YOUR SCENE NAME" – Gary Riches Feb 13 '15 at 16:12
  • What I do is, first I add all scenes through the build settings like you said. Than I open the Editor Window and select all scenes I want in my asset bundle. After that I hit "create bundle". I upload it to my server. Then in a different project I try to load this asset bundle. But of course in that project, those scenes are not included in the build settings. Am I doing it wrong? – Jenny Feb 13 '15 at 16:21
  • When you build your asset bundle, do you see the "Add scene : YOUR SCENE NAME"in your console? – Gary Riches Feb 13 '15 at 16:25
  • Yes I do, after I open the editor window I see 5 times "Add scene..." for all scenes I use. When I click on create bundle, the bundle is updated. – Jenny Feb 13 '15 at 16:28
  • And you've double checked your scene names and capitalisation? – Gary Riches Feb 13 '15 at 16:35
  • Yes, all three scenes I put in the bundle are called scene01,scene02,scene03. And via Application.LoadLevel I am trying to load "scene01". Do I need to add an ".unity" or something? – Jenny Feb 13 '15 at 16:38
  • Is this required LoadAll still needed in Unity5? – Fabricio Apr 17 '15 at 10:54
  • @Fabricio It has been deprecated, you must use LoadAllAssets instead. – ForceMagic Aug 09 '16 at 18:27