Can somebody explain to me how asset bundles work?

Hello, the company I currently work at needs us to reduce the size of the applications. After some research I found the asset bundles. This was 2 weeks ago. I’ve read a lot of articles, answers, forum discussions, and I can’t seem to make them work. My problem is, if the assets are in the asset folder, the codes will work just fine. They load my asset bundle, the scene inside of it, it’s perfect. Up to that point, it’s fine. But hey, doesn’t the assets being inside the assets folder make the asset bundles pointless? If I need to include the assets inside the build, then what’s the point to asset bundles? Also, the codes only work with the simulation mode turned on. I’m using the asset bundle manager provided by unity and a code to download the bundles, which I’m using, for tests, a local address in my own machine. Once I build the project, it doesn’t works either. I don’t know what to do anymore.

Oh, I see, it’s just that we were planning on building a simple menu scene and then the user will have to download the asset bundles with the next scenes. We just needed to reduce the initial size of the app.

But yeah, if you wanted, you could decrease the size of the main install by only having a ‘boot’ scene that loaded in AssetBundles.

Then you built ONLY that ‘boot’ scene.

Then all your other scenes would get compiled into AssetBundles, and the ‘boot’ scene ended up loading those.

1 Like

Yes, exactly, the problem is: the asset bundles won’t load unless the assets are located in the assets folder, which is also referenced in my object bundle manifest. How am I supposed to do in the oficial apk? Just build the main scene? How will the app download the objects? There are so many questions and the tutorials I’ve seem just don’t answer them. It’s confusing.

Well start here at the documentation:

Right at the top it shows how the AssetBundle is loaded, in that example from the webpage. Of course below you can find the various methods including load methods for loading from file and the sort.

Once you have the AssetBundle loaded, if you want to instantiate any specific prefab in it, you can use the LoadAsset to get the prefab and then Instantiate per usual.

If you want to a load a ‘scene’ that is in the AssetBundle, well it’s automatically accessible as a loadable scene through normal loading methods (Application.LoadScene, or SceneManager) as soon as the AssetBundle has finished loading. You can call ‘GetAllScenePaths’ on the asset bundle as soon as it’s loaded to get the names of scenes if you don’t already know the names.

My only question is why the Assets need to be in the AssetBundle folder, even with the Assets being built into a bundle already. I can already load asset bundles perfectly, unless the Assets are no longer in the folder. How would I build the final program knowing that once the Assets are not there anymore, the app won’t run?

I’m not sure what you’re asking…

They don’t need to be in some AssetBundle folder. They can be anywhere in your Assets folder.

Huh?

Again, what folder? And why are you removing them when you’re done?

They SHOULD be there. You don’t get rid of the assets after you build the AssetBundle.

When you actually build, you decide what scenes are included in the build. It will only compile the assets that are needed for that build. So if the only scene that is included is some ‘boot’ scene… only assets referenced by that scene are included in the build. You don’t need to manually remove them from the Assets folder when you build.

Assets in the assets folder are only included in the build if they are referenced in a scene in the build. So by moving your assets to a asset bundle, and removing references to them in scenes in the build, you will be reducing your build size.

If you don’t remove the scene references, then you are indeed wasting your time.

1 Like

Well, thanks for everyone’s replies and sorry if I didn’t made my point clear. I finally understood it a bit more thanks to your answers. It still doesn’t works but I’ll try my best, thank you all for your time. :slight_smile:

Hey @Kiwasi , can i just follow you up on this?

So say I have the following structure:

  • Assets

  • MyAssets

  • MyTexture.png (tagged with myassets assetBundle)

And if my scene references MyTexture, are you saying that texture will be included twice, once as part of the AssetBundle (for this example, AssetBundles are in the streaming assets folder and thus come with the build) and another as part of the build as the texture is in the assets folder and referenced by a scene?

yep

It has to be.

Otherwise how would the main build be able to run. It’d go to load that asset, but it’d fail.

This is why I was telling you that when you make your main build only include the necessary scenes (probably just 1) with limited assets, that then do the loading of the AssetBundles.

Honestly, I’d make the first scene completely empty of assets. Just a single GameObject, with a single ‘load’ script on it, that set up the environment and loaded the first AssetBundle that is the title screen.

The game would just boot to a black screen and that was it. Maybe include a single texture for a logo. Your build would then just be big enough to house all your code, the unity engine, and that single texture.

Note, this will increase your overall size though.

If say ‘MyTexture.png’ is referenced in multiple assetbundles… then each assetbundle is going to contain a version of that texture. If you sum up the size of all asset bundles, you’re going to end up taking up that much more disk space for each version of the texture.

As for in memory… I don’t know if unity is smart enough to recognize you’re loading a duplicate of the same asset. I would not be surprised if it loads dups into memory as well. So it’s probably smart to unload assetbundles as you progress to the next scene.

Hey @lordofduct , I’m not actually the OP, I just stumbled across this thread and it interested me as I’m working on asset bundles now.

I actually have exactly the setup you described, one scene included in the build which triggers initialising and loading the asset bundles, while all the other scenes are in the bundles.

So if my scene that references the texture is also in an asset bundle, will the texture still be included in the build as well as the asset bundle?

yep

If it’s in your initializing scene, it’s included in the main build.

It’s fairly simple. If the the build you make (assetbundle, or game) references an asset, it includes that asset.

Each build must exist on its own. It can not assume that some other build contains the stuff it needs (with the exception of the code, the scripts are assumed to be in the main build).

1 Like

That is only the case if that texture is not in an asset bundle itself. If the texture is in an asset bundle, which is the case in my example, then any asset bundles that reference it (say via prefabs etc) will have a dependancy on that bundle, but they won’t have their own version of it.

From my experience so far, it will not duplicate it in memory. It would only be duplicated if it is referenced by objects inside an assetbundle and objects not inside an asset bundle such as the Resources folder.

Ah ok, yeah that makes perfect sense, and explains how Unity knows what is needed and what is not. For some reason I misunderstood you and proceeded to have a minor meltdown ha!

Thanks for the information!