libgdx 加载资产,纹理打包器
libgdx loading asset, texture packer
抱歉打扰了,我知道已经有人问过这个问题,但我无法解决。我已经开始使用 libgdx 并决定有一本书作为教程 - "Learning LigbGDX Game Development"。一切顺利,直到第 4 章,我无法将资产加载到 canyonbunny.pack。我不知道发生了什么。在 ../CanyonBunny-android/assets/images 中创建了两个文件 - 一个是 png,另一个是 atlas。如果您能帮助我,我将不胜感激。
DesktopLauncher.java
package com.packtub.libgdx.canyonbunny.desktop;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.packtub.libgdx.canyonbunny.CanyonBunnyMain;
import com.badlogic.gdx.tools.texturepacker.TexturePacker;
import com.badlogic.gdx.tools.texturepacker.TexturePacker.Settings;
public class DesktopLauncher {
private static boolean rebuildAtlas = true;
private static boolean drawDebugOutline = true;
public static void main (String[] args) {
if (rebuildAtlas){
Settings settings = new Settings();
settings.maxWidth = 1024;
settings.maxHeight = 1024;
settings.debug = drawDebugOutline;
TexturePacker.process(settings, "assets-raw/images",
"..CanyonBunny-android/assets/images",
"canyonbunny.pack");
}
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.title = "CanyonBunny";
config.useGL30 = false;
config.width = 800;
config.height = 480;
new LwjglApplication(new CanyonBunnyMain(), config);
}
}
Assets.java
package com.packtub.libgdx.canyonbunny.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetDescriptor;
import com.badlogic.gdx.assets.AssetErrorListener;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.utils.Disposable;
import com.packtub.libgdx.canyonbunny.util.Constants;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
public class Assets implements Disposable, AssetErrorListener{
public static final String TAG = Assets.class.getName();
public static final Assets instance = new Assets();
private AssetManager assetManager;
//singleton: previene de inicializaciones desde otras clases
private Assets () {}
public AssetBunny bunny;
public AssetRock rock;
public AssetGoldCoin goldCoin;
public AssetFeather feather;
public AssetLevelDecoration levelDecoration;
public void init (AssetManager assetManager){
this.assetManager = assetManager;
//Se establece el asset manager que maneja los errores
assetManager.setErrorListener(this);
//Carga las texturas atlas
assetManager.load(Constants.TEXTURE_ATLAS_OBJECTS, TextureAtlas.class);
//Se inicia a cargar los assets y se espera que termine
assetManager.finishLoading();
Gdx.app.debug(TAG, "# de assets cargados: "
+ assetManager.getAssetNames().size);
for (String a : assetManager.getAssetNames()) Gdx.app.debug(TAG, "asset: " + a);
TextureAtlas atlas = assetManager.get(Constants.TEXTURE_ATLAS_OBJECTS);
//activa la opcion "texture filtering" para una dibujado mas suave de los pixeles
for (Texture t : atlas.getTextures()) t.setFilter(TextureFilter.Linear, TextureFilter.Linear);
//Crea los objetos del juego
bunny = new AssetBunny(atlas);
rock = new AssetRock(atlas);
goldCoin = new AssetGoldCoin(atlas);
feather = new AssetFeather(atlas);
levelDecoration = new AssetLevelDecoration(atlas);
}
@Override
public void dispose(){
assetManager.dispose();
}
@Override
public void error(AssetDescriptor asset, Throwable throwable) {
Gdx.app.error(TAG, "No se pudo cargar el asset '" + asset + "'", (Exception)throwable);
}
//public void error(String filename, Class type, Throwable throwable){
//Gdx.app.error(TAG, "No se pudo cargar el asset '" + filename + "'", (Exception)throwable);
//}
public class AssetBunny {
public final AtlasRegion head;
public AssetBunny (TextureAtlas atlas){
head = atlas.findRegion("bunny_head");
}
}
public class AssetRock {
public final AtlasRegion edge;
public final AtlasRegion middle;
public AssetRock(TextureAtlas atlas){
edge = atlas.findRegion("rock_edge");
middle = atlas.findRegion("rock_middle");
}
}
public class AssetGoldCoin {
public final AtlasRegion goldCoin;
public AssetGoldCoin(TextureAtlas atlas){
goldCoin = atlas.findRegion("item_gold_coin");
}
}
public class AssetFeather{
public final AtlasRegion feather;
public AssetFeather(TextureAtlas atlas){
feather = atlas.findRegion("item_feather");
}
}
public class AssetLevelDecoration{
public final AtlasRegion cloud01;
public final AtlasRegion cloud02;
public final AtlasRegion cloud03;
public final AtlasRegion mountainLeft;
public final AtlasRegion mountainRight;
public final AtlasRegion waterOverlay;
public AssetLevelDecoration(TextureAtlas atlas){
cloud01 = atlas.findRegion("cloud01");
cloud02 = atlas.findRegion("cloud02");
cloud03 = atlas.findRegion("cloud03");
mountainLeft = atlas.findRegion("mountain_left");
mountainRight = atlas.findRegion("mountain_right");
waterOverlay = atlas.findRegion("water_overlay");
}
}
}
Console
images Packing......... Writing 1024x1024:
..CanyonBunny-android\assets\images\canyonbunny.pack.png
com.packtub.libgdx.canyonbunny.game.Assets: No se pudo cargar el asset
'images/canyonbunny.pack, com.badlogic.gdx.graphics.g2d.TextureAtlas'
com.badlogic.gdx.utils.GdxRuntimeException: File not found:
images\canyonbunny.pack (Internal) at
com.badlogic.gdx.files.FileHandle.read(FileHandle.java:136) at
com.badlogic.gdx.graphics.g2d.TextureAtlas$TextureAtlasData.(TextureAtlas.java:103)
at
com.badlogic.gdx.assets.loaders.TextureAtlasLoader.getDependencies(TextureAtlasLoader.java:58)
at
com.badlogic.gdx.assets.loaders.TextureAtlasLoader.getDependencies(TextureAtlasLoader.java:34)
at
com.badlogic.gdx.assets.AssetLoadingTask.handleSyncLoader(AssetLoadingTask.java:98)
at
com.badlogic.gdx.assets.AssetLoadingTask.update(AssetLoadingTask.java:87)
at
com.badlogic.gdx.assets.AssetManager.updateTask(AssetManager.java:466)
at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:354)
at
com.badlogic.gdx.assets.AssetManager.finishLoading(AssetManager.java:377)
at com.packtub.libgdx.canyonbunny.game.Assets.init(Assets.java:35)
at
com.packtub.libgdx.canyonbunny.CanyonBunnyMain.create(CanyonBunnyMain.java:25)
at
com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:137)
at
com.badlogic.gdx.backends.lwjgl.LwjglApplication.run(LwjglApplication.java:114)
com.packtub.libgdx.canyonbunny.game.Assets: # de assets cargados: 0
Exception in thread "LWJGL Application"
com.badlogic.gdx.utils.GdxRuntimeException: Asset not loaded:
images/canyonbunny.pack at
com.badlogic.gdx.assets.AssetManager.get(AssetManager.java:110) at
com.packtub.libgdx.canyonbunny.game.Assets.init(Assets.java:40) at
com.packtub.libgdx.canyonbunny.CanyonBunnyMain.create(CanyonBunnyMain.java:25)
at
com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:137)
at
com.badlogic.gdx.backends.lwjgl.LwjglApplication.run(LwjglApplication.java:114)
TexturePacker 自动放置由 settings.atlasExtension (".atlas") 定义的扩展。
更改此设置或将 Constants.TEXTURE_ATLAS_OBJECTS 变量的值更改为 "canyonbunny.pack.atlas"。
将设置 atlasExtension 设置为“”。
if (rebuildAtlas) {
Settings settings = new Settings();
settings.maxWidth = 1024;
settings.maxHeight = 1024;
settings.duplicatePadding = false;
settings.atlasExtension="";
...
}
抱歉打扰了,我知道已经有人问过这个问题,但我无法解决。我已经开始使用 libgdx 并决定有一本书作为教程 - "Learning LigbGDX Game Development"。一切顺利,直到第 4 章,我无法将资产加载到 canyonbunny.pack。我不知道发生了什么。在 ../CanyonBunny-android/assets/images 中创建了两个文件 - 一个是 png,另一个是 atlas。如果您能帮助我,我将不胜感激。
DesktopLauncher.java
package com.packtub.libgdx.canyonbunny.desktop;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.packtub.libgdx.canyonbunny.CanyonBunnyMain;
import com.badlogic.gdx.tools.texturepacker.TexturePacker;
import com.badlogic.gdx.tools.texturepacker.TexturePacker.Settings;
public class DesktopLauncher {
private static boolean rebuildAtlas = true;
private static boolean drawDebugOutline = true;
public static void main (String[] args) {
if (rebuildAtlas){
Settings settings = new Settings();
settings.maxWidth = 1024;
settings.maxHeight = 1024;
settings.debug = drawDebugOutline;
TexturePacker.process(settings, "assets-raw/images",
"..CanyonBunny-android/assets/images",
"canyonbunny.pack");
}
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.title = "CanyonBunny";
config.useGL30 = false;
config.width = 800;
config.height = 480;
new LwjglApplication(new CanyonBunnyMain(), config);
}
}
Assets.java
package com.packtub.libgdx.canyonbunny.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetDescriptor;
import com.badlogic.gdx.assets.AssetErrorListener;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.utils.Disposable;
import com.packtub.libgdx.canyonbunny.util.Constants;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
public class Assets implements Disposable, AssetErrorListener{
public static final String TAG = Assets.class.getName();
public static final Assets instance = new Assets();
private AssetManager assetManager;
//singleton: previene de inicializaciones desde otras clases
private Assets () {}
public AssetBunny bunny;
public AssetRock rock;
public AssetGoldCoin goldCoin;
public AssetFeather feather;
public AssetLevelDecoration levelDecoration;
public void init (AssetManager assetManager){
this.assetManager = assetManager;
//Se establece el asset manager que maneja los errores
assetManager.setErrorListener(this);
//Carga las texturas atlas
assetManager.load(Constants.TEXTURE_ATLAS_OBJECTS, TextureAtlas.class);
//Se inicia a cargar los assets y se espera que termine
assetManager.finishLoading();
Gdx.app.debug(TAG, "# de assets cargados: "
+ assetManager.getAssetNames().size);
for (String a : assetManager.getAssetNames()) Gdx.app.debug(TAG, "asset: " + a);
TextureAtlas atlas = assetManager.get(Constants.TEXTURE_ATLAS_OBJECTS);
//activa la opcion "texture filtering" para una dibujado mas suave de los pixeles
for (Texture t : atlas.getTextures()) t.setFilter(TextureFilter.Linear, TextureFilter.Linear);
//Crea los objetos del juego
bunny = new AssetBunny(atlas);
rock = new AssetRock(atlas);
goldCoin = new AssetGoldCoin(atlas);
feather = new AssetFeather(atlas);
levelDecoration = new AssetLevelDecoration(atlas);
}
@Override
public void dispose(){
assetManager.dispose();
}
@Override
public void error(AssetDescriptor asset, Throwable throwable) {
Gdx.app.error(TAG, "No se pudo cargar el asset '" + asset + "'", (Exception)throwable);
}
//public void error(String filename, Class type, Throwable throwable){
//Gdx.app.error(TAG, "No se pudo cargar el asset '" + filename + "'", (Exception)throwable);
//}
public class AssetBunny {
public final AtlasRegion head;
public AssetBunny (TextureAtlas atlas){
head = atlas.findRegion("bunny_head");
}
}
public class AssetRock {
public final AtlasRegion edge;
public final AtlasRegion middle;
public AssetRock(TextureAtlas atlas){
edge = atlas.findRegion("rock_edge");
middle = atlas.findRegion("rock_middle");
}
}
public class AssetGoldCoin {
public final AtlasRegion goldCoin;
public AssetGoldCoin(TextureAtlas atlas){
goldCoin = atlas.findRegion("item_gold_coin");
}
}
public class AssetFeather{
public final AtlasRegion feather;
public AssetFeather(TextureAtlas atlas){
feather = atlas.findRegion("item_feather");
}
}
public class AssetLevelDecoration{
public final AtlasRegion cloud01;
public final AtlasRegion cloud02;
public final AtlasRegion cloud03;
public final AtlasRegion mountainLeft;
public final AtlasRegion mountainRight;
public final AtlasRegion waterOverlay;
public AssetLevelDecoration(TextureAtlas atlas){
cloud01 = atlas.findRegion("cloud01");
cloud02 = atlas.findRegion("cloud02");
cloud03 = atlas.findRegion("cloud03");
mountainLeft = atlas.findRegion("mountain_left");
mountainRight = atlas.findRegion("mountain_right");
waterOverlay = atlas.findRegion("water_overlay");
}
}
}
Console
images Packing......... Writing 1024x1024: ..CanyonBunny-android\assets\images\canyonbunny.pack.png com.packtub.libgdx.canyonbunny.game.Assets: No se pudo cargar el asset 'images/canyonbunny.pack, com.badlogic.gdx.graphics.g2d.TextureAtlas' com.badlogic.gdx.utils.GdxRuntimeException: File not found: images\canyonbunny.pack (Internal) at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:136) at com.badlogic.gdx.graphics.g2d.TextureAtlas$TextureAtlasData.(TextureAtlas.java:103) at com.badlogic.gdx.assets.loaders.TextureAtlasLoader.getDependencies(TextureAtlasLoader.java:58) at com.badlogic.gdx.assets.loaders.TextureAtlasLoader.getDependencies(TextureAtlasLoader.java:34) at com.badlogic.gdx.assets.AssetLoadingTask.handleSyncLoader(AssetLoadingTask.java:98) at com.badlogic.gdx.assets.AssetLoadingTask.update(AssetLoadingTask.java:87) at com.badlogic.gdx.assets.AssetManager.updateTask(AssetManager.java:466) at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:354) at com.badlogic.gdx.assets.AssetManager.finishLoading(AssetManager.java:377) at com.packtub.libgdx.canyonbunny.game.Assets.init(Assets.java:35) at com.packtub.libgdx.canyonbunny.CanyonBunnyMain.create(CanyonBunnyMain.java:25) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:137) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.run(LwjglApplication.java:114) com.packtub.libgdx.canyonbunny.game.Assets: # de assets cargados: 0 Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Asset not loaded: images/canyonbunny.pack at com.badlogic.gdx.assets.AssetManager.get(AssetManager.java:110) at com.packtub.libgdx.canyonbunny.game.Assets.init(Assets.java:40) at com.packtub.libgdx.canyonbunny.CanyonBunnyMain.create(CanyonBunnyMain.java:25) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:137) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.run(LwjglApplication.java:114)
TexturePacker 自动放置由 settings.atlasExtension (".atlas") 定义的扩展。
更改此设置或将 Constants.TEXTURE_ATLAS_OBJECTS 变量的值更改为 "canyonbunny.pack.atlas"。
将设置 atlasExtension 设置为“”。
if (rebuildAtlas) {
Settings settings = new Settings();
settings.maxWidth = 1024;
settings.maxHeight = 1024;
settings.duplicatePadding = false;
settings.atlasExtension="";
...
}