Unity 4 - using AssetBundle to pre-load assets that are shared across multiple scenes?

1.1k views Asked by At

I have several areas in my game project that are split over several scenes, for instance let's say there is a first area called "Basement" which is split in the Basement_1, Basement_2 and Basement_3 scenes; and another area, "Attic", split over Attic_1 and Attic_2.

Some assets (textures atlas and sounds) are shared across all the scenes of an area and so I thought that I could make an AssetBundle with all the shared assets for "Basement", and another AssetBundle with the shared assets for the "Attic" area. Then when my player is about to enter the "Basement" area, I'd load the "Basement" AssetBundle, so that when I call Application.LoadLevel("Basement_1"), Application.LoadLevel("Basement_2") or Application.LoadLevel("Basement_3"), the shared assets (which have been loaded in the bundle) aren't unnecessarily reloaded, only the scene-specific assets would be loaded. Then if I get to a point where the game knows the Basement assets will no longer be necessary, or not for a long time at least, then I'd unload the bundle, to free up memory, and load whatever other bundle may be relevant (the Attic one for instance).

My question is: is this how it works? As in, say I'm about to enter "Basement", so if I load my "Basement" AssetBundle from disk, and basically just call LoadAllAssets() on it, will Application.LoadLevel("Basement_1") "automatically" look for assets in the bundle that I've loaded? or will Application.LoadLevel just reload the assets it needs anew?

My current understanding is that Application.LoadLevel will not automatically look in the AssetBundle. If so, is there a way to make it look in the loaded AssetBundle(s) ? Is there another way that I'm not seeing?

The idea is to try to reduce loading times, because for instance going in and out of a door could trigger a call to Application.LoadLevel("Basement_X") and Application.LoadLevel("Basement_Y") back and forth.

Thanks in advance for any insights ;)

Cheers!

1

There are 1 answers

2
Roberto On

Asset Bundles can be used the way you are proposing, but they are primarily used to resolve the issue of downloading content from a server. Basically, if you are not planning to have anything being downloaded, you don't have many reasons to use asset bundles.

Prefabs, on the other hand, seems to be what you need. They are game objects that you "pack" into a prefab that can be reused. If you want to re-use a game object, in the same scene or on different scenes, prefab is the way to go.

If you are programmatically instantiating and positioning game objects, you would do this:

GameObject myItem = Instantiate(Resources.Load("myPrefab")) as GameObject;

Given that myPrefab is a prefab inside a folder called Resources.

If you are creating your scenes in the editor, you'd only drag and drop prefabs to the scene, they'll become regular game objects.

You can optimize game object usage between scenes by using Object.DontDestroyOnLoad to prevent objects of being destroyed. I'd highly recommend that you only use it if they are absolutely not changing. Such premature optimization (of reusing objects) can potentially cause unthought problems and delay the development of your game.

That said, also keep in mind that if you keep a reference to the loaded prefab, asset bundle, game object or whatever, it will never be garbage collected, so you can make use of Object.DontDestroyOnLoad and/or static references to keep them in memory.