Package com.badlogic.gdx.assets
Class AssetManager
- java.lang.Object
-
- com.badlogic.gdx.assets.AssetManager
-
- All Implemented Interfaces:
Disposable
public class AssetManager extends java.lang.Object implements Disposable
Loads and stores assets like textures, bitmapfonts, tile maps, sounds, music and so on.
-
-
Constructor Summary
Constructors Constructor Description AssetManager()
Creates 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected <T> void
addAsset(java.lang.String fileName, java.lang.Class<T> type, T asset)
Adds an asset to this AssetManagervoid
clear()
Clears and disposes all assets and the preloading queue.boolean
contains(java.lang.String fileName)
Returns true if an asset with the specified name is loading, queued to be loaded, or has been loaded.boolean
contains(java.lang.String fileName, java.lang.Class type)
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
finishLoading()
Blocks until all assets are loaded.<T> T
finishLoadingAsset(AssetDescriptor assetDesc)
Blocks until the specified asset is loaded.<T> T
finishLoadingAsset(java.lang.String fileName)
Blocks until the specified asset is loaded.<T> T
get(AssetDescriptor<T> assetDescriptor)
<T> T
get(java.lang.String fileName)
<T> T
get(java.lang.String fileName, boolean required)
<T> T
get(java.lang.String fileName, java.lang.Class<T> type)
<T> T
get(java.lang.String fileName, java.lang.Class<T> type, boolean required)
<T> Array<T>
getAll(java.lang.Class<T> type, Array<T> out)
<T> java.lang.String
getAssetFileName(T asset)
Array<java.lang.String>
getAssetNames()
java.lang.Class
getAssetType(java.lang.String fileName)
Array<java.lang.String>
getDependencies(java.lang.String fileName)
java.lang.String
getDiagnostics()
FileHandleResolver
getFileHandleResolver()
Returns theFileHandleResolver
for which this AssetManager was loaded with.int
getLoadedAssets()
<T> AssetLoader
getLoader(java.lang.Class<T> type)
Returns the default loader for the given type.<T> AssetLoader
getLoader(java.lang.Class<T> type, java.lang.String fileName)
Returns the loader for the given type and the specified filename.Logger
getLogger()
float
getProgress()
int
getQueuedAssets()
int
getReferenceCount(java.lang.String fileName)
Returns the reference count of an asset.boolean
isFinished()
Returns true when all assets are loaded.boolean
isLoaded(AssetDescriptor assetDesc)
boolean
isLoaded(java.lang.String fileName)
boolean
isLoaded(java.lang.String fileName, java.lang.Class type)
void
load(AssetDescriptor desc)
Adds the given asset to the loading queue of the AssetManager.<T> void
load(java.lang.String fileName, java.lang.Class<T> type)
Adds the given asset to the loading queue of the AssetManager.<T> void
load(java.lang.String fileName, java.lang.Class<T> type, AssetLoaderParameters<T> parameter)
Adds the given asset to the loading queue of the AssetManager.void
setErrorListener(AssetErrorListener listener)
Sets anAssetErrorListener
to be invoked in case loading an asset failed.<T,P extends AssetLoaderParameters<T>>
voidsetLoader(java.lang.Class<T> type, AssetLoader<T,P> loader)
Sets a newAssetLoader
for the given type.<T,P extends AssetLoaderParameters<T>>
voidsetLoader(java.lang.Class<T> type, java.lang.String suffix, AssetLoader<T,P> loader)
Sets a newAssetLoader
for the given type.void
setLogger(Logger logger)
void
setReferenceCount(java.lang.String fileName, int refCount)
Sets the reference count of an asset.protected void
taskFailed(AssetDescriptor assetDesc, java.lang.RuntimeException ex)
Called when a task throws an exception during loading.void
unload(java.lang.String fileName)
Removes the asset and all its dependencies, if they are not used by other assets.boolean
update()
Updates the AssetManager for a single task.boolean
update(int millis)
Updates the AssetManager continuously for the specified number of milliseconds, yielding the CPU to the loading thread between updates.
-
-
-
Constructor Detail
-
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 Detail
-
getFileHandleResolver
public FileHandleResolver getFileHandleResolver()
Returns theFileHandleResolver
for which this AssetManager was loaded with.- Returns:
- the file handle resolver which this AssetManager uses
-
get
public <T> T get(java.lang.String fileName)
- Parameters:
fileName
- the asset file name- Returns:
- the asset
- Throws:
GdxRuntimeException
- if the asset is not loaded
-
get
public <T> T get(java.lang.String fileName, java.lang.Class<T> type)
- Parameters:
fileName
- the asset file nametype
- the asset type- Returns:
- the asset
- Throws:
GdxRuntimeException
- if the asset is not loaded
-
get
@Null public <T> T get(java.lang.String fileName, boolean required)
- Parameters:
fileName
- the asset file namerequired
- true to throw GdxRuntimeException if the asset is not loaded, else null is returned- Returns:
- the asset or null if it is not loaded and required is false
-
get
@Null public <T> T get(java.lang.String fileName, java.lang.Class<T> type, boolean required)
- Parameters:
fileName
- the asset file nametype
- the asset typerequired
- true to throw GdxRuntimeException if the asset is not loaded, else null is returned- Returns:
- the asset or null if it is not loaded and required is false
-
get
public <T> T get(AssetDescriptor<T> assetDescriptor)
- Parameters:
assetDescriptor
- the asset descriptor- Returns:
- the asset
- Throws:
GdxRuntimeException
- if the asset is not loaded
-
getAll
public <T> Array<T> getAll(java.lang.Class<T> type, Array<T> out)
- Parameters:
type
- the asset type- Returns:
- all the assets matching the specified type
-
contains
public boolean contains(java.lang.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(java.lang.String fileName, java.lang.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(java.lang.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> java.lang.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(java.lang.String fileName)
- Parameters:
fileName
- the file name of the asset- Returns:
- whether the asset is loaded
-
isLoaded
public boolean isLoaded(java.lang.String fileName, java.lang.Class type)
- Parameters:
fileName
- the file name of the asset- Returns:
- whether the asset is loaded
-
getLoader
public <T> AssetLoader getLoader(java.lang.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(java.lang.Class<T> type, java.lang.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 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
public <T> void load(java.lang.String fileName, java.lang.Class<T> type)
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.
-
load
public <T> void load(java.lang.String fileName, java.lang.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
public void load(AssetDescriptor desc)
Adds the given asset to the loading queue of the AssetManager.- Parameters:
desc
- theAssetDescriptor
-
update
public boolean update()
Updates the AssetManager for a single task. Returns if the current task is still being processed or there are no tasks, otherwise it finishes the current task and starts the next task.- Returns:
- true if all loading is finished.
-
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. On GWT, updates for a single task instead (seeupdate()
).- Returns:
- true if all loading is finished.
-
isFinished
public boolean isFinished()
Returns true when all assets are loaded. Can be called from any thread but noteupdate()
or related methods must be called to process tasks.
-
finishLoading
public void finishLoading()
Blocks until all assets are loaded.
-
finishLoadingAsset
public <T> T finishLoadingAsset(AssetDescriptor assetDesc)
Blocks until the specified asset is loaded.- Parameters:
assetDesc
- the AssetDescriptor of the asset
-
finishLoadingAsset
public <T> T finishLoadingAsset(java.lang.String fileName)
Blocks until the specified asset is loaded.- Parameters:
fileName
- the file name (interpretation depends onAssetLoader
)
-
addAsset
protected <T> void addAsset(java.lang.String fileName, java.lang.Class<T> type, T asset)
Adds an asset to this AssetManager
-
taskFailed
protected void taskFailed(AssetDescriptor assetDesc, java.lang.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(java.lang.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(java.lang.Class<T> type, java.lang.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
public void setErrorListener(AssetErrorListener listener)
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.
-
getLogger
public Logger getLogger()
- Returns:
- the
Logger
used by theAssetManager
-
setLogger
public void setLogger(Logger logger)
-
getReferenceCount
public int getReferenceCount(java.lang.String fileName)
Returns the reference count of an asset.- Parameters:
fileName
-
-
setReferenceCount
public void setReferenceCount(java.lang.String fileName, int refCount)
Sets the reference count of an asset.- Parameters:
fileName
-
-
getDiagnostics
public java.lang.String getDiagnostics()
- Returns:
- a string containing ref count and dependency information for all assets.
-
getAssetNames
public Array<java.lang.String> getAssetNames()
- Returns:
- the file names of all loaded assets.
-
getDependencies
public Array<java.lang.String> getDependencies(java.lang.String fileName)
- Returns:
- the dependencies of an asset or null if the asset has no dependencies.
-
getAssetType
public java.lang.Class getAssetType(java.lang.String fileName)
- Returns:
- the type of a loaded asset.
-
-