Reputation: 469
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?
Upvotes: 4
Views: 9952
Reputation: 2875
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.
Upvotes: 2
Reputation: 469
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?
Upvotes: 3