How to build a ResourceManager in libGDX with AssetManager

后端 未结 1 1802
生来不讨喜
生来不讨喜 2021-01-27 05:39

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

相关标签:
1条回答
  • 2021-01-27 06:06

    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
    }
    }
    
    0 讨论(0)
提交回复
热议问题