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?
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:
ObjectMap<String, Object>
and put your fonts in this map.SkinParameter
and give it the ObjectMap
as a parameter.Skin
through the AsserManager
as usual using the created SkinParameter
.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);
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:
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With