问题描述
我不想加载fnt文件,因为它们在不同的屏幕分辨率上显示不同。所以我现在所做的就是在每个演员或者屏幕上动态生成我的字体。
现在我认为最好在游戏开始时生成一次字体,并将其加载到资产管理器中。但是AssetManager似乎需要一个带有BitmapFont.class参数的文件名。我想要做的是生成5个不同的位图字体,并将这些BitmapFonts加载到assetmanager中,所以我把所有的资源放在一个地方,并且可以重复使用它们。我可以创建这些BitmapFonts,将它们保存在一个列表中,并将列表提供给每个演员或屏幕,就像我管理我的纹理和音频的assetmanager一样。但是,将所有东西放在一个地方,比如资产管理器,会更加优雅。
那么,有没有办法将使用FreeTypeFontGenerator创建的BitmapFonts加载到assetmanager中?
你可以阅读如何提供自己的。
您必须实现。那些会得到一个自由的字体的文件。有了这个,你可以使用生成器来生成你想要的 BitmapFont
。由于您想使用资产管理器,因此您必须像下面这样覆盖位图字体的默认加载器:
manager.setLoader BitmapFont.class,新的MyFreeTypeFontLoader(新的InternalFileHandleResolver())); b
$ /com/badlogic/gdx/assets/AssetLoaderParameters.html> AssetLoaderParameters
,您可以向装载程序提供更多信息,例如字体大小。
下面的代码是未经测试,但可能的工作:
公共类FreeTypeFontLoader延伸SynchronousAssetLoader< ; BitmapFont,FreeTypeFontLoader.FreeTypeFontParameters> {
public FreeTypeFontLoader(FileHandleResolver解析器){
super(resolver);
}
@覆盖
公共BitmapFont负载(AssetManager assetManager,字符串文件名,文件句柄文件,FreeTypeFontParameters参数){
FreeTypeFontGenerator发生器=新FreeTypeFontGenerator(文件);
return generator.generateFont(parameter.fontParameters);
}
static public class FreeTypeFontParameters extends AssetLoaderParameters< BitmapFont> {
public FreeTypeFontParameter fontParameters;
}
@Override
public Array< AssetDescriptor> getDependencies(String fileName,FileHandle file,FreeTypeFontParameters参数){
return null;
$ b $ / code $ / $ p
UPDATE:
这不再是必要的,gdx-freetype扩展现在拥有freetype字体本身的加载器!
-
- [FreeTypeFontLoaderTest]
()
i'd like to use the asset manager in combination with the FreeTypeFontGenerator.
I dont want to load fnt files cause they display differently on different screenresolutions. So what i do currently is generating my fonts on the fly in every actor or screen.
Now i think its best to generate the fonts once when the game is started and load them into the asset manager. But the AssetManager seems to need a filename with the BitmapFont.class parameter. What i want to do is generate 5 different bitmapfonts and and load those BitmapFonts into the assetmanager, so i have all my ressources in one place, and can reuse them. I could just create those BitmapFonts, save them in a list, and give the list to every actor or screen, just like i do with the assetmanager i manage my textures and audio with. But it would be more elegant to have everything in one place, the asset manager.
So, is there a way to load BitmapFonts created with FreeTypeFontGenerator into the assetmanager?
解决方案 Here you can read about how to supply your own AssetLoader
.
You would have to implement either a SynchronousAssetLoader
or an AsynchronousAssetLoader
. Those Would get the file to a free type font. With that you can use the generator to generate your desired BitmapFont
. Since you want to use the asset manager, you have to overwrite the default loader for bitmap fonts like this:
manager.setLoader(BitmapFont.class, new MyFreeTypeFontLoader(new InternalFileHandleResolver()));
Via AssetLoaderParameters
you could supply further information to your loader, like the font size.
The following code is untested, but might work:
public class FreeTypeFontLoader extends SynchronousAssetLoader<BitmapFont, FreeTypeFontLoader.FreeTypeFontParameters> {
public FreeTypeFontLoader(FileHandleResolver resolver) {
super(resolver);
}
@Override
public BitmapFont load(AssetManager assetManager, String fileName, FileHandle file, FreeTypeFontParameters parameter) {
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(file);
return generator.generateFont(parameter.fontParameters);
}
static public class FreeTypeFontParameters extends AssetLoaderParameters<BitmapFont> {
public FreeTypeFontParameter fontParameters;
}
@Override
public Array<AssetDescriptor> getDependencies(String fileName, FileHandle file, FreeTypeFontParameters parameter) {
return null;
}
}
UPDATE:
This is no longer necessary, the gdx-freetype extension now has loaders for freetype fonts itself!
- FreeTypeFontGeneratorLoader (it's the same like the code above)
- FreeTypeFontLoader
- [FreeTypeFontLoaderTest](https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/extensions/FreeTypeFontLoaderTest.java)
这篇关于带有AssetManager的Libgdx FreeTypeFontGenerator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!