How to AssetBundle and Load a whole scene?

I’ve been doing extensive Googling and have only seen people instantiate(spawn) an object from the internet.

What I want is to be able to pack entire scenes inside of an AssetBundle and when it’s time to load the scene, the game will download the AssetBundle from the internet and then unpack it or even have it download prior to the game’s first load. I want my game size to be as small as possible.

Is this possible to do? Since I have not seen this type of tutorial anywhere. If possible, what would I need? Apparently a lot of the AssetBundle Assets are depreciated and are unavailable for download aside from “AssetBundle Browser” which is the only AssetBundle Asset I have to use.

I know that with AssetBundles you can update your game and reupload an AssetBundle for download to do simple fixes or events. But if I do it the way I wanted, AssetBundle-ing a whole scene does this make it so I am unable to do this?

Any help would be appreciated. Thank you!

2 Likes

I assume you did not find these resources?

Guide to AssetBundles
https://unity3d.com/learn/tutorials/topics/best-practices/guide-assetbundles-and-resources

How to (down)load AssetBundles

Addressables System
AssetBundles add a significant amount of complexity to the project. If you’re not up to that, you might want to look into Addressables. I believe the Addressables System makes working with that easier.

For anyone searching, none of those links above point to any reference to loading scenes from an asset bundle. In fact LoadAsset() is prohibited. I would guess the OP read those manual articles then asked the question. There is info on loading a scene from an asset bundle via the (unsupported) AssetBundles-Manager.

8 Likes

Only need to load bundle and call :

string[ ] scenePath = bundles.GetAllScenePaths();
SceneManager.LoadScene(scenePath[0]);

28 Likes

Now that’s useful info.

9 Likes

For what it’s worth, I can’t get a scene and all its assets to live inside a single asset bundle either.

I did discover that although you can put lots of things in asset bundles, you can’t call bundle.LoadAssetAsync<SceneAsset> because the type is in UnityEditor assembly, not UnityEngine. This is (at least partly) why there’s special handling throughout the asset bundle system, and there’s a separate SceneManager interface… because the type itself doesn’t exist at runtime. Figured this out while creating AutoBuilder, and had to do a few jumping jacks to get that one type handled properly out of all the things that go into asset bundles.

2 Likes

This works for me.But I am stuck with " Error while getting Asset Bundle: The AssetBundle can’t be loaded because another AssetBundle with the same files is already loaded.".I tried “AssetBundle.GetAllLoadedAssetBundles()”,but it works with normal AssetBundles but doesn’t return scene bundle.I am using a whole scene as an AssetBundle.
Any Help would be great…

bro i created a bike race game and i exported all its assets inside a folder including scene.now i uploaded all of its asset to web . now i can download all asset but how can i locate my scene and open it.As my scene is inside a folder.and one more thing as i uploaded all my asset to web so can i delete all my asset from game the only script left in game is asset downloder script which will download all the assets

1 Like

if your asset bundle in loaded than can you delete that asset from the game .

This completely flew by me the first time checking this specific forum thread: AutoBuilder is an asset on the store. Just read the description on the asset store and the nifty key features like testing asset bundles INSIDE THE EDITOR.
I think this takes care of a lot of the headaches (and maybe is a must have) for everybody going AssetBundles workflows, will give this a proper try.

Question: Any reason to go AssetBundles or Addressables? can both co-exist? (also, maybe “AutoAddressables” could be a thing)

Yes, one of the things that I thought made it particularly useful (and unique) is the workflow where you build (or download) a working set of asset bundles, and it can incrementally generate an asset bundle that is the difference since that set of bundles was created, so only the things you changed have to be bundled (quickly). It’s fast enough to bind to a hotkey for reasonable projects and just use that to build-and-run.

I can’t say one is better than the other, not having direct experience with Addressables. I don’t see a reason you couldn’t use both, since data is data and however you load it is up to you. I was specifically working on a large game with a lousy asset bundles workflow so I built a better system to scratch that itch. The whole build system in Unity is half-done, so getting that written and simplified and put up on the Asset Store was really a selfish desire to never wast time writing it again. Haha.

2 Likes

As far as I can understand, Scenes work quite differently than the rest of the assets in a bundle. As soon as you load the bundle that contains them, they get added to the scenes path in “/Assets/Scenes” regardless of where you have your bundle.
So you don´t even need to list them before calling them.

Example: let´s say you have a game with 5 scenes (named: level1, level2, level3,etc.), you make a bundle which includes all five, named “gameLevels” and you store it on StreamingAssets or PersistentDataPath. It can be already included in your build (in case of StreamingAssets) or downloaded at runtime, doesn´t matter.

You include a code in a “landing scene”, maybe the title screen, the loading screen or anything that loads before you open the first actual game scene.

The code would be like this:

using UnityEngine.SceneManagement;

AssetBundle levels;

    void Start()
    {
        levels = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + "gameLevels");

//Or levels = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath,"gameLevels"));

        SceneManager.LoadScene("level1", LoadSceneMode.Single);
    }

And that´s it. Now you can refer to any of those 5 levels by name from anywhere in your game without doing anything else. They get listed just the same as if you added them from “build settings”.

So my recommendation would be NOT to bundle them with everything else, but to do a separate bundle that just includes all the scenes that you need, load once at the start of the game, and never again.

And best of all: you can actually change the scenes in editor, re-bundle them, replace them in the final build (could be through UnityWebRequest) and, just like that, update your game.

5 Likes

Why not just add all your scene objects to a parent gameobject then make the gameobject a prefab and bundle that instead. Load the empty scene and instantiate your prefab which contains all the objects for your scene.

1 Like

Asset bundle is for memory and storage heavy stuff. The scene itself is light. Another problem is, u cannot include scripts in asset bundle,but your scene will probably contain some.

There’s no way to distribute scripts through any way other than by making a new executable. You can use asset bundles and attach monobehaviours to objects in them, and that works fine if your executable knows about that monobehaviour. It’s just a reference and variable settings that gets stored in the asset bundle, whether it’s a scene or a prefab or whatever you put in there. If you restrict yourself to only putting raw art assets into asset bundles, you can do that, but that’s not a restriction Unity or AutoBuilder places on you. That’s your choice.

It depends what do you mean “you cant include any script”.
If you are talking about completely new monobehav script that is not shipped with client player then yes, because the code itself is compiled into assembly, but the DATA of the script WILL be serialized into scene. You CAN use existing scripts that are already shipped with the game and their data will be correctly picked up on scene load. However, there are ways around this limitation:

  1. Ship Assembly with AB as a text asset and load from memory runtime before loading ANYTHING else from the bundle: https://discussions.unity.com/t/734298
  2. Use some kind of blueprint-like framework, xNode, Bolt (if it supports this kind of stuff runtime) or LUA plugin)
1 Like

We tried using scenes but had some issues because of the differences so we ended up putting our scene under a single prefab root and using that. Then we have a script called SceneData for variables stored in the scene