AssetManager in libgdx

http://ju.outofmemory.cn/entry/25612
http://code.google.com/p/libgdx/wiki/AssetManager

Here, the author lists several advantages of using AssetManager to

load most resources using asynchronous This way, the rendering process can be loaded without blocking the rendering process. Reference counting is
implemented . When both A and B depend on the C material, C will only be destroyed when both A and B are destroyed. This also means that even if a resource is loaded Many times, one copy in the memory.
Use a single strength to manage all the materials
. It can realize the cache of materials. The method of
creating AssetManager is

AssetManager manager = new AssetManager();

When loading materials, AssetManager needs to know which loader to use. Load this material. These loaders implement the methods in AssetLoaders, which are divided into two categories, SynchronourAssetLoader and AsynchronousAssetLoader. The former loads resources in the rendering process, and the latter loads resources in another process. For example, creating a Pixmap requires Texture, and Some OpenGL materials depend on the rendering process. The following is the correspondence between some resources and loaders that the system has implemented.

Pixmaps via PixmapLoader
Textures via TextureLoader
BitmapFonts via BitmapFontLoader
TextureAtlases via TextureAtlasLoader
TiledAtlases via TiledAtlasLoader
TileMapRenderers via TileMapRendererLoader
Music instances via MusicLoader
Sound instances via SoundLoader How
to use:

manager.load("data/mytexture.png", Texture.class);
manager.load("data/myfont.fnt", BitmapFont.class );
manager.load("data/mymusic.ogg", Music.class);
These resources will be added to the loading queue. If you need to use special initialization parameters when loading resources, you can refer to the following method

//Used by loading TextureParameter The parameters
//The type of loading parameters passed by each resource is different
TextureParameter param = new TextureParameter();
param.minFilter = TextureFilter.Linear;
param.genMipMaps = true;
//You can pass in
manager when loading. load("data/mytexture.png", Texture.class, param);
So far, the material is only added to the loading column, but will not start loading, We need to call frame by frame
AssetManager#update() function

public MyAppListener implements ApplicationListener {

   public void render() {
      if(manager.update()) {
         // updata() returns true, which proves that all resources are loaded
         // and the corresponding operations can be performed
      }

      // Get the loading progress, this is quite tricky, and it will be explained later
      float progress = manager.getProgress()
      ... left to the reader ...
   }
}
If you need to force all loading to complete, you can call
Means that asynchronous loading is designed for synchronization Loading
This function will block until the material in the queue is loaded.

  manager.finishLoading();
The way to get the material is:

Texture tex = manager.get("data/mytexture.png", Texture.class);
BitmapFont font = manager.get ("data/myfont.fnt", BitmapFont.class);
The way to judge whether a single material is loaded is:

if(manager.isLoaded("data/mytexture.png")) {
   // texture is available, let's fetch it and do do something interesting
   Texture tex = manager.get("data/mytexture.png", Texture.class);
}
Destroy the material:

manager.unload("data/myfont.fnt");
The way to destroy all the material is:

manager.clear();

//or
//The difference is that dispose will destroy the AssetManager as well
manager.dispose();
In addition, we can define the way to read the file by ourselves.
For example , if the material is encrypted by itself, we need to decrypt it to read
it. At this time, we can implement our own FileHandleResolver with

a function in it

.
fileName);
just pass it in when

new AssetManager manager = new AssetManager(new ExternalFileHandleResolver());
Another thing to note is that when the program switches to the background, and the
material ,
then the program needs to reload these resources when switching to the foreground.
Use AssetManager to load resources without worrying
, but if not, you can use

Texture.setAssetManager (manager);
way to let AssetManager manage these resources
In addition , maybe the loader provided by the system does not meet our needs, then we can define our own Loader to load our own resources
From the source code of AssetManager, we can see how it is Add Loader:

public AssetManager (FileHandleResolver resolver) {
setLoader(BitmapFont.class, new BitmapFontLoader(resolver));
setLoader(Music.class, new MusicLoader(resolver));
setLoader(Pixmap.class, new PixmapLoader(resolver));
setLoader(Sound.class, new SoundLoader(resolver));
setLoader(TextureAtlas.class, new TextureAtlasLoader(resolver));
setLoader(Texture.class, new TextureLoader(resolver));
setLoader(Skin.class, new SkinLoader(resolver));
setLoader(TileAtlas.class, new TileAtlasLoader(resolver));
setLoader(TileMapRenderer.class, new TileMapRendererLoader(resolver) ); //..........Do
not look at the code
}
If you need to define your own Loader, you can refer to these Loaders to implement
MusicLoader Synchronous Loader

public class MusicLoader extends SynchronousAssetLoader<Music, MusicLoader.MusicParameter> {
public MusicLoader ( FileHandleResolver resolver) {
super(resolver);
}

        //When calling load, return the object to be created
@Override
public Music load (AssetManager assetManager, String fileName, MusicParameter parameter) {
return Gdx.audio.newMusic(resolve(fileName));
}

        //Here returns the dependency array of this material
        //For example, bitmapfont needs text images outside the configuration file
        //Then you can return
@Override
public Array<AssetDescriptor> getDependencies ( String fileName, MusicParameter parameter) {
return null;
}

static public class MusicParameter extends AssetLoaderParameters<Music> {
}
}
Asynchronous loader PixmapLoader

public class PixmapLoader extends AsynchronousAssetLoader<Pixmap, PixmapLoader.PixmapParameter> {
public PixmapLoader (FileHandleResolver resolver) {
super(resolver );
}

Pixmap pixmap;

        //non-rendering process calls the function
        //no return value
@Override
public void loadAsync (AssetManager manager, String fileName, PixmapParameter parameter) {
pixmap = null;
pixmap = new Pixmap(resolve(fileName));
}

        //The rendering process calls the function
        //Returns the object to be created, and returns a null proof Not loaded
@Override
public Pixmap loadSync (AssetManager manager, String fileName, PixmapParameter parameter) {
return pixmap;
}
        //Similarly, return dependencies
@Override
public Array<AssetDescriptor> getDependencies (String fileName, PixmapParameter parameter) {
return null;
}

static public class PixmapParameter extends AssetLoaderParameters<Pixmap> {
}
}
ok, now I complain about the loading progress of AssetManager. If

I call the AssetManager#load function twice
, the returned progress will only appear 0, 0.5, 1.

That is to say, the progress of AssetManager does not consider the dependent resources at all. How many loads are calculated?
Therefore, unless there are too many resources, don't expect the progress of his return to be smooth.

In my opinion, this is a big problem..

Like
libgdx

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326270236&siteId=291194637