Search Unity

How to AssetBundle and Load a whole scene?

Discussion in 'Asset Bundles' started by phongle123, Aug 18, 2018.

  1. phongle123

    phongle123

    Joined:
    Mar 20, 2016
    Posts:
    17
    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!
     
    Belader likes this.
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    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
    https://docs.unity3d.com/Manual/AssetBundles-Workflow.html
    https://docs.unity3d.com/Manual/AssetBundles-Native.html

    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.
    https://forum.unity.com/threads/addressables-are-here.536304/
     
  3. MarkHenryC

    MarkHenryC

    Joined:
    Nov 30, 2009
    Posts:
    67
    For anyone searching, none of those links above point to any reference to loading scenes from an asset bundle. In fact LoadAsset<Scene>() 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.
     
    OmarVector, fherbst, Psyco92 and 4 others like this.
  4. Belader

    Belader

    Joined:
    Oct 1, 2012
    Posts:
    9
    Only need to load bundle and call :

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

    MarkHenryC

    Joined:
    Nov 30, 2009
    Posts:
    67
    Now that's useful info.
     
  6. jhughes2112

    jhughes2112

    Joined:
    Nov 20, 2014
    Posts:
    178
    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.
     
    AlejMC and radiantboy like this.
  7. mathewsbabu

    mathewsbabu

    Joined:
    Sep 30, 2014
    Posts:
    33
    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..
     
  8. RaviVohra

    RaviVohra

    Joined:
    Mar 8, 2019
    Posts:
    5
    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
     
    Psyco92 likes this.
  9. RaviVohra

    RaviVohra

    Joined:
    Mar 8, 2019
    Posts:
    5
    if your asset bundle in loaded than can you delete that asset from the game .
     
  10. AlejMC

    AlejMC

    Joined:
    Oct 15, 2013
    Posts:
    149
    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)
     
  11. jhughes2112

    jhughes2112

    Joined:
    Nov 20, 2014
    Posts:
    178
    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.
     
    mbarnes-mv and AlejMC like this.
  12. adrianzarp

    adrianzarp

    Joined:
    Feb 25, 2014
    Posts:
    4
    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:

    Code (CSharp):
    1.  
    2.  
    3. using UnityEngine.SceneManagement;
    4.  
    5. AssetBundle levels;
    6.  
    7.     void Start()
    8.     {
    9.         levels = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + "gameLevels");
    10.  
    11. //Or levels = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath,"gameLevels"));
    12.  
    13.         SceneManager.LoadScene("level1", LoadSceneMode.Single);
    14.     }
    15.  
    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.
     
    alphdevcode, regduce, cdu051 and 2 others like this.
  13. orrinjones

    orrinjones

    Joined:
    Jan 14, 2017
    Posts:
    8
    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.
     
    hoedeer likes this.
  14. viktorkadza

    viktorkadza

    Joined:
    Sep 4, 2018
    Posts:
    46
    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.
     
  15. jhughes2112

    jhughes2112

    Joined:
    Nov 20, 2014
    Posts:
    178
    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.
     
  16. Digika

    Digika

    Joined:
    Jan 7, 2018
    Posts:
    225
    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://forum.unity.com/threads/solved-inserting-dll-into-built-game.638671/
    2. Use some kind of blueprint-like framework, xNode, Bolt (if it supports this kind of stuff runtime) or LUA plugin)
     
    Last edited: Mar 29, 2021
    viktorkadza likes this.
  17. DaveBurlingame

    DaveBurlingame

    Joined:
    May 4, 2021
    Posts:
    3
    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