libgdx Getting started with libgdx AssetManager


The AssetManager is a class that helps you manage your assets.

First off, you need to create an instance:

AssetManager am = new AssetManager();

After this is initialized, and before you render anything, you want to get the resources:

am.load("badlogic.jpg", Texture.class);//Texture.class is the class this asset is of. If it is a 
//sound asset, it doesn't go under Texture. if it is a 3D model, it doesn't go under Texture.class
//Which class added depends on the asset you load

//... other loading here ...//

//when finished, call finishLoading:

Now, whereever you feel like getting badlogic.jpg:

Texture texture = am.get("badlogic.jpg");
//Ready to render! The rendering itself is in the normal way

Using AssetManager allows you to load them once into the memory of the AssetManager, and then get them as many times as you want.

Why use AssetManager? (from the wiki):

AssetManager (code) helps you load and manage your assets. It is the recommended way to load your assets, due to the following nice behaviors:

  • Loading of most resources is done asynchronously, so you can display a reactive loading screen while things load
  • Assets are reference counted. If two assets A and B both depend on another asset C, C won't be disposed until A and B have been disposed. This also means that if you load an asset multiple times, it will actually be shared and only take up memory once!
  • A single place to store all your assets.
  • Allows to transparently implement things like caches (see FileHandleResolver below)