Asset Manager – LibGDX Tutorial

Asset Manager LibGdx

Sharing is caring!

What is an Asset Manager?

An Asset Manager in LibGDX is a class that is used to load and unload resources for your project. It will store a number of references an asset has and keep it loaded until it is no longer required. The Asset Manager API is viewable here.

By using an Asset Manager you will load items asynchronously, keep all the assets together, stop loading duplicate resources and keep references of used assets until no other assets reference it. This will help reduce memory usage in your project as well as make loading assets easy.

Adding an Asset Manager

In this tutorial I will be showing you how to use the asset manager by creating a MyAssetManager class. In this class you will load images, sounds, fonts, particle effects and skins, which should cover most of what your project would need. The first step is to create the MyAssetManager class.

In the above code we have created a new class called MyAssetManager and added a new AssetManager. We have also imported all the packages we will be using in our Asset Manager.

Loading Images with your Asset Manager

Now we have our class MyAssetManager we need to load our image in it. First we define strings which contain the location of the image.pack files. Image.pack files are images created by a texture packer to help reduce memory usage and expensive calls to create gfx objects. This guide assumes you are using a texture packer to pack your images.

Next we create a method which we will use to load the images.

This method loads the images and sets the type of class they will be when they are loaded. Here we use TextureAtlas.class as they will each become a TextureAtlas.

Loading Sounds with your Asset Manager

Loading Sounds is the same as loading images except the Sound.class is used to set the type of class that the sound will be loaded as. The example below show how this is done.

Loading Fonts with your Asset Manager

Loading a Btmap font is done by adding the location of the .fnt file and then adding the method to load the .fnt file with the BitmapFont.class

Loading Particle Effects in your Asset Manager

Loading particle Effects is slightly different from the normal method of loading assets as they will need a ParticleEffectParameter. The ParticleEffectParameter is used to define where the images used in the particles is found.

Loading Skins in your Asset Manager

Loading a skin class is similar to loading a particle effect as it requires a parameter from the SkinParameter class. The file used for loading is the JSON file and the parameter uses the .atlas file.

Now you know how to load all these different objects into the asset manager, but how do you get them into your project?

Getting the assets into your project

First you load your MyAssetManager

Now you have your asset manager you need to set which object you want to load. So for example we may only need the images loaded now and don’t need sounds so we would use this code:

Now you may think that the images have been loaded now, but they have not. All we have done is told the asset manager that we want the images to be loaded. In order to start the loading of the images we need to tell our manager to finishLoading();

The finishLoading will method will stop any processing until the assets are loaded. This is especially useful if you split your assets into pre loading and post loading. Pre loading assets would be assets used to display loading bars and post loading stuff would be everything else.

For post loading where you want to show a progress bar showing how far the loading process is going you can use the following code

The above code uses the assMan.manager.update() to start the loading of assets. It will then report false until the asset is fully loaded so our label/progress bar update is in the else clause. This will update the progress bar each time. Once the asset is loaded assMan.manager.update() will return true which will increase the currentLoad level triggering the next set of assets to be loaded. This is repeated until all are loaded and then a screen change is triggered.

Now that all the assets are loaded we can finally use them within our project. In order to do this you will need to use the manager.get() method. This method gets the objects. Example below:

Further Reading

https://github.com/libgdx/libgdx/wiki/Managing-your-assets

http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/assets/AssetManager.html

Sharing is caring!

5 Replies to “Asset Manager – LibGDX Tutorial”

  1. Assman lol

  2. assMan, what a great variable name, hahaha!

  3. Good tutorial, thanks!

  4. i am assman

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.