You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LibGDX自定义字体加载失败,抛出SerializationException错误求助

Fixing LibGDX Custom Font Loading Error

The SerializationException you're seeing happens because you're trying to load a .fnt bitmap font file directly into a Skin—but Skin expects a JSON/XML skin definition file, not a raw font description. Let's fix this with two straightforward approaches:


Approach 1: Use BitmapFont Directly (Quick Fix)

Skip the Skin entirely for font loading and use BitmapFont directly, which is designed to parse .fnt files:

// Load the custom bitmap font
BitmapFont coolCrayonFont = new BitmapFont(Gdx.files.internal("font/Bitmap.fnt"));
coolCrayonFont.setColor(Color.WHITE); // Set your preferred font color

// Create UI elements with the font
Label.LabelStyle labelStyle = new Label.LabelStyle(coolCrayonFont, Color.WHITE);
Label heading = new Label(QuizMain.TITLE, labelStyle);

TextButton.TextButtonStyle buttonStyle = new TextButton.TextButtonStyle();
buttonStyle.font = coolCrayonFont;
buttonStyle.fontColor = Color.WHITE;
// Optional: Add hover/down states for better UX
buttonStyle.downFontColor = Color.GRAY;
TextButton buttonMap = new TextButton("Map", buttonStyle);

// Build your tables as before
behindTable = new Table();
table = new Table();
table.add(heading).spaceBottom(100).row();
table.add(buttonMap).spaceLeft(15);

Key Checks for This Method:

  • Ensure Bitmap.fnt and Bitmap_0.tga are both in the assets/font/ directory (the .fnt file references the .tga texture, so they must be together).
  • Open Bitmap.fnt in a text editor to confirm it points to the correct texture: look for a line like page id=0 file="Bitmap_0.tga". If not, rename the texture or edit the .fnt file to match.

Approach 2: Integrate the Font with a Skin (Best for Scalable UI)

If you want to use a Skin to manage all your UI styles, create a simple skin JSON file and load the font properly:

  1. Create a skin definition file (e.g., assets/font/skin.json):
{
  "com.badlogic.gdx.graphics.g2d.BitmapFont": {
    "coolCrayon": { "file": "Bitmap.fnt" }
  },
  "com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle": {
    "default": { "font": "coolCrayon", "fontColor": "#ffffff" }
  },
  "com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle": {
    "default": {
      "font": "coolCrayon",
      "fontColor": "#ffffff",
      "downFontColor": "#cccccc"
    }
  }
}
  1. Load the Skin correctly in your code:
Skin skin = new Skin();

// Load the font texture first
Texture fontTexture = new Texture(Gdx.files.internal("font/Bitmap_0.tga"));
fontTexture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear); // Smoother scaling

// Add the font to the skin
BitmapFont coolCrayonFont = new BitmapFont(Gdx.files.internal("font/Bitmap.fnt"), new TextureRegion(fontTexture), false);
skin.add("coolCrayon", coolCrayonFont);

// Load the skin styles from the JSON file
skin.load(Gdx.files.internal("font/skin.json"));

// Use the skin for your UI elements
behindTable = new Table(skin);
table = new Table(skin);
Label heading = new Label(QuizMain.TITLE, skin); // Uses default label style
TextButton buttonMap = new TextButton("Map", skin); // Uses default button style

// Build your tables
table.add(heading).spaceBottom(100).row();
table.add(buttonMap).spaceLeft(15);

Important Notes:

  • Always dispose of your BitmapFont and Texture in your screen's dispose() method to avoid memory leaks:
    @Override
    public void dispose() {
        coolCrayonFont.dispose();
        fontTexture.dispose();
        skin.dispose();
    }
    
  • If you run into texture issues, try converting the .tga to .png (update the .fnt file to reference the .png instead) — LibGDX handles PNGs reliably across all platforms.

内容的提问来源于stack exchange,提问作者Filip

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:07:53