Class AssetDirectory
- All Implemented Interfaces:
com.badlogic.gdx.utils.Disposable
The manager allows assets to be loaded normally, but it is best to use the
global loadAssets() method to load from the JSON directory. Once
that method is called, you may use getEntry(java.lang.String, java.lang.Class<T>) instead of
AssetManager.get(java.lang.String) to access an asset.
As with any AssetManager, this class loads assets via AssetLoader
objects. However, it also requires AssetParser objects. An asset
parser takes an JSON entry in the directory and instructs the appropriate
loader how to load that file. This class has built in parsers for the classes
Texture, TextureRegion, BitmapFont,
SoundEffect, MusicQueue, and JsonValue.
If you wish to add custom assets, you need to add BOTH a custom AssetLoader
and a custom AssetParser to this manager. While a type can only have
one loader per file suffix, there is no limit on the number of asset parsers
per type. Use the method addParser(edu.cornell.gdiac.assets.AssetParser<?>) to add additional asset parsers.
Right now, this class supports built-in reading for Textures (and Texture regions), audio assets, fonts, and other JSON files. To add more assets, you will need to extend this class.
We have decided not implement this class as a singleton. It is possible that
you may want more than one asset manager (for managing separate scenes). If you
need to globalize access to this asset manager, use ResourceManager.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThis class represents the top level index of an asset directory. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected AssetDirectory.IndexThe directory contents (including the map from JSON keys to file names)protected StringThe asset directory of this asset managerprotected com.badlogic.gdx.assets.loaders.FileHandleResolverThe resolver (converting strings to file handles)protected DirectoryLoaderThe dedicated loader for theAssetDirectory.Indexclass -
Constructor Summary
ConstructorsConstructorDescriptionAssetDirectory(String directory) Creates a new AssetDirectory from the given directory.AssetDirectory(String directory, com.badlogic.gdx.assets.loaders.FileHandleResolver resolver) Creates a new AssetDirectory from the given directory. -
Method Summary
Modifier and TypeMethodDescription<T> voidAdds an entry to the asset directory, associating it with the given key.voidaddParser(AssetParser<?> parser) Adds aAssetParserfor this directory loaderReturns the file name for the asset directory.<T> TReturns the asset associated with the given directory key<T> StringgetFileName(String key, Class<T> type) Returns the asset path associated with the given directory keycom.badlogic.gdx.utils.Array<AssetParser<?>>Returns theAssetParserobjects associated with this directory loaderfloatReturns the progress in percent of completion.<T> booleanReturns true if there is an asset associated with the given directory keyvoidLoads all assets defined by the asset directoryvoidremoveParser(AssetParser<?> parser) Removes aAssetParserfrom this directory loadervoidUnloads all assets previously loaded byloadAssets().Methods inherited from class com.badlogic.gdx.assets.AssetManager
addAsset, clear, contains, contains, containsAsset, dispose, finishLoading, finishLoadingAsset, finishLoadingAsset, get, get, get, get, get, getAll, getAssetFileName, getAssetNames, getAssetType, getDependencies, getDiagnostics, getFileHandleResolver, getLoadedAssets, getLoader, getLoader, getLogger, getQueuedAssets, getReferenceCount, isFinished, isLoaded, isLoaded, isLoaded, load, load, load, setErrorListener, setLoader, setLoader, setLogger, setReferenceCount, taskFailed, unload, update, update
-
Field Details
-
resolver
protected com.badlogic.gdx.assets.loaders.FileHandleResolver resolverThe resolver (converting strings to file handles) -
filename
The asset directory of this asset manager -
contents
The directory contents (including the map from JSON keys to file names) -
topLoader
The dedicated loader for theAssetDirectory.Indexclass
-
-
Constructor Details
-
AssetDirectory
Creates a new AssetDirectory from the given directory.This class uses an
InternalFileHandleResolverto convert asset file names into assets.- Parameters:
directory- The asset directory file name
-
AssetDirectory
public AssetDirectory(String directory, com.badlogic.gdx.assets.loaders.FileHandleResolver resolver) Creates a new AssetDirectory from the given directory.This class uses the given
FileHandleResolverto convert asset file names into assets.- Parameters:
directory- The asset directory file nameresolver- The file handle resolver
-
-
Method Details
-
getProgress
public float getProgress()Returns the progress in percent of completion.- Overrides:
getProgressin classcom.badlogic.gdx.assets.AssetManager- Returns:
- the progress in percent of completion.
-
getDirectory
Returns the file name for the asset directory.- Returns:
- the file name for the asset directory.
-
loadAssets
public void loadAssets()Loads all assets defined by the asset directoryEach asset must have an associated
AssetParserfor this to work. There are default parsers for the classesTexture,TextureRegion,BitmapFont,SoundEffect,MusicQueue,JsonValueas well as several others. Any additional asset parsers should be added with theaddParser(edu.cornell.gdiac.assets.AssetParser<?>)method. -
unloadAssets
public void unloadAssets()Unloads all assets previously loaded byloadAssets().Assets loaded manually (e.g. not via the asset directory JSON) will not be affected and will remain in this asset manager.
-
getParsers
Returns theAssetParserobjects associated with this directory loaderIf there are no asset parsers, then
loadAssets()will not generate any assets beyond the initial JSON file. The contents of the JSON file will be ignored.Each asset type may have multiple asset parsers
- Returns:
- the
AssetParserobjects associated with this directory loader
-
addParser
Adds aAssetParserfor this directory loaderIf there are no asset parsers, then
loadAssets()will not generate any assets beyond the initial JSON file. The contents of the JSON file will be ignored.Each asset type may have multiple asset parsers
- Parameters:
parser- TheAssetParserto add
-
removeParser
Removes aAssetParserfrom this directory loaderIf there are no asset parsers, then
loadAssets()will not generate any assets beyond the initial JSON file. The contents of the JSON file will be ignored.Each asset type may have multiple asset parsers
- Parameters:
parser- TheAssetParserto remove
-
getEntry
Returns the asset associated with the given directory keyThe method
loadAssets()must have been called for this method to return a value. In addition, it will not return a value until the asset is actually loaded.- Type Parameters:
T- the asset type- Parameters:
key- the asset directory keytype- the asset type- Returns:
- the asset associated with the given directory key
-
hasEntry
Returns true if there is an asset associated with the given directory keyThe method
loadAssets()must have been called for this method to return anything other than false. However, the asset does not need to be loaded for it to return true.- Type Parameters:
T- the asset type- Parameters:
key- the asset directory keytype- the asset type- Returns:
- true if there is an asset associated with the given directory key
-
addEntry
Adds an entry to the asset directory, associating it with the given key.This method is used to add assets without a loader. It is thread-safe due to the use of synchronization.
- Type Parameters:
T- the asset type- Parameters:
key- the unique identifier for the assettype- theClassobject representing the type of the assetasset- the asset instance to be added
-
getFileName
Returns the asset path associated with the given directory keyThe method
loadAssets()must have been called for this method to return a value. However, the asset does not have to have finished loading.- Type Parameters:
T- the asset type- Parameters:
key- the asset directory keytype- the asset type- Returns:
- the asset associated with the given directory key
-