问题描述
我想在我的安卓游戏中使用非静态 AssetManager?我有很多层次.我应该如何使用 AssetManager?我应该为每个级别创建一个单独的 AssetManager 吗?因为如果我创建一个 AssetManager,它将在第一个屏幕中加载所有纹理等.
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 create one AssetManager, it will load all textures, etc, in first screen.
推荐答案
您应该在您的主类中创建一个 AssetManager,它会在应用程序启动时加载所有资产.然后对于每个级别,您应该根据您的游戏创建不同的屏幕或使用一个屏幕.然后将 AssetManager 的引用传递给您创建的每个新屏幕实例(以便屏幕访问其资产).
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).
例如:
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
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
}
}
这篇关于如何使用 AssetManager 在 libGDX 中构建 ResourceManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!