libgdx:加载游戏中所有资产的最佳方法 [英] libgdx: best way to load all assets in the game

查看:59
本文介绍了libgdx:加载游戏中所有资产的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的播放屏幕上,有很多纹理Texture,字体FreeType,图像scene2d.Image和按钮scene2d.Button.因此,当我设置播放屏幕时

In my play screen has a lot of textures Texture, fonts FreeType, images scene2d.Image and buttons scene2d.Button. So, when I set play screen

((Game) Gdx.app.getApplicationListener()).setScreen(new PlayScreen());

需要几秒钟的时间才能打开屏幕.

it takes a few seconds to open the screen.

我想快速打开它,实现该目标的最佳方法是什么?

我是否要创建一个新类来创建所有资产,然后将其加载到屏幕上?

Do I create a new class to create all assets then load them on the screens?

推荐答案

您应该使用AssetManager .启动游戏时,管理器允许您加载资产.它还具有向您显示已加载资产百分比的功能,因此您可以显示进度条.有大量的资源向您展示如何使用AssetManager.一些示例向您展示了带有静态AssetManager的示例,但是Badlogic不建议这样做,并且可能会导致黑色图像.

You should use the AssetManager. The manager allows you to load your assets when you start the game. It also has the functionality to show you the percentage of assets loaded so you can show a progress bar. There are plenty of sources out there that show you how to use the AssetManager. Some show you examples with a static AssetManager but this is discouraged by badlogic and can cause black images.

您应该创建一个类,在其中存储要与管理器一起处理的资产,管理器将异步加载该资产.我喜欢使用AssetDescriptor并使用静态描述符来查找我的纹理.但是AssetManager可以有许多不同的使用方式.您可以像manager.load("images/sometexture.png, Texture.class);这样向管理器中添加一些东西,然后通过执行manager.get("images/sometexture.png");来查找它,这样就可以简单地通过其路径查找纹理.

You should make a class where you store your assets you want to handle with the manager and the manager will load it asynchronously. I like to work with the AssetDescriptor and using static descriptors to lookup my textures. but there are many different ways you can work with the AssetManager. You can just add a something to the manager like so manager.load("images/sometexture.png, Texture.class); and look it up by doing manager.get("images/sometexture.png"); this way it simple looks up the textures by it's path.

public class Assets {
public AssetManager manager = new AssetManager();

    public static final AssetDescriptor<Texture> someTexture = 
        new AssetDescriptor<Texture>("images/sometexture.png", Texture.class);

public static final AssetDescriptor<TextureAtlas> uiAtlas =
        new AssetDescriptor<TextureAtlas>("ui/uiskin.pack", TextureAtlas.class);

public static final AssetDescriptor<Skin> uiSkin =
        new AssetDescriptor<Skin>("ui/uiskin.json", Skin.class,
                new SkinLoader.SkinParameter("ui/uiskin.pack"));

public void load()
{
    manager.load(someTexture);
    manager.load(uiAtlas);
    manager.load(uiSkin);
}

public void dispose()
{
    manager.dispose();
}
}

在入口处,我创建带有徽标的初始屏幕,然后开始加载我的资产.这是您加载资产的方式.

In the entry point I create a splash screen with a logo then I start loading my assets. Here is how you load the assets.

public class MyGame extends Game
{
  public void create()
  {
    Assets assets = new Assets();
    assets.manager.load(); //starts loading assets

    //option 1
    assets.manager.finishLoading(); //Continues when done loading.
    //it won't continue until all assets are finished loading.
  }

  //option 2
public void update()
  {
    //draw something nice to look at
    if (manager.update())
    {
      //Assets have finished loading, change screen maybe?
      setScreen(new MenuScreen(assets)); //your screen should take a Assets object in it's constructor.
    }
  }  
}

也许您可以像平常一样加载菜单资产,并让资产管理器在用户位于菜单中时工作.那取决于你.这就是您加载资产的方式.

Perhaps you can load your menu assets like you normally and let the asset manager work while the user is in the menu. That is up to you. This is how you load a asset.

someTexture = assets.manager.get(Assets.someTexture);
atlas = assets.manager.get(Assets.uiAtlas);
skin = assets.manager.get(Assets.uiSkin);

相同的规则适用于在地图集中查找区域,最好是一次对地图集中的每个区域执行一次.您必须将Assets对象传递到需要加载资产的任何地方.使其公开为静态很诱人,但可能会引起问题.

The same rules apply for looking up regions in your atlas, best is to do this a single time for each region in your atlas. You have to pass around the Assets object around to anywhere you need to load assets. It makes it tempting to make it public static but it can cause issues.

这篇关于libgdx:加载游戏中所有资产的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆