将Freetypefont放入libgdx皮肤中

15
在我的uiskin.json文件中,我有以下代码: com.badlogic.gdx.graphics.g2d.BitmapFont: { default-font: { file: text/default.fnt } } 当我的默认字体default.fnt在assets文件夹的text文件夹下时,这样做非常好用。
然而,我想要使用一个FreeType字体。
我该如何创建和加载一个FreeType字体到uiskin文件中?
3个回答

15

更新

感谢Jos van Egmond的评论,告诉我如何实现这一点并仍然从AssetManager中加载Skin,因此我决定更新答案。旧答案(以及另一种方法)仍保持不变,请参见下文。

新流程如下:

  • 加载字体文件。
  • 创建一个ObjectMap<String, Object>并将您的字体放入其中。
  • 创建SkinParameter并将其作为参数赋予ObjectMap
  • 通过使用创建的SkinParameter像往常一样通过AsserManager加载您的Skin

代码可能看起来像这样:

/* Load your fonts */
load font1...
load font2...

/* Create the ObjectMap and add the fonts to it */
ObjectMap<String, Object> fontMap = new ObjectMap<String, Object>();
fontMap.put("font1", font1);
fontMap.put("font2", font2);

/* Create the SkinParameter and supply the ObjectMap to it */
SkinParameter parameter = new SkinParameter(fontMap);

/* Load the skin as usual */
assetManager.load("file/location/of/your/skin", Skin.class, parameter);

New

目前被接受的答案实际上有点不正确。这篇博客文章介绍了一种方法,让您在Json文件中引用生成的BitmapFont。步骤大致如下:

  • 使用默认构造函数创建空白皮肤文件。
  • 将字体添加到皮肤中。
  • 将Atlas文件添加到皮肤中。
  • 将Json文件加载到皮肤中。

代码可能会像这样:

// Create a new skin using the default constructor and
// add your fonts to it.
Skin skin = new Skin();
skin.add("myFont", myFont, BitmapFont.class);

// Remember that this texture atlas is NOT automatically disposed by the skin, 
// so keep a reference and dispose of it yourself.
skin.addRegion(new TextureAtlas("file/location/of/your/atlas");
skin.load(Gdx.files.internal("file/location/of/your/skin"));

您现在可以在皮肤Json中像这样执行操作:

...,
font: "myFont",
...
尽管这是一个解决方法,但也意味着您无法(至少我没有找到方法)通过AssetManager加载您的皮肤。不过,您仍然可以通过管理器加载TextureAtlas

1
通过 AssetManager 加载带有自定义字体的皮肤,您可以利用 SkinParameter 类中的资源映射参数,并将 BitmapFont 传递到其中。 - Jos van Egmond
@JosvanEgmond 谢谢,我没有想到这个。我已经更新了答案。 - Charanor
更新已接受的答案。 - Seanoseanohay

7

由于您将在运行时生成它,因此无法提前完成。

我的解决方法是保留默认字体,并通过代码在运行时替换它。您可以使用Skin.add(...)来实现。

BitmapFont newDefaultFont = FreeTypeFontGenerator...;
skin.add("default-font", newDefaultFont, BitmapFont.class);

感谢您的回复...实际上文本并不是空白的,而是使用了“旧”的字体,而不是新添加的字体。 - Seanoseanohay
2
明白了...如果我可以补充一下,当我删除JSON文件中已经存在的"default-font"后,它就起作用了。[链接]http://badlogicgames.com/forum/viewtopic.php?f=11&t=8485&p=38863&hilit=skin+font#p38863[链接] - Seanoseanohay

0

我遇到了同样的问题。目前的答案没有解决它,但有所帮助。在我的情况下,这个方法有效:

FreeTypeFontGenerator fontGenerator = new FreeTypeFontGenerator(Gdx.files.internal("VT323.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter fontParameter = new FreeTypeFontGenerator.FreeTypeFontParameter();

fontParameter.size = 24;
fontParameter.borderWidth = 3.6f;
fontParameter.color = new Color(1, 1, 1, 0.3f);
fontParameter.borderColor = new Color(0, 0, 0, 0.3f);

BitmapFont font = fontGenerator.generateFont(fontParameter);

fontParameter.size = 32;

BitmapFont title = fontGenerator.generateFont(fontParameter);

Skin skin = new Skin(new TextureAtlas(Gdx.files.internal("skins.atlas")));
skin.add("font", font);
skin.add("title", title);
skin.load(Gdx.files.internal("skins.json"));

skins.jsonskins.atlasskins.png 文件是使用 Skin Composer 生成的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接