I want use non static AssetManager In my android game? I have many levels. How should I use AssetManager? I should create for every level a separate AssetManager? Because if I c
You should create one AssetManager in your main class which load all assets at the startup of the application. Then for each level you should create a different screen or use one screen, depending on your game. Then you pass a reference of the AssetManager to every new screen instance you create (for the screen to access it's assets).
For example:
Main.java
public class Main extends Game {
private GameScreen gameScreen; // the game screen
private AssetManager assetManager; // for loading all assets
public void create(){
assetManager = new AssetManager();
assetManager.load("assets/data/yourSkin", Skin.class);
assetManager.finishLoading(); // load assets (not asynchron for this example)
setGameScreen();
}
public void setGameScreen(){
setScreen(new GameScreen(this));
}
public void dispose() {
assetManager.dispose(); // disposes all assets when the game exits
}
// so that screens can get the assetManager from the main class
public AssetManager getAssetManager() { return assetManager; }
}
GameScreen.java
public class GameScreen implements Screen {
private Main mainClass;
private AssetManager assetManager;
private Skin skin; // holds fonts, button style, ...
public GameScreen(Main mc) {
// init
this.mainClass = mc;
assetManager = mc.getAssetManager();
// load skin
skin = assetManager.get("assets/data/skin", Skin.class);
// make GUI...
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// draw
}
}