TextureAtlas 和皮肤 libgdx

TextureAtlas and Skin libgdx

我愿意充分利用 Libgdx 的 AssetManager 及其所有加载程序。 但我无法理解 Skin 和 TextureAtlas 之间如此特殊的 link。

我想尽可能地使用JSON创建皮肤的方式。

在 wiki 中我可以看到这个:

Note the JSON does not describe texture regions, ninepatche splits, or other information which comes from the texture atlas

我在 SkinLoader 页面看到了这个:

AssetLoader for Skin instances. (1)All Texture and BitmapFont instances will be loaded as dependencies. Passing a SkinLoader.SkinParameter allows the (2)exact name of the texture associated with the skin to be specified. Otherwise the skin texture is looked up just as with a call to (3)Skin.Skin(com.badlogic.gdx.files.FileHandle). A SkinLoader.SkinParameter also allows named resources to be set that will be added to the skin before loading the json file, meaning that they can be referenced from inside the json file itself. This is useful for dynamic resources such as a BitmapFont generated through FreeTypeFontGenerator.

所以这就是我读到这篇文章时的想法。

(1) 所有纹理。他们使用复数形式,所以我猜他们是在提到我可能需要的按钮纹理(作为可绘制对象),例如。

(2) 纹理的确切名称。所以现在,也许他们在谈论 TextureAtlas,因为 SkinParameter 只有两个字段资源和 textureAtlasPath。

(3) 这似乎证实了 (2)

所以如果我明白,如果我想使用这种方法来加载我的皮肤,我必须使用 TexturePacker 和 TextureAtlas。

你能证实我的理解是正确的吗?没有任何方法可以初始化将在我的皮肤中使用的纹理,如果没有 TextureAtlas 但使用简单的 png 文件?

如果使用 TexturePacker 来创建我的包文件,显然当我对其中一个纹理进行修改时我将不得不重新打包所有内容,对吗?

皮肤确实支持为您的各种可绘制对象使用单独的纹理,但它很笨重,因为这并不是任何人都应该做的事情。你可以做的是从你的纹理中创建 TextureRegion 可绘制对象,并在告诉皮肤加载 json 文件之前通过名称将它们注册到皮肤,如下所示:

TextureRegionDrawable someButtonImage = new TextureRegionDrawable(
    new TextureRegion(someButtonTextureThatIsAlreadyLoaded));
Skin skin = new Skin(); //don't give it a json file yet
skin.add("someButton", someButtonImage);
skin.load(Gdx.files.internal("mySkin.json"));

现在您的 Json 可以使用 "someButton" 作为皮肤中任何可绘制参数的值。

但是,如果您使用的是 AssetManager,则只有在您已经在 AssetManager 之外加载了所有必要的纹理时才能执行此操作,因此您可以将它们作为 TextureRegionDrawables 的名称映射传递到 SkinLoader 的 SkinParameter 中。

无论哪种方式,您都将输入大量代码来手动加载所有这些纹理,而您本可以只使用纹理图集,无论如何它都会表现得更好。您可以在 IDE 中设置 运行 配置,以便在每次启动应用程序时为您打包图集,如果您担心每次更改皮肤中的图像时都必须这样做的话.

BitmapFonts 是例外,它不必与其他任何东西属于同一图集,您就可以纯粹从 Json 加载它们。皮肤视其为特例。

无论如何,下面是我如何加载我的皮肤以及它用来检索其可绘制对象的图集:

assetManager.load("mySkin.json", Skin.class, new SkinLoader.SkinParameter("myAtlas.atlas"));