Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 遊戲 >> Android 遊戲開發

Android遊戲引擎libgdx使用教學11:如何使用Skin與UI設定檔

編輯:Android 遊戲開發

libgdx 的 UI 改進幅度很大,過去各種稀奇古怪的問題大多已被解決,UI 類型也大致齊全,建議大家下載最新版本來使用。

我認為 UI 唯一較複雜的地方在於各種樣式的設定,例如 TextButton:

public TextButtonStyle (NinePatch down, NinePatch up, NinePatch checked,
    float pressedOffsetX, float pressedOffsetY,
    float unpressedOffsetX, float unpressedOffsetY,
    BitmapFont font, Color fontColor, Color downFontColor, Color checkedFontColor)

再看看 List:

public ListStyle (BitmapFont font, Color fontColorSelected,
    Color fontColorUnselected, NinePatch selectedPatch)

每次使用都要建立好幾個 Texture、NinePatch 之類的物件,多少有點麻煩。幸好 libgdx 提供了解決方案:Skin。Skin 保存了 UI 的樣式與相關資源,定義方式是使用 JSON 或類似 JSON 的格式(API 網址:https://libgdx.l33tlabs.org/docs/api/com/badlogic/gdx/scenes/scene2d/ui/Skin.html)。

先看基本格式:

{
  resources: {
    className: {
      name: value,
      ...
    },
    ...
  },
  styles: {
    className: {
      name: value,
      ...
    },
    ...
  }
}

由「資源」與「樣式」兩大區塊組成。先製作一個 fnt 檔以支援中文。

命名為 chinese.fnt 與 chinese.png。再做一張圖:

全部複製到專案資料夾中(我另外建了一個 UI 資料夾)。先寫個 Label 試試。定義所需的 NinePatch:

com.badlogic.gdx.graphics.g2d.NinePatch: {
    default-rect-red: [
        { width: 2, height: 1, x: 1, y: 43 },
        { width: 1, height: 1, x: 2, y: 43 },
        { width: 2, height: 1, x: 3, y: 43 },
        { width: 2, height: 1, x: 1, y: 45 },
        { width: 1, height: 1, x: 2, y: 45 },
        { width: 2, height: 1, x: 3, y: 45 },
        { width: 2, height: 1, x: 1, y: 43 },
        { width: 1, height: 1, x: 2, y: 43 },
        { width: 2, height: 1, x: 3, y: 43 }
    ],
    default-rect-yellow: [
        { width: 2, height: 1, x: 1, y: 54 },
        { width: 1, height: 1, x: 2, y: 54 },
        { width: 2, height: 1, x: 3, y: 54 },
        { width: 2, height: 1, x: 1, y: 55 },
        { width: 1, height: 1, x: 2, y: 55 },
        { width: 2, height: 1, x: 3, y: 55 },
        { width: 2, height: 1, x: 1, y: 54 },
        { width: 1, height: 1, x: 2, y: 54 },
        { width: 2, height: 1, x: 3, y: 54 }
    ]
}

再定義一個白色:

com.badlogic.gdx.graphics.Color: {
    white: { a: 1, b: 1, g: 1, r: 1 }
}

然後是我們的字型:

com.badlogic.gdx.graphics.g2d.BitmapFont: {
    default-font: { file: chinese.fnt }
}

接著在樣式中宣告一個 Label 樣式:

com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle: {
    default: { font: default-font, fontColor: white }
}

使用時:

Skin skin = new Skin(Gdx.files.internal("ui/uiskin.json"),
    Gdx.files.internal("ui/ui.jpg")); // 載入樣式

final Label label = new Label("FPS:",
    skin.getStyle("default", LabelStyle.class), "fpsLabel"); // 取得 Label 樣式

效果:

接著再來試試 TextButton:

com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle: {
    default: { down: default-rect-red, up: default-rect-yellow,
        fontColor: white, font: default-font }
}

呼叫方式:

final TextButton textButton = new TextButton("確認",
    skin.getStyle("default", TextButtonStyle.class), "okButton");

效果:

按下去的時候會變成黃色(沒截到圖)。Skin 真的很方便,特別是當你大量使用 libgdx 的 UI 時。

寫在最後

1. Skin 支援 XML 嗎?說實話我也很想知道,因為 gdx-tests 裡的 Skin 設定檔有兩種,一種是 JSON 格式,另一種是 XML 格式,但我試著直接載入 XML 會報錯。其實我比較推薦 XML 格式,因為 JSON 書寫時容易出錯(少個逗號或括號之類的),而且 Eclipse 的自動縮排似乎沒發揮作用(難不成是設定問題?)。

2. Skin 不支援一個設定檔對應多個圖片檔,這種情況建議把圖片合併,或者乾脆用多個 Skin。

3. Skin 的圖片定義原點是左上角。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved