问题
In my uiskin.json I have this
com.badlogic.gdx.graphics.g2d.BitmapFont: { default-font: { file: text/default.fnt } }
This is fine for when I have the default.fnt in my text folder in the assets folder...
However I want to use a Freetypefont.
How do I create a Freetypefont and load it into the uiskin file?
回答1:
It is not possible to do it beforehand, since you are going to generate it at runtime.
My workaround would be to keep the default font as it is and replace it at runtime via code. You can use Skin.add(...) for that.
BitmapFont newDefaultFont = FreeTypeFontGenerator...;
skin.add("default-font", newDefaultFont, BitmapFont.class);
回答2:
Update
Thanks to a comment from Jos van Egmond telling me how to do this and still load the Skin
from the AssetManager
I have decided to update the answer. The old answer (and the alternative way of doing this) still remains untouched below.
The new procedure goes like this:
- Load your font files.
- Create an ObjectMap<String, Object> and put your fonts in this map.
- Create a
SkinParameter
and give it theObjectMap
as a parameter. - Load your
Skin
through theAsserManager
as usual using the createdSkinParameter
.
The code might look something like this:
/* 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);
Old
The current accepted answer is actually (kind of) incorrect. This blog post describes a way for you to reference generated BitmapFont
s in your Json file. The procedure goes something like this:
- Create blank skin file using the default constructor.
- Add your fonts to the skin.
- Add your atlas file to your skin.
- Load your Json file to your skin.
The code might look something like this:
// 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"));
You can now do something like this in your skin Json:
...,
font: "myFont",
...
Although this is a workaround this also means that you cannot (at least I haven't found a way to) load your skin through an AssetManager
. You can still load the TextureAtlas
through the manager though.
来源:https://stackoverflow.com/questions/24856201/putting-freetypefont-into-libgdx-skin