简体   繁体   中英

How to handle AssetManager in multiple screens

public static AssetManager assets = new AssetManager(); should be avoided, however how do I handle an AssetManager in multiple screens? Is this a good solution?

public class myGame {

   assetMananger manager = new AssetManager(); 
   ...
   public assetManager getAssetMananger(){
      return manager; 
   }
}

public class GameScreen implements Screen{

    private AssetManager manager;

    public GameScreen(myGame game){
       manager = game.getManager();
    }

    public void render(){
        manager.load(...);
    }

Gdx.app.getApplicationListener() return ApplicationListener instance. You can typecast to your implemented class and then easily access any method or data member of that class.

In this way :

((GdxTest)Gdx.app.getApplicationListener()).assets  // <-- You can use from where you want in your project. 

ApplicationListener implemented class :

public class GdxTest extends ApplicationAdapter {

    public Assets assets;

    @Override
    public void create() {

        assets= new Assets();
    }

    ....

    @Override
    public void dispose() {
       assets.dispose();
    }
}

Assets class that handle all assets/resources of Game

public class Assets implements Disposable {

    public AssetManager assetManager;

    public Assets(){

        assetManager=new AssetManager();   
    }

    ...

    @Override
    public void dispose() {
        assetManager.dispose();
    }
}

  • Either load all resources in create() method and show splash screen while all your data are loading to AssetManager . (best way for small project)

  • else for big projects having lots of resources unload unnecessary resources -> load new resources, while loading show loading screen to user.

My approach is always like this:-

Create my own manager class that receive instance if AssetsManager and load all game assets when game is initially loaded

public class Assets implements Disposable{
    public static final Assets instance = new Assets();
    private AssetManager manager;

    // Individual assets
    public PlayerAssets playerAssets;

    private Assets(){}

    public void init(AssetManager manager){
        this.manager = manager;
        manager.load(..);
        manager.finishLoading();
        TextureAtlas atlas = manager.get(..);
        playerAssets = new PlayerAssets(atlas);
    }
    public void dispose(){
        manager.dispose();
    }
    /** Sample Inner class for catching loaded game assets */ 
    public PlayerAssets{
        public final AtlasRegion legs;
        PlayerAssets(TextureAtlas atlas){
            legs = atlas.find(..);
        }
    }
}

On your game class you load all game assets.

public class MyGame extends Game {
    @Override
    public void create(){
        Assets.instance.init(new AssetManager()); // loading your resources
        setScreen(new GameScreen());
    }
}

Update from the comment below

Disposing your assets. You might want to create an abstract class for all your screen as follows

public abstract class AbstractScreen implements Screen{
    protected Game game;
    public AbstractScreen(Game game){
        this.game = game;
    }
}

. . . . . . 

public void dispose(){
    Assets.instance.dispose();
}

Then all of your Screens will extend this class. eg

public class MenuScreen extends AbstractScreen {
    public MenuScreen(Game game){
       super(game);
    }

}

Calling screen instance dispose method will dispose all of the resources, you can override it for places where you might prefer different behaviour eg

@everride
public void dispose(){
    // super.dispose();
    // Release other screen resources
}

Where to call screen dispose is definitely up to you. Also you might want to show loading screen when you load the assets to avoid blank screen due to loading delays If you override dispose as above disposing a screen wont dispose the assets

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM