APIdia Launch Partner
Top Description Inners Fields Constructors Methods
com.badlogic.gdx.assets

public Class AssetManager

extends Object
implements Disposable
Class Inheritance
All Implemented Interfaces
com.badlogic.gdx.utils.Disposable
Imports
com.badlogic.gdx.Application, .Gdx, com.badlogic.gdx.assets.loaders.AssetLoader, .BitmapFontLoader, .CubemapLoader, .FileHandleResolver, .I18NBundleLoader, .MusicLoader, .ParticleEffectLoader, .PixmapLoader, .ShaderProgramLoader, .SkinLoader, .SoundLoader, .TextureAtlasLoader, .TextureLoader, com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver, com.badlogic.gdx.audio.Music, .Sound, com.badlogic.gdx.graphics.Cubemap, .Pixmap, .Texture, com.badlogic.gdx.graphics.g2d.BitmapFont, .ParticleEffect, .PolygonRegion, .PolygonRegionLoader, .TextureAtlas, com.badlogic.gdx.graphics.g3d.Model, com.badlogic.gdx.graphics.g3d.loader.G3dModelLoader, .ObjLoader, com.badlogic.gdx.graphics.glutils.ShaderProgram, com.badlogic.gdx.scenes.scene2d.ui.Skin, com.badlogic.gdx.utils.Array, .Disposable, .GdxRuntimeException, .I18NBundle, .JsonReader, .Logger, .Null, .ObjectIntMap, .ObjectMap, .ObjectMap.Entry, .ObjectSet, .TimeUtils, .UBJsonReader, com.badlogic.gdx.utils.async.AsyncExecutor, .ThreadUtils, com.badlogic.gdx.utils.reflect.ClassReflection

Loads and stores assets like textures, bitmapfonts, tile maps, sounds, music and so on.
Author
mzechner

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class

Field Summary

Modifier and TypeField and Description
pack-priv final ObjectMap<String, Array<String>>
pack-priv final ObjectMap<Class<T>, ObjectMap<String, AssetManager.RefCountedContainer>>
pack-priv final ObjectMap<String, Class<T>>
pack-priv final AsyncExecutor
pack-priv final ObjectSet<String>
pack-priv AssetErrorListener
pack-priv int
pack-priv final ObjectMap<Class<T>, ObjectMap<String, AssetLoader<T, P extends AssetLoaderParameters<T>>>>
pack-priv final Array<AssetDescriptor<T>>
pack-priv Logger
pack-priv int
pack-priv final FileHandleResolver
pack-priv final Array<AssetLoadingTask>
pack-priv int

Constructor Summary

AccessConstructor and Description
public
AssetManager()

Creates a new AssetManager with all default loaders.

public
AssetManager(FileHandleResolver resolver)

Creates a new AssetManager with all default loaders.

public
AssetManager(FileHandleResolver resolver, boolean
whether to add the default loaders
defaultLoaders
)

Creates a new AssetManager with optionally all default loaders.

Method Summary

Modifier and TypeMethod and Description
protected <T> void
addAsset(final String fileName, Class<T> type, T asset)

Adds an asset to this AssetManager

private void
addTask(AssetDescriptor<T> assetDesc)

Adds a AssetLoadingTask to the task stack for the given asset.

public void
clear()

Clears and disposes all assets and the preloading queue.

public synchronized boolean
contains(String fileName)

Returns true if an asset with the specified name is loading, queued to be loaded, or has been loaded.

public synchronized boolean
contains(String fileName, Class<T> type)

Returns true if an asset with the specified name and type is loading, queued to be loaded, or has been loaded.

public synchronized <T> boolean
containsAsset(T
the asset
asset
)

Returns: whether the asset is contained in this manager

public void
dispose()

Implements com.badlogic.gdx.utils.Disposable.dispose.

Disposes all assets in the manager and stops all asynchronous loading.

public void
finishLoading()

Blocks until all assets are loaded.

public <T> T
finishLoadingAsset(AssetDescriptor<T>
the AssetDescriptor of the asset
assetDesc
)

Blocks until the specified asset is loaded.

public <T> T
finishLoadingAsset(String
the file name (interpretation depends on AssetLoader)
fileName
)

Blocks until the specified asset is loaded.

public synchronized <T> T
get(String
the asset file name
fileName
)

Returns: the asset

public synchronized <T> T
get(String
the asset file name
fileName
,
Class<T>
the asset type
type
)

Returns: the asset

public synchronized <T> T
get(String
the asset file name
fileName
,
boolean
true to throw GdxRuntimeException if the asset is not loaded, else null is returned
required
)

Returns: the asset or null if it is not loaded and required is false

public synchronized <T> T
get(String
the asset file name
fileName
,
Class<T>
the asset type
type
,
boolean
true to throw GdxRuntimeException if the asset is not loaded, else null is returned
required
)

Returns: the asset or null if it is not loaded and required is false

public synchronized <T> T
get(AssetDescriptor<T>
the asset descriptor
assetDescriptor
)

Returns: the asset

public synchronized <T> Array<T>
getAll(Class<T>
the asset type
type
,
Array<T> out)

Returns: all the assets matching the specified type

public synchronized <T> String
getAssetFileName(T
the asset
asset
)

Returns: the filename of the asset or null

public synchronized Array<String>
getAssetNames()

Returns: the file names of all loaded assets.

public synchronized Class<T>
getAssetType(String fileName)

Returns: the type of a loaded asset.

public synchronized Array<String>
getDependencies(String fileName)

Returns: the dependencies of an asset or null if the asset has no dependencies.

public synchronized String
getDiagnostics()

Returns: a string containing ref count and dependency information for all assets.

public FileHandleResolver

Returns:

the file handle resolver which this AssetManager uses
getFileHandleResolver
()

Returns the FileHandleResolver for which this AssetManager was loaded with.

public synchronized int
getLoadedAssets()

Returns: the number of loaded assets

public <T> AssetLoader<T, P extends AssetLoaderParameters<T>>

Returns:

The loader capable of loading the type, or null if none exists
getLoader
(final Class<T>
The type of the loader to get
type
)

Returns the default loader for the given type.

public <T> AssetLoader<T, P extends AssetLoaderParameters<T>>

Returns:

The loader capable of loading the type and filename, or null if none exists
getLoader
(final Class<T>
The type of the loader to get
type
,
final String
The filename of the asset to get a loader for, or null to get the default loader
fileName
)

Returns the loader for the given type and the specified filename.

public Logger
getLogger()

Returns: the Logger used by the AssetManager

public synchronized float
getProgress()

Returns: the progress in percent of completion.

public synchronized int
getQueuedAssets()

Returns: the number of currently queued assets

public synchronized int
getReferenceCount(String fileName)

Returns the reference count of an asset.

private void
handleTaskError(Throwable t)

Handles a runtime/loading error in update() by optionally invoking the AssetErrorListener.

private void
pack-priv synchronized void
injectDependencies(String parentAssetFilename, Array<AssetDescriptor<T>> dependendAssetDescs)

private synchronized void
injectDependency(String parentAssetFilename, AssetDescriptor<T> dependendAssetDesc)

public synchronized boolean
isFinished()

Returns true when all assets are loaded.

public synchronized boolean
isLoaded(AssetDescriptor<T>
the AssetDescriptor of the asset
assetDesc
)

Returns: whether the asset is loaded

public synchronized boolean
isLoaded(String
the file name of the asset
fileName
)

Returns: whether the asset is loaded

public synchronized boolean
isLoaded(String
the file name of the asset
fileName
,
Class<T> type)

Returns: whether the asset is loaded

public synchronized <T> void
load(String
the file name (interpretation depends on AssetLoader)
fileName
,
Class<T>
the type of the asset.
type
)

Adds the given asset to the loading queue of the AssetManager.

public synchronized <T> void
load(String
the file name (interpretation depends on AssetLoader)
fileName
,
Class<T>
the type of the asset.
type
,
AssetLoaderParameters<T>
parameters for the AssetLoader.
parameter
)

Adds the given asset to the loading queue of the AssetManager.

public synchronized void
load(AssetDescriptor<T> desc)

Adds the given asset to the loading queue of the AssetManager.

private void
nextTask()

Removes a task from the loadQueue and adds it to the task stack.

public synchronized void
setErrorListener(AssetErrorListener
the listener or null
listener
)

Sets an AssetErrorListener to be invoked in case loading an asset failed.

public synchronized <T, P extends AssetLoaderParameters<T>> void
setLoader(Class<T>
the type of the asset
type
,
AssetLoader<T, P>
the loader
loader
)

Sets a new AssetLoader for the given type.

public synchronized <T, P extends AssetLoaderParameters<T>> void
setLoader(Class<T>
the type of the asset
type
,
String
the suffix the filename must have for this loader to be used or null to specify the default loader.
suffix
,
AssetLoader<T, P>
the loader
loader
)

Sets a new AssetLoader for the given type.

public void
setLogger(Logger logger)

public synchronized void
setReferenceCount(String fileName, int refCount)

Sets the reference count of an asset.

protected void
taskFailed(AssetDescriptor<T> assetDesc, RuntimeException ex)

Called when a task throws an exception during loading.

public synchronized void
unload(String
the file name
fileName
)

Removes the asset and all its dependencies, if they are not used by other assets.

public synchronized boolean

Returns:

true if all loading is finished.
update
()

Updates the AssetManager for a single task.

public boolean

Returns:

true if all loading is finished.
update
(int millis)

Updates the AssetManager continuously for the specified number of milliseconds, yielding the CPU to the loading thread between updates.

private boolean

Returns:

true if the asset is loaded or the task was cancelled.
updateTask
()

Updates the current task on the top of the task stack.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

assetDependenciesback to summary
pack-priv final ObjectMap<String, Array<String>> assetDependencies
assetsback to summary
pack-priv final ObjectMap<Class<T>, ObjectMap<String, AssetManager.RefCountedContainer>> assets
assetTypesback to summary
pack-priv final ObjectMap<String, Class<T>> assetTypes
executorback to summary
pack-priv final AsyncExecutor executor
injectedback to summary
pack-priv final ObjectSet<String> injected
listenerback to summary
pack-priv AssetErrorListener listener
loadedback to summary
pack-priv int loaded
loadersback to summary
pack-priv final ObjectMap<Class<T>, ObjectMap<String, AssetLoader<T, P extends AssetLoaderParameters<T>>>> loaders
loadQueueback to summary
pack-priv final Array<AssetDescriptor<T>> loadQueue
logback to summary
pack-priv Logger log
peakTasksback to summary
pack-priv int peakTasks
resolverback to summary
pack-priv final FileHandleResolver resolver
tasksback to summary
pack-priv final Array<AssetLoadingTask> tasks
toLoadback to summary
pack-priv int toLoad

Constructor Detail

AssetManagerback to summary
public AssetManager()

Creates a new AssetManager with all default loaders.

AssetManagerback to summary
public AssetManager(FileHandleResolver resolver)

Creates a new AssetManager with all default loaders.

AssetManagerback to summary
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:boolean

whether to add the default loaders

Method Detail

addAssetback to summary
protected <T> void addAsset(final String fileName, Class<T> type, T asset)

Adds an asset to this AssetManager

addTaskback to summary
private void addTask(AssetDescriptor<T> assetDesc)

Adds a AssetLoadingTask to the task stack for the given asset.

clearback to summary
public void clear()

Clears and disposes all assets and the preloading queue.

containsback to summary
public synchronized boolean contains(String fileName)

Returns true if an asset with the specified name is loading, queued to be loaded, or has been loaded.

containsback to summary
public synchronized boolean contains(String fileName, Class<T> type)

Returns true if an asset with the specified name and type is loading, queued to be loaded, or has been loaded.

containsAssetback to summary
public synchronized <T> boolean containsAsset(T asset)
Parameters
asset:T

the asset

Returns:boolean

whether the asset is contained in this manager

disposeback to summary
public void dispose()

Implements com.badlogic.gdx.utils.Disposable.dispose.

Disposes all assets in the manager and stops all asynchronous loading.

Annotations
@Override
finishLoadingback to summary
public void finishLoading()

Blocks until all assets are loaded.

finishLoadingAssetback to summary
public <T> T finishLoadingAsset(AssetDescriptor<T> assetDesc)

Blocks until the specified asset is loaded.

Parameters
assetDesc:AssetDescriptor<T>

the AssetDescriptor of the asset

finishLoadingAssetback to summary
public <T> T finishLoadingAsset(String fileName)

Blocks until the specified asset is loaded.

Parameters
fileName:String

the file name (interpretation depends on AssetLoader)

getback to summary
public synchronized <T> T get(String fileName)
Parameters
fileName:String

the asset file name

Returns:T

the asset

Exceptions
GdxRuntimeException:
if the asset is not loaded
getback to summary
public synchronized <T> T get(String fileName, Class<T> type)
Parameters
fileName:String

the asset file name

type:Class<T>

the asset type

Returns:T

the asset

Exceptions
GdxRuntimeException:
if the asset is not loaded
getback to summary
public synchronized <T> T get(String fileName, boolean required)
Parameters
fileName:String

the asset file name

required:boolean

true to throw GdxRuntimeException if the asset is not loaded, else null is returned

Returns:T

the asset or null if it is not loaded and required is false

Annotations
@Null
getback to summary
public synchronized <T> T get(String fileName, Class<T> type, boolean required)
Parameters
fileName:String

the asset file name

type:Class<T>

the asset type

required:boolean

true to throw GdxRuntimeException if the asset is not loaded, else null is returned

Returns:T

the asset or null if it is not loaded and required is false

Annotations
@Null
getback to summary
public synchronized <T> T get(AssetDescriptor<T> assetDescriptor)
Parameters
assetDescriptor:AssetDescriptor<T>

the asset descriptor

Returns:T

the asset

Exceptions
GdxRuntimeException:
if the asset is not loaded
getAllback to summary
public synchronized <T> Array<T> getAll(Class<T> type, Array<T> out)
Parameters
type:Class<T>

the asset type

Returns:Array<T>

all the assets matching the specified type

getAssetFileNameback to summary
public synchronized <T> String getAssetFileName(T asset)
Parameters
asset:T

the asset

Returns:String

the filename of the asset or null

getAssetNamesback to summary
public synchronized Array<String> getAssetNames()
Returns:Array<String>

the file names of all loaded assets.

getAssetTypeback to summary
public synchronized Class<T> getAssetType(String fileName)
Returns:Class<T>

the type of a loaded asset.

getDependenciesback to summary
public synchronized Array<String> getDependencies(String fileName)
Returns:Array<String>

the dependencies of an asset or null if the asset has no dependencies.

getDiagnosticsback to summary
public synchronized String getDiagnostics()
Returns:String

a string containing ref count and dependency information for all assets.

getFileHandleResolverback to summary
public FileHandleResolver getFileHandleResolver()

Returns the FileHandleResolver for which this AssetManager was loaded with.

Returns:FileHandleResolver

the file handle resolver which this AssetManager uses

getLoadedAssetsback to summary
public synchronized int getLoadedAssets()
Returns:int

the number of loaded assets

getLoaderback to summary
public <T> AssetLoader<T, P extends AssetLoaderParameters<T>> getLoader(final Class<T> type)

Returns the default loader for the given type.

Parameters
type:Class<T>

The type of the loader to get

Returns:AssetLoader<T, P extends AssetLoaderParameters<T>>

The loader capable of loading the type, or null if none exists

getLoaderback to summary
public <T> AssetLoader<T, P extends AssetLoaderParameters<T>> getLoader(final Class<T> type, final 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:Class<T>

The type of the loader to get

fileName:String

The filename of the asset to get a loader for, or null to get the default loader

Returns:AssetLoader<T, P extends AssetLoaderParameters<T>>

The loader capable of loading the type and filename, or null if none exists

getLoggerback to summary
public Logger getLogger()
Returns:Logger

the Logger used by the AssetManager

getProgressback to summary
public synchronized float getProgress()
Returns:float

the progress in percent of completion.

getQueuedAssetsback to summary
public synchronized int getQueuedAssets()
Returns:int

the number of currently queued assets

getReferenceCountback to summary
public synchronized int getReferenceCount(String fileName)

Returns the reference count of an asset.

handleTaskErrorback to summary
private void handleTaskError(Throwable t)

Handles a runtime/loading error in update() by optionally invoking the AssetErrorListener.

incrementRefCountedDependenciesback to summary
private void incrementRefCountedDependencies(String parent)
injectDependenciesback to summary
pack-priv synchronized void injectDependencies(String parentAssetFilename, Array<AssetDescriptor<T>> dependendAssetDescs)
injectDependencyback to summary
private synchronized void injectDependency(String parentAssetFilename, AssetDescriptor<T> dependendAssetDesc)
isFinishedback to summary
public synchronized boolean isFinished()

Returns true when all assets are loaded. Can be called from any thread but note update() or related methods must be called to process tasks.

isLoadedback to summary
public synchronized boolean isLoaded(AssetDescriptor<T> assetDesc)
Parameters
assetDesc:AssetDescriptor<T>

the AssetDescriptor of the asset

Returns:boolean

whether the asset is loaded

isLoadedback to summary
public synchronized boolean isLoaded(String fileName)
Parameters
fileName:String

the file name of the asset

Returns:boolean

whether the asset is loaded

isLoadedback to summary
public synchronized boolean isLoaded(String fileName, Class<T> type)
Parameters
fileName:String

the file name of the asset

Returns:boolean

whether the asset is loaded

loadback to summary
public synchronized <T> void load(String fileName, Class<T> type)

Adds the given asset to the loading queue of the AssetManager.

Parameters
fileName:String

the file name (interpretation depends on AssetLoader)

type:Class<T>

the type of the asset.

loadback to summary
public synchronized <T> void load(String fileName, Class<T> type, AssetLoaderParameters<T> parameter)

Adds the given asset to the loading queue of the AssetManager.

Parameters
fileName:String

the file name (interpretation depends on AssetLoader)

type:Class<T>

the type of the asset.

parameter:AssetLoaderParameters<T>

parameters for the AssetLoader.

loadback to summary
public synchronized void load(AssetDescriptor<T> desc)

Adds the given asset to the loading queue of the AssetManager.

Parameters
desc:AssetDescriptor<T>

the AssetDescriptor

nextTaskback to summary
private void nextTask()

Removes a task from the loadQueue and adds it to the task stack. If the asset is already loaded (which can happen if it was a dependency of a previously loaded asset) its reference count will be increased.

setErrorListenerback to summary
public synchronized void setErrorListener(AssetErrorListener listener)

Sets an AssetErrorListener to be invoked in case loading an asset failed.

Parameters
listener:AssetErrorListener

the listener or null

setLoaderback to summary
public synchronized <T, P extends AssetLoaderParameters<T>> void setLoader(Class<T> type, AssetLoader<T, P> loader)

Sets a new AssetLoader for the given type.

Parameters
type:Class<T>

the type of the asset

loader:AssetLoader<T, P>

the loader

setLoaderback to summary
public synchronized <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:Class<T>

the type of the asset

suffix:String

the suffix the filename must have for this loader to be used or null to specify the default loader.

loader:AssetLoader<T, P>

the loader

setLoggerback to summary
public void setLogger(Logger logger)
setReferenceCountback to summary
public synchronized void setReferenceCount(String fileName, int refCount)

Sets the reference count of an asset.

taskFailedback to summary
protected void taskFailed(AssetDescriptor<T> 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.

unloadback to summary
public synchronized void unload(String fileName)

Removes the asset and all its dependencies, if they are not used by other assets.

Parameters
fileName:String

the file name

updateback to summary
public synchronized 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:boolean

true if all loading is finished.

updateback to summary
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 (see update()).

Returns:boolean

true if all loading is finished.

updateTaskback to summary
private boolean updateTask()

Updates the current task on the top of the task stack.

Returns:boolean

true if the asset is loaded or the task was cancelled.

com.badlogic.gdx.assets back to summary

pack-priv Class AssetManager.RefCountedContainer

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv Object
pack-priv int

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

objectback to summary
pack-priv Object object
refCountback to summary
pack-priv int refCount

Constructor Detail

RefCountedContainerback to summary
pack-priv RefCountedContainer()