Search code examples
javaandroidlibgdx

Disposing Assets in Libgdx


I am managing my assets using Assets class which has assetManager

In game screens(eg. SplashScreen or MenuScreen) I am loading assets which I will use in this screen using assetManager.load() and when game setScreen to another screen I call dispose() of the previous one ?

Should I call assetManager.clear() in dispose() of screen ?

Or calling it in dispose of my game class (which I use right now) ?

something like this :

public class GameMain extends Game {
    @Override
        public void dispose(){
            super.dispose();
            Assets.dispose();
   }
}

Solution

  • Sounds like you'll want to call it at the screen level. Calling clear on the asset manager will dispose ALL assets the manager knows about. So each screen will have to load all the assets it needs to use, as the manager is effectively cleared each screen.

    If you dispose your assets at the game level like you do now, sounds like you may have a lot of extra assets loaded that you don't need and could affect performance (ie, your menu assets are still loaded when you're showing your play screen) .

    If you have assets you share between screens, keep that in mind. Calling clear at the screen level will have you reloading those assets every screen switch - which may be just fine depening on the amount/size of your assets.