AssetManagerX - GameDevWeek/CodeBase GitHub Wiki

Introduction

LibGDX uses an [AssetManager] to load resources. But you still need to tell it manually what to load etc. AssetManagerX extends AssetManager to allow loading resouce lists from JSON files.

Note that calling loadAssetList, etc. only prepares the loading. The actual loading takes place during the loading screen so the game won't freeze until done (see LoadGameState).

Creating an instance:

private final AssetManagerX assetManager = new AssetManagerX();

loadAssetList

This allows to load a simple JSON file that looks like this:

{
    "image_1": "data/images/image_1.png",
    "image_2": "data/images/image_2.png",
...
    "image_n": "data/images/image_n.png"
}

(I.e. a map from key to filepath). To load all resourcesreferences in this JSON file, supply the assetManager with the filepath, a classtype and an AssetLoaderParameters object if required:

TextureParameter param = new TextureParameter();
param.minFilter = param.magFilter = Texture.TextureFilter.Linear;
assetManager.loadAssetList("data/json/images.json", Texture.class, param);

assetManager.loadAssetList("data/json/sounds.json", Sound.class, null);

loadAssetListWithParam

Now let's say, your AssetLoaderParameters vary for each file. In this case, you can put them into the JSON file like this:

{
	"player_team0": {
		"filename": "data/animations/player/team0.png",
		"frames" : 12,
		"frameDuration": [0.04],
		"playType" : "LOOP",
		"columns": 12,
		"rows" : 1
	},
...
}

(I.e. a map from key to object, where object contains a key "filename"). To load all animations references in this JSON file, supply the assetManager with the filepath, a classtype and a classtype for the AssetLoaderParameters:

assetManager.loadAssetListWithParam("data/json/animations.json", AnimationExtended.class,
		AnimationExtendedLoader.AnimationExtendedParameter.class);

Shortcuts for getting resources

A couple of methods to allow for a shorter syntax for accessing resources are already built-in:

AnimationExtended getAnimation(String name);
BitmapFont getFont(String name);
TiledMap getTiledMap(String name);
Music getMusic(String name);
Sound getSound(String name); // supports random
Pixmap getPixmap(String name);
TextureAtlas getTextureAtlas(String name);
Texture getTexture(String name);
ParticleEffect getParticleEffect(String name);

Getting resources the explicit way:

The above all basicly just call this:

T getByName(String name, Class<T> type);
// example:
texture = assetManager.getByName("image_1", Texture.class);

Except for getSound(), which uses a special variation, which supports random:

T getByNameWithRandom(String name, Class<T> type);

getByNameWithRandom supports chosing a random resource. To allow this, the name parameter must end with a "/" and the JSON file must have entries for a resource like this:

...
"footstep/0": "data/sounds/footstep_0.wav",
"footstep/1": "data/sounds/footstep_1.wav",
...

I.e. the key must end with a "/" and a number starting with 0. These numbers must be continuous.

Example for getting a random footstep sound:

sound = assetManager.getSound("footstep/");