The libgdx used in this article is version 0.92, which may be different from the latest version. The full text is for reference only.
I haven't written anything about libgdx for a long time, but I don't know what to rewrite.
Recently I have received many emails, mostly about resource loading, especially asynchronous resource loading, and how to implement the loading window. In this blog, I will give a rough answer.
When do I need resource pre-loading?
The purpose of resource pre-loading is clear to improve user experience. Of course, for developers, better pre-loading implementation methods can also be easily managed.
If the game is simple and has very few resources, it is difficult to get resource pre-loading. However, we recommend that you use it in the following situations:
1. A large number of resources
Music, pictures, videos, and so on. If you have too many games, we recommend that you use
2. Reuse of some resources
Some resources will be used repeatedly, such as some background music and some small icons.
Assetmanager in libgdx
Assetmanager is a resource loading solution provided by libgdx. There is an example in the demo. You can refer to it in detail.
Assetmanager is easy to use. It is instantiated first.
Assetmanager =NewAssetmanager ();
You must specify the resource location and type when loading resources. Currently, resources of the following types are supported: pixmaps, textures, bitmapfonts, textureatlases, tiledatlases, tilemaprenderers, music, and sound.
Usage:
Assetmanager. Load ("Data/img/bg.png", texture.Class);
Assetmanager. Load ("Data/font/Chinese. fnt", bitmapfont.Class);
Assetmanager. Load ("Data/music/op_head.ogg", music.Class);
Assetmanager. Load ("Data/Pack/task1.pack", textureatlases.Class);
Custom parameters are also supported during loading, such as when texture is loaded.
Textureparameter =NewTextureparameter ();
Textureparameter. minfilter = texturefilter. Linear;
Textureparameter. genmipmaps =True;
Manager. Load ("Data/img/bg.png", texture.Class, Textureparameter );
In most cases, you do not need to consider custom parameters.
After the load method is executed, only the pre-loaded resources are loaded into the queue. In fact, there is nothing to load. The resource is loaded using the update method.
This method has a return value. If it is true, the load is completed. If it is false, the load is still in progress.
If you need an accurate response, for example, to draw a progress bar, you can use the getprogress method to return a number between 0 and 1.
Use assetmanager. Get (location, type) to obtain resources, such
Assetmanager. Get ("Data/img/bg.png", texture.Class);
Assetmanager. Get ("Data/font/Chinese. fnt", bitmapfont.Class);
If necessary, check it first.
Assetmanager. isloaded ("Data/img/bg.png ")
Precautions for use
To cancel the pre-loading, call the finishloading method.
If you want to destroy the used resources, call the unload method.
Another major problem is that after the game is interrupted, some resources need to be reloaded. Otherwise, numerous errors may occur.