Package arc.assets
Class AssetManager
java.lang.Object
arc.assets.AssetManager
- All Implemented Interfaces:
Disposable
Loads and stores assets like textures, bitmapfonts, tile maps, sounds, music and so on.
-
Constructor Summary
ConstructorDescriptionCreates a new AssetManager with all default loaders.AssetManager
(FileHandleResolver resolver) Creates a new AssetManager with all default loaders.AssetManager
(FileHandleResolver resolver, boolean defaultLoaders) Creates a new AssetManager with optionally all default loaders. -
Method Summary
Modifier and TypeMethodDescriptionprotected <T> void
Adds an asset to this AssetManagervoid
clear()
Clears and disposes all assets and the preloading queue.boolean
Returns true if an asset with the specified name is loading, queued to be loaded, or has been loaded.boolean
Returns true if an asset with the specified name and type is loading, queued to be loaded, or has been loaded.<T> boolean
containsAsset
(T asset) void
dispose()
Disposes all assets in the manager and stops all asynchronous loading.void
Blocks until all assets are loaded.void
finishLoadingAsset
(AssetDescriptor assetDesc) Blocks until the specified asset is loaded.void
finishLoadingAsset
(String fileName) Blocks until the specified asset is loaded.<T> T
get
(AssetDescriptor<T> assetDescriptor) <T> T
<T> T
<T> Seq<T>
<T> String
getAssetFileName
(T asset) getAssetType
(String fileName) getDependencies
(String fileName) Returns theFileHandleResolver
for which this AssetManager was loaded with.int
<T> AssetLoader
Returns the default loader for the given type<T> AssetLoader
Returns the loader for the given type and the specified filename.float
int
int
getReferenceCount
(String fileName) Returns the reference count of an asset.boolean
Returns true when all assets are loaded.boolean
isLoaded
(AssetDescriptor assetDesc) boolean
boolean
<T> AssetDescriptor<T>
load
(AssetDescriptor<T> desc) Adds the given asset to the loading queue of the AssetManager.Loads a custom one-time 'asset' that knows how to load itself.<T> AssetDescriptor<T>
Adds the given asset to the loading queue of the AssetManager.<T> AssetDescriptor<T>
load
(String fileName, Class<T> type, AssetLoaderParameters<T> parameter) Adds the given asset to the loading queue of the AssetManager.Loads a custom one-time 'asset' that knows how to load itself.Loads a custom one-time 'asset' that knows how to load itself.void
setErrorListener
(AssetErrorListener listener) Sets anAssetErrorListener
to be invoked in case loading an asset failed.<T,
P extends AssetLoaderParameters<T>>
voidsetLoader
(Class<T> type, AssetLoader<T, P> loader) Sets a newAssetLoader
for the given type.<T,
P extends AssetLoaderParameters<T>>
voidsetLoader
(Class<T> type, String suffix, AssetLoader<T, P> loader) Sets a newAssetLoader
for the given type.void
setReferenceCount
(String fileName, int refCount) Sets the reference count of an asset.protected void
taskFailed
(AssetDescriptor assetDesc, RuntimeException ex) Called when a task throws an exception during loading.void
Removes the asset and all its dependencies, if they are not used by other assets.boolean
update()
Updates the AssetManager, keeping it loading any assets in the preload queue.boolean
update
(int millis) Updates the AssetManager continuously for the specified number of milliseconds, yielding the CPU to the loading thread between updates.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface arc.util.Disposable
isDisposed
-
Constructor Details
-
AssetManager
public AssetManager()Creates a new AssetManager with all default loaders. -
AssetManager
Creates a new AssetManager with all default loaders. -
AssetManager
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
Returns theFileHandleResolver
for which this AssetManager was loaded with.- Returns:
- the file handle resolver which this AssetManager uses
-
get
- Parameters:
fileName
- the asset file name- Returns:
- the asset
-
get
- Parameters:
fileName
- the asset file nametype
- the asset type- Returns:
- the asset
-
getAll
- Parameters:
type
- the asset type- Returns:
- all the assets matching the specified type
-
get
- Parameters:
assetDescriptor
- the asset descriptor- Returns:
- the asset
-
contains
Returns true if an asset with the specified name is loading, queued to be loaded, or has been loaded. -
contains
Returns true if an asset with the specified name and type is loading, queued to be loaded, or has been loaded. -
unload
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
- Parameters:
asset
- the asset- Returns:
- the filename of the asset or null
-
isLoaded
- Parameters:
assetDesc
- the AssetDescriptor of the asset- Returns:
- whether the asset is loaded
-
isLoaded
- Parameters:
fileName
- the file name of the asset- Returns:
- whether the asset is loaded
-
isLoaded
- Parameters:
fileName
- the file name of the asset- Returns:
- whether the asset is loaded
-
getLoader
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
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 getfileName
- 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
Adds the given asset to the loading queue of the AssetManager.- Parameters:
fileName
- the file name (interpretation depends onAssetLoader
)type
- the type of the asset.
-
loadRun
Loads a custom one-time 'asset' that knows how to load itself. -
loadRun
Loads a custom one-time 'asset' that knows how to load itself. -
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 onAssetLoader
)type
- the type of the asset.parameter
- parameters for the AssetLoader.
-
load
Adds the given asset to the loading queue of the AssetManager.- Parameters:
desc
- theAssetDescriptor
-
update
public boolean update()Updates the AssetManager, keeping it loading any assets in the preload queue.- Returns:
- true if all loading is finished.
-
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
Blocks until the specified asset is loaded.- Parameters:
assetDesc
- the AssetDescriptor of the asset
-
finishLoadingAsset
Blocks until the specified asset is loaded.- Parameters:
fileName
- the file name (interpretation depends onAssetLoader
)
-
addAsset
Adds an asset to this AssetManager -
taskFailed
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 newAssetLoader
for the given type.- Parameters:
type
- the type of the assetloader
- the loader
-
setLoader
public <T,P extends AssetLoaderParameters<T>> void setLoader(Class<T> type, String suffix, AssetLoader<T, P> loader) Sets a newAssetLoader
for the given type.- Parameters:
type
- the type of the assetsuffix
- 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
Sets anAssetErrorListener
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 interfaceDisposable
-
clear
public void clear()Clears and disposes all assets and the preloading queue. -
getReferenceCount
Returns the reference count of an asset. -
setReferenceCount
Sets the reference count of an asset. -
getDiagnostics
- Returns:
- a string containing ref count and dependency information for all assets.
-
getAssetNames
- Returns:
- the file names of all loaded assets.
-
getDependencies
- Returns:
- the dependencies of an asset or null if the asset has no dependencies.
-
getAssetType
- Returns:
- the type of a loaded asset.
-