libgdx的UI API

kac*_*ous 14 android libgdx

Android和libgdx noob在这里.

有没有人知道最近为libgdx发布的UI API?请参阅此处的博文:http://www.badlogicgames.com/wordpress/?p = 2058

我想创建一个基本的菜单系统,我想知道这个UI API是否会使它更容易.

Dor*_*ran 24

已更新以反映对LibGDX的更改

我处于类似的位置,以下代码为我创建了一个基本菜单(一个按钮容器).代码不会按原样运行,因为它使用了我的一些类,但真正重要的是create方法的内容.这会创建一个居中的标题,然后容器中的一些按钮会居中,然后是左下角的fps标签和右下角的图像.主题文件和一些图像来自LibGDX测试资产.

我已经将它与JOGL,LWJGL和Android应用程序类一起使用了.我在Droid 2上运行它并让它像在桌面上一样运行.希望这可以让你开始.

public class MenuScreen extends Screen{
private Stage ui;
private Table window;
@Override
public void create(final Game game) {
    super.create(game);
    TextureRegion image = new TextureRegion(new Texture(Gdx.files.internal(Art.badlogicSmall)));
    Label fps = new Label("fps: ", Art.sSkin.getStyle(LabelStyle.class),"fps");
    ui = new Stage(Gdx.graphics.getWidth(),Gdx.graphics.getHeight(), true);
    Gdx.input.setInputProcessor(ui);
    window = new Table("window");
    window.width = ui.width();
    window.height = ui.height();
    window.x = 0;
    window.y = 0;
    window.debug();
    Label title = new Label("Title",Art.sSkin.getStyle(LabelStyle.class),"title");
    Button newGame = new Button("New Game",Art.sSkin.getStyle(ButtonStyle.class),"new");
    newGame.setClickListener(new ClickListener() {
        @Override
        public void click(Actor actor) {
            game.setScreen(GameScreen.class);               
        }
    });
    Button optionMenu = new Button("Option",Art.sSkin.getStyle(ButtonStyle.class),"Options");
    Button helpMenu = new Button("Help",Art.sSkin.getStyle(ButtonStyle.class),"Help");
    Image libgdx = new Image("libgdx", image);
    window.row().fill(false,false).expand(true,false).padTop(50).padBottom(50);
    window.add(title);
    Table container = new Table("menu");
    container.row().fill(true, true).expand(true, true).pad(10, 0, 10, 0);
    container.add(newGame);
    container.row().fill(true, true).expand(true, true).pad(10, 0, 10, 0);
    container.add(optionMenu);
    container.row().fill(true, true).expand(true, true).pad(10, 0, 10, 0);
    container.add(helpMenu);
    window.row().fill(0.5f,1f).expand(true,false);
    window.add(container);
    Table extras = new Table("extras");
    extras.row().fill(false,false).expand(true,true);
    extras.add(fps).left().center().pad(0,25,25,0); 
    extras.add(libgdx).right().center().pad(0,0,25,25);
    window.row().fill(true,false).expand(true,true);
    window.add(extras).bottom();
    ui.addActor(window);
}

@Override
public void render(float arg0) {
    Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    ((Label)ui.findActor("fps")).setText("fps: " + Gdx.graphics.getFramesPerSecond());  
    ui.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));
    ui.draw();
    Table.drawDebug(ui);
}
@Override
public void resize(int width, int height) {
    ui.setViewport(width, height, true);
    Log.d("Resize: "+width+", "+height);
}
Run Code Online (Sandbox Code Playgroud)


小智 7

是的,新的UI api非常易于使用.您可以使用Skin对象创建一些Actor对象,然后将它们连接到 Stage对象.

您可以在libgdx源中引用UITest.java文件.它演示了如何使用基本的UI元素.

从低级别看到libgdx新UI,它只包含以下元素:

  • NinePatch:用于创建元素的基本形状对象可以拉伸;
  • Region:固定大小元素的形状对象;
  • 字体:显示文本的bitmapfont对象;

高级元素由它们组成,例如Button对象,包括:ninepatch和font ojbect,依此类推.

因此,您可以非常轻松地创建2D UI使用它们.:)