您很可能必须将图像放在android/assets 文件夹中。创建按钮的方式看起来不错,但是 LibGDX 提供了一种更方便的方式来创建按钮,需要提前做一些工作。这就是我创建按钮的方式:
首先我创建一个像这样的图像:
然后,我使用 TexturePacker 将所有 UI 图像打包在一起。这会生成一个图像表和一个 txt 文件,看起来像这样,但没有 split 参数:
uiskin.png
format: RGBA8888
filter: Linear,Linear
repeat: none
container_blue
rotate: false
xy: 17, 2
size: 41, 45
split: 20, 20, 22, 22
orig: 41, 45
offset: 0, 0
index: -1
container_gold
rotate: false
xy: 60, 2
size: 41, 45
split: 20, 20, 22, 22
orig: 41, 45
offset: 0, 0
index: -1
分割参数旨在使这些图像正确拉伸。 I once asked a question hero on SO how they work. 所以你要做的就是在你的绘图应用中查找图像,看看哪些部分需要拉伸并填写分割参数。
精灵表和 txt 文件一起被称为 TextureAtlas,一旦你创建了一个像 TextureAtlas atlas = new TextureAtlas("uiskin.png"); 这样的图集,你可以通过 atlas.findRegion("container_blue") 请求纹理,但是这是一个相当昂贵的调用,所以不要这样做每次更新。
现在,对于真正的魔力,LibGDX 提供了一个皮肤,您可以在其中存储样式。皮肤文件采用.json 格式,如下所示:
{
com.badlogic.gdx.graphics.g2d.BitmapFont: {
default: { file: fonts/myDefaultFont.fnt }
large: { file: fonts/myLargeFont.fnt }
},
com.badlogic.gdx.graphics.Color: {
white: { a: 1, r: 1, g: 1, b: 1 }
green: { a: 1, r: 0, g: 1, b: 0 }
gray: { a: 1, r: 0.5, g: 0.5, b: 0.5 }
black: { a: 1, r: 0, g:0, b:0 }
},
com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle: {
default: { font: default, fontColor: white }
container_gold: { background: container_gold, font: default, fontColor: green }
},
com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle: {
default: { font: large, fontColor: white }
container_gold: { down: container_gold, up: container_blue, disabled: container_grey, font: large, fontColor: green }
container_gold_small: { down: container_gold, up: container_blue, disabled: container_grey, font: default, fontColor: green }
},
}
其实很简单。首先,我找到我的字体并命名它们,然后为我的字体添加一些颜色。从那里我为我的标签添加了两种样式,为我的 TextButtons 添加了 3 种样式。如果我将它加载到像 Skin skin = new Skin("Gdx.files.internal("uiskin.json"), atlas); 这样的 Skin 中,我可以使用我使用单行创建的样式创建许多按钮。
//This would create a "larger button using the default font.
TextButton textButton = new TextButton("Click Me!", skin, "container_gold");
//This would create a smaller button
TextButton textButton = new TextButton("Click Me!", skin, "container_gold_small");
//This would create a label looking exactly like the first button
Label label = new Label("Label", skin, "container_gold");
//This would draw smaller white text without a background using the default style
Label label = new Label("Default text", skin);
提前做了一些工作,但效果很好。还有一件事,如果你有很多屏幕,你不应该每次都加载一个新皮肤。您应该使用AssetManager。这更容易设置,您只需要一个小班。我使用资产描述符加载我的资产,当我需要查找我的皮肤或图集时,我无需输入更多字符。
public class Assets {
//The asset manager
public static AssetManager manager = new AssetManager();
//The atlas, I renamed .txt to pack (just a habit).
public static final AssetDescriptor<TextureAtlas> uiAtlas =
new AssetDescriptor<TextureAtlas>("ui/uiskin.pack", TextureAtlas.class);
//The skin
public static final AssetDescriptor<Skin> uiSkin =
new AssetDescriptor<Skin>("ui/uiskin.json", Skin.class,
new SkinLoader.SkinParameter("ui/uiskin.pack"));
//Method for loading the assets into the manager
public static void load()
{
manager.load(uiAtlas);
manager.load(uiSkin);
}
//Easy asset disposing, whenever you are done with it just dispose the manager instead of many files.
public void dispose()
{
manager.dispose();
}
}
您可以使用启动画面和快速加载栏来使用它,但基本上您需要做的就是将以下代码作为运行的第一行代码。
//This is on top of my first `create()` method.
//Tell the manager to start loading
Assets.load();
//Tell the program to "loop" the loading until finished. Essentially stopping the game from continuing.
Assets.manager.finishLoading();
如果您发布游戏并且加载时间超过几秒钟,那么您最好使用某种启动/加载屏幕。