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?