Package arc.assets

Class AssetManager

java.lang.Object
arc.assets.AssetManager
All Implemented Interfaces:
Disposable

public class AssetManager extends Object implements Disposable
Loads and stores assets like textures, bitmapfonts, tile maps, sounds, music and so on.
  • Constructor Details

    • AssetManager

      public AssetManager()
      Creates a new AssetManager with all default loaders.
    • AssetManager

      public AssetManager(FileHandleResolver resolver)
      Creates a new AssetManager with all default loaders.
    • AssetManager

      public AssetManager(FileHandleResolver resolver, boolean defaultLoaders)
      Creates a new AssetManager with optionally all default loaders. If you don't add the default loaders then you do have to manually add the loaders you need, including any loaders they might depend on.
      Parameters:
      defaultLoaders - whether to add the default loaders
  • Method Details

    • getFileHandleResolver

      public FileHandleResolver getFileHandleResolver()
      Returns the FileHandleResolver for which this AssetManager was loaded with.
      Returns:
      the file handle resolver which this AssetManager uses
    • get

      public <T> T get(String fileName)
      Parameters:
      fileName - the asset file name
      Returns:
      the asset
    • get

      public <T> T get(String fileName, Class<T> type)
      Parameters:
      fileName - the asset file name
      type - the asset type
      Returns:
      the asset
    • getAll

      public <T> Seq<T> getAll(Class<T> type, Seq<T> out)
      Parameters:
      type - the asset type
      Returns:
      all the assets matching the specified type
    • get

      public <T> T get(AssetDescriptor<T> assetDescriptor)
      Parameters:
      assetDescriptor - the asset descriptor
      Returns:
      the asset
    • contains

      public boolean contains(String fileName)
      Returns true if an asset with the specified name is loading, queued to be loaded, or has been loaded.
    • contains

      public boolean contains(String fileName, Class type)
      Returns true if an asset with the specified name and type is loading, queued to be loaded, or has been loaded.
    • unload

      public void unload(String fileName)
      Removes the asset and all its dependencies, if they are not used by other assets.
      Parameters:
      fileName - the file name
    • containsAsset

      public <T> boolean containsAsset(T asset)
      Parameters:
      asset - the asset
      Returns:
      whether the asset is contained in this manager
    • getAssetFileName

      public <T> String getAssetFileName(T asset)
      Parameters:
      asset - the asset
      Returns:
      the filename of the asset or null
    • isLoaded

      public boolean isLoaded(AssetDescriptor assetDesc)
      Parameters:
      assetDesc - the AssetDescriptor of the asset
      Returns:
      whether the asset is loaded
    • isLoaded

      public boolean isLoaded(String fileName)
      Parameters:
      fileName - the file name of the asset
      Returns:
      whether the asset is loaded
    • isLoaded

      public boolean isLoaded(String fileName, Class type)
      Parameters:
      fileName - the file name of the asset
      Returns:
      whether the asset is loaded
    • getLoader

      public <T> AssetLoader getLoader(Class<T> type)
      Returns the default loader for the given type
      Parameters:
      type - The type of the loader to get
      Returns:
      The loader capable of loading the type, or null if none exists
    • getLoader

      public <T> AssetLoader getLoader(Class<T> type, String fileName)
      Returns the loader for the given type and the specified filename. If no loader exists for the specific filename, the default loader for that type is returned.
      Parameters:
      type - The type of the loader to get
      fileName - The filename of the asset to get a loader for, or null to get the default loader
      Returns:
      The loader capable of loading the type and filename, or null if none exists
    • load

      public <T> AssetDescriptor<T> load(String fileName, Class<T> type)
      Adds the given asset to the loading queue of the AssetManager.
      Parameters:
      fileName - the file name (interpretation depends on AssetLoader)
      type - the type of the asset.
    • loadRun

      public AssetDescriptor loadRun(String name, Class<?> type, Runnable loadasync)
      Loads a custom one-time 'asset' that knows how to load itself.
    • loadRun

      public AssetDescriptor loadRun(String name, Class<?> type, Runnable loadasync, Runnable loadsync)
      Loads a custom one-time 'asset' that knows how to load itself.
    • load

      public AssetDescriptor load(Loadable load)
      Loads a custom one-time 'asset' that knows how to load itself.
      Parameters:
      load - the asset
    • load

      public <T> AssetDescriptor<T> load(String fileName, Class<T> type, AssetLoaderParameters<T> parameter)
      Adds the given asset to the loading queue of the AssetManager.
      Parameters:
      fileName - the file name (interpretation depends on AssetLoader)
      type - the type of the asset.
      parameter - parameters for the AssetLoader.
    • load

      public <T> AssetDescriptor<T> load(AssetDescriptor<T> desc)
      Adds the given asset to the loading queue of the AssetManager.
      Parameters:
      desc - the AssetDescriptor
    • update

      public boolean update()
      Updates the AssetManager, keeping it loading any assets in the preload queue.
      Returns:
      true if all loading is finished.
    • getCurrentLoading

      public AssetDescriptor getCurrentLoading()
      Returns:
      the asset loading task that is currently being processed. May return null if nothing is being loaded.
    • update

      public boolean update(int millis)
      Updates the AssetManager continuously for the specified number of milliseconds, yielding the CPU to the loading thread between updates. This may block for less time if all loading tasks are complete. This may block for more time if the portion of a single task that happens in the GL thread takes a long time.
      Returns:
      true if all loading is finished.
    • isFinished

      public boolean isFinished()
      Returns true when all assets are loaded. Can be called from any thread.
    • finishLoading

      public void finishLoading()
      Blocks until all assets are loaded.
    • finishLoadingAsset

      public void finishLoadingAsset(AssetDescriptor assetDesc)
      Blocks until the specified asset is loaded.
      Parameters:
      assetDesc - the AssetDescriptor of the asset
    • finishLoadingAsset

      public void finishLoadingAsset(String fileName)
      Blocks until the specified asset is loaded.
      Parameters:
      fileName - the file name (interpretation depends on AssetLoader)
    • addAsset

      protected <T> void addAsset(String fileName, Class<T> type, T asset)
      Adds an asset to this AssetManager
    • taskFailed

      protected void taskFailed(AssetDescriptor assetDesc, RuntimeException ex)
      Called when a task throws an exception during loading. The default implementation rethrows the exception. A subclass may supress the default implementation when loading assets where loading failure is recoverable.
    • setLoader

      public <T, P extends AssetLoaderParameters<T>> void setLoader(Class<T> type, AssetLoader<T,P> loader)
      Sets a new AssetLoader for the given type.
      Parameters:
      type - the type of the asset
      loader - the loader
    • setLoader

      public <T, P extends AssetLoaderParameters<T>> void setLoader(Class<T> type, String suffix, AssetLoader<T,P> loader)
      Sets a new AssetLoader for the given type.
      Parameters:
      type - the type of the asset
      suffix - the suffix the filename must have for this loader to be used or null to specify the default loader.
      loader - the loader
    • getLoadedAssets

      public int getLoadedAssets()
      Returns:
      the number of loaded assets
    • getQueuedAssets

      public int getQueuedAssets()
      Returns:
      the number of currently queued assets
    • getProgress

      public float getProgress()
      Returns:
      the progress in percent of completion.
    • setErrorListener

      public void setErrorListener(AssetErrorListener listener)
      Sets an AssetErrorListener to be invoked in case loading an asset failed.
      Parameters:
      listener - the listener or null
    • dispose

      public void dispose()
      Disposes all assets in the manager and stops all asynchronous loading.
      Specified by:
      dispose in interface Disposable
    • clear

      public void clear()
      Clears and disposes all assets and the preloading queue.
    • getReferenceCount

      public int getReferenceCount(String fileName)
      Returns the reference count of an asset.
    • setReferenceCount

      public void setReferenceCount(String fileName, int refCount)
      Sets the reference count of an asset.
    • getDiagnostics

      public String getDiagnostics()
      Returns:
      a string containing ref count and dependency information for all assets.
    • getAssetNames

      public Seq<String> getAssetNames()
      Returns:
      the file names of all loaded assets.
    • getDependencies

      public Seq<String> getDependencies(String fileName)
      Returns:
      the dependencies of an asset or null if the asset has no dependencies.
    • getAssetType

      public Class getAssetType(String fileName)
      Returns:
      the type of a loaded asset.