Reputation: 81
Both versions work but I am not sure which one to use and why to use that version? Do you need to dispose the TextureAtlas if you unload the assets already?
Version 1: Load the pack file (TexturePacker file with positions data) along with the png.
gameSpriteSheet = new TextureAtlas(Gdx.files.internal(DATA_DIR_PATH + "GameSpritesPack.txt"));
assetManager.load(DATA_DIR_PATH + "GameSpritesPack.txt");
assetManager.load(DATA_DIR_PATH + "GameSpritesPack.png");
// Call only if assetmanager's update method returns true
headerRegion = getTexture("MainMenuHeader", gameSpriteSheet);
To unload:
if (assetManager.isLoaded(DATA_DIR_PATH + "GameSpritesPack.txt")) {
assetManager.unload(DATA_DIR_PATH + "GameSpritesPack.txt");
}
if (assetManager.isLoaded(DATA_DIR_PATH + "GameSpritesPack.png")) {
assetManager.unload(DATA_DIR_PATH + "GameSpritesPack.png");
}
if(gameSpriteSheet != null) {
gameSpriteSheet.dispose();
}
Version 2: Load only the pack file (TexturePacker file with positions data).
assetManager.load(DATA_DIR_PATH + "GameSpritesPack.txt");
// Call only if assetmanager's update method returns true
gameSpriteSheet = assetManager.get(DATA_DIR_PATH + "GameSpritesPack.txt", TextureAtlas.class);
headerRegion = getTexture("MainMenuHeader", gameSpriteSheet);
To unload:
if (assetManager.isLoaded(DATA_DIR_PATH + "GameSpritesPack.txt")) {
assetManager.unload(DATA_DIR_PATH + "GameSpritesPack.txt");
}
if(gameSpriteSheet != null) {
gameSpriteSheet.dispose();
}
Note: I have read Mario's post on AssetManager and other blogs on that but I don't seem to get why the both versions above work fine.
Upvotes: 1
Views: 2780
Reputation: 25177
The AssetManager is for loading assets "in the background". The first version you listed just loads the atlas synchronously in the current thread. Its not actually using the AssetManager at all. This is fine and will work, the downside is that if your atlas is large (or you have a lot of them that you're doing this for), your app will "hang" for a moment while:
gameSpriteSheet = new TextureAtlas(Gdx.files.internal(DATA_DIR_PATH + "GameSpritesPack.txt"));
runs. You could delete the two assetManager
lines in your first example, as they're not doing anything (useful) in this case.
The second version is a "correct" usage of the AssetManager
. Basically you're asking the asset manager to run the code you used in the first version ( ... new TextureAtlas(...)
) on a background thread. You must wait until that background thread completes (i.e., update()
returns true). The idea is that you can do other stuff on the render thread in the foreground (i.e., animate pretty patterns on the screen to distract the user while you load up game assets) while periodically checking if the loading is complete.
Upvotes: 6