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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package blog.gamedevelopment.loaders; import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.assets.loaders.ParticleEffectLoader.ParticleEffectParameter; import com.badlogic.gdx.assets.loaders.SkinLoader.SkinParameter; import com.badlogic.gdx.audio.Sound; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.ParticleEffect; import com.badlogic.gdx.graphics.g2d.TextureAtlas; import com.badlogic.gdx.scenes.scene2d.ui.Skin; public class MyAssetManager { public final AssetManager manager = new AssetManager(); } |
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.
1 2 3 |
// Textures public final String imagesPack = "images/images.pack"; public final String lazorPack = "lazor/lazor.pack"; |
Next we create a method which we will use to load the images.
1 2 3 4 |
public void loadImages(){ manager.load(imagesPack, TextureAtlas.class); manager.load(lazorPack, TextureAtlas.class); } |
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.
1 2 3 4 5 6 7 8 9 10 |
//Sounds public final String explosionSound = "sounds/explosion.wav"; public final String boingSound = "sounds/boing.wav"; public final String pingSound = "sounds/ping.wav"; public void loadSounds(){ manager.load(explosionSound, Sound.class); manager.load(boingSound, Sound.class); manager.load(pingSound, Sound.class); } |
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
1 2 3 4 5 6 |
// fonts public final String visfont = "font/visitor.fnt"; public void loadFonts(){ manager.load(visfont, 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// Particle Effects public final String sparksPE ="particles/sparks.pe"; public final String explosionPE ="particles/explosion.pe"; public final String lazerPE ="particles/lazer.pe"; public final String pupGetEffectPE ="particles/pupGetEffect.pe"; public final String laserHitPE ="particles/laserHitSparks.pe"; public void loadParticleEffects(){ ParticleEffectParameter pep = new ParticleEffectParameter(); pep.atlasFile = "images/images.pack"; manager.load(sparksPE, ParticleEffect.class, pep); manager.load(explosionPE, ParticleEffect.class, pep); manager.load(lazerPE, ParticleEffect.class, pep); manager.load(pupGetEffectPE, ParticleEffect.class, pep); manager.load(laserHitPE, ParticleEffect.class, pep); } |
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.
1 2 3 4 5 6 7 |
// Skin public final String skin = "uiskin.json"; public void loadSkin(){ SkinParameter params = new SkinParameter("uiskin.atlas"); manager.load(skin, Skin.class, params); } |
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
1 |
public MyAssetManager assMan = new 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:
1 |
assMan.loadImages(); |
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();
1 |
assMan.manager.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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
if (assMan.manager.update()) { // Load some, will return true if done loading currentLoad+= 1; switch(currentLoad){ case FONT: assMan.loadFonts(); loadingLabel.setText("Loading Fonts"); break; case PARTY: assMan.loadParticleEffects(); loadingLabel.setText("Loading Particle Effects"); break; case SOUND: assMan.loadSounds(); loadingLabel.setText("Loading Sounds"); break; case MUSIC: assMan.loadMusic(); loadingLabel.setText("Loading Music"); break; case 5: assMan.loadMusic(); loadingLabel.setText("Loading Fonts"); break; } if (currentLoad >5){ loadingLabel.setText(""); percent = 1; // set bar to full parent.changeScreen(BlockBreaker.MENU); //changes screen } }else{ percent = Interpolation.linear.apply(percent, assMan.manager.getProgress(), 0.05f); } |
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:
1 2 3 4 5 6 7 8 9 10 11 |
// get a texture altas TextureAtlas atlas = assMan.manager.get("gui/loadingGui.pack"); //get a particle effect sparks = assMan.manager.get("particles/sparks.pe",ParticleEffect.class); // get a font BitmapFont font = assMan.manager.get("font/visitor.fnt", BitmapFont.class); //get a sound Sound introMusic = assMan.manager.get("music/saw.mp3"); |
Further Reading
https://github.com/libgdx/libgdx/wiki/Managing-your-assets
http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/assets/AssetManager.html
Assman lol
assMan, what a great variable name, hahaha!
Good tutorial, thanks!
PogChamp
i am assman