简体   繁体   English

LibGDX静态AssetManager

[英]LibGDX static AssetManager

I am having some trouble figuring out the proper use of libGDX's AssetManager class. 我在弄清楚libGDX的AssetManager类的正确使用时遇到了一些麻烦。 From what I understand, you are never supposed to create a static instance of an AssetManager. 据我了解,您永远不应创建AssetManager的静态实例。 The alternative that people suggested was to pass around the AssetManager reference to every single class that uses it. 人们建议的替代方法是将AssetManager引用传递给使用它的每个类。 I'm wondering if what I have proposed below is another safe alternative. 我想知道我下面提出的建议是否是另一种安全的选择。

public class GdxGame extends Game {

    private AssetManager assets;

    ....dispose, create AssetManager etc


    public static AssetManager getAssets() {
        return ((GdxGame) Gdx.app.getApplicationListener()).assets;
    }
}

I really don't want to have to pass an AssetManager or an Assets class around to every single class in the game, so I'm trying to figure out a solution that allows some kind of static referencing. 我真的不希望必须将AssetManager或Assets类传递给游戏中的每个类,因此我试图找出一种允许某种静态引用的解决方案。 Is this safe? 这样安全吗?

I think you worry too much :-) The static instance you suggested originally will be perfectly adequate and functional for your purposes: 我认为您太担心了:-)您最初建议的静态实例对于您的目的将是完全足够的并且可以正常工作:

private static AssetManager assets;
....dispose, create AssetManager etc

public static AssetManager getAssets() {
    return assets;
}

The only trap for young players to avoid is when you pause an Android app: on resume, just reload the AssetManager and its assets before continuing. 年轻玩家唯一需要避免的陷阱是当您暂停Android应用程序时:在继续时,只需重新加载AssetManager及其资产,然后再继续。 Don't worry about semantics - if it works for you, then it's fine. 不必担心语义-如果它对您有用,那就很好。 Happy days :-). 快乐的时光 :-)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM