public interface AssetManager
AssetManager
provides an interface for managing the data assets
of a jME3 application.
The asset manager provides a means to register AssetLocator
s,
which are used to find asset data on disk, network, or other file system.
The asset locators are invoked in order of addition to find the asset data.
Use the registerLocator(java.lang.String, java.lang.Class)
method
to add new AssetLocator
s.
Some examples of locators:
FileLocator
- Used to find assets on the local file system.ClasspathLocator
- Used to find assets in the Java classpath
The asset data is represented by the AssetInfo
class, this
data is passed into the registered AssetLoader
s in order to
convert the data into a usable object. Use the
registerLoader(java.lang.Class, java.lang.String[])
method
to add loaders.
Some examples of loaders:
Once the asset has been loaded, it will be
post-processed
by the key's processor
.
If the key specifies a cache type
, the asset
will be cached in the specified cache. Next, the AssetProcessor
will be requested to AssetProcessor.createClone(java.lang.Object)
generate a clone for the asset. Some assets do not require cloning,
such as immutable or shared assets. Others, like models, must be cloned
so that modifications to one instance do not leak onto others.
Modifier and Type | Method and Description |
---|---|
void |
addAssetEventListener(AssetEventListener listener)
Add an
AssetEventListener to receive events from this
AssetManager . |
void |
addClassLoader(java.lang.ClassLoader loader)
Adds a
ClassLoader that is used to load classes
that are needed for finding and loading Assets. |
<T> void |
addToCache(AssetKey<T> key,
T asset)
Inject an asset into the asset cache.
|
void |
clearAssetEventListeners()
Removes all asset event listeners.
|
void |
clearCache()
Clears the asset cache.
|
<T> boolean |
deleteFromCache(AssetKey<T> key)
Delete an asset from the asset cache.
|
java.util.List<java.lang.ClassLoader> |
getClassLoaders()
Retrieve the list of registered ClassLoaders that are used for loading
classes from asset files. |
<T> T |
getFromCache(AssetKey<T> key)
Retrieve an asset from the asset cache.
|
ShaderGenerator |
getShaderGenerator(java.util.EnumSet<Caps> caps)
Returns the shaderGenerator responsible for generating the shaders
|
<T> T |
loadAsset(AssetKey<T> key)
Load an asset from a key, the asset will be located
by one of the
AssetLocator implementations provided in the
registerLocator(java.lang.String, java.lang.Class)
call. |
java.lang.Object |
loadAsset(java.lang.String name)
Load an asset by name, calling this method is the same as calling
loadAsset(new AssetKey(name)) . |
<T> T |
loadAssetFromStream(AssetKey<T> key,
java.io.InputStream inputStream)
Load an asset from an
InputStream . |
AudioData |
loadAudio(AudioKey key)
Load audio file, supported types are WAV or OGG.
|
AudioData |
loadAudio(java.lang.String name)
Load audio file, supported types are WAV or OGG.
|
FilterPostProcessor |
loadFilter(FilterKey key)
Loads a filter *.j3f file with a FilterKey.
|
FilterPostProcessor |
loadFilter(java.lang.String name)
Loads a filter *.j3f file with a FilterKey.
|
BitmapFont |
loadFont(java.lang.String name)
Load a font file.
|
Material |
loadMaterial(java.lang.String name)
Load a material instance (J3M) file.
|
Spatial |
loadModel(ModelKey key)
Loads a 3D model with a ModelKey.
|
Spatial |
loadModel(java.lang.String name)
Loads a 3D model.
|
Texture |
loadTexture(java.lang.String name)
Loads texture file, supported types are BMP, JPG, PNG, GIF,
TGA, DDS, PFM, and HDR.
|
Texture |
loadTexture(TextureKey key)
Loads texture file, supported types are BMP, JPG, PNG, GIF,
TGA, DDS, PFM, and HDR.
|
AssetInfo |
locateAsset(AssetKey<?> key)
Manually locates an asset with the given
AssetKey . |
void |
registerLoader(java.lang.Class<? extends AssetLoader> loaderClass,
java.lang.String... extensions)
Register an
AssetLoader by using a class object. |
void |
registerLocator(java.lang.String rootPath,
java.lang.Class<? extends AssetLocator> locatorClass)
Registers the given locator class for locating assets with this
AssetManager . |
void |
removeAssetEventListener(AssetEventListener listener)
Remove an
AssetEventListener from receiving events from this
AssetManager |
void |
removeClassLoader(java.lang.ClassLoader loader)
Remove a
ClassLoader from the list of registered ClassLoaders |
void |
setShaderGenerator(ShaderGenerator generator)
Sets the shaderGenerator to generate shaders based on shaderNodes.
|
void |
unregisterLoader(java.lang.Class<? extends AssetLoader> loaderClass)
Unregister a
AssetLoader from loading its assigned extensions. |
void |
unregisterLocator(java.lang.String rootPath,
java.lang.Class<? extends AssetLocator> locatorClass)
Unregisters the given locator class.
|
void addClassLoader(java.lang.ClassLoader loader)
ClassLoader
that is used to load classes
that are needed for finding and loading Assets.
This does not allow loading assets from that classpath,
use registerLocator for that.loader
- A ClassLoader that Classes in asset files can be loaded from.void removeClassLoader(java.lang.ClassLoader loader)
ClassLoader
from the list of registered ClassLoadersjava.util.List<java.lang.ClassLoader> getClassLoaders()
classes
from asset files.void registerLoader(java.lang.Class<? extends AssetLoader> loaderClass, java.lang.String... extensions)
AssetLoader
by using a class object.loaderClass
- The loader class to register.extensions
- Which extensions this loader is responsible for loading,
if there are already other loaders registered for that extension, they
will be overridden - there should only be one loader for each extension.void unregisterLoader(java.lang.Class<? extends AssetLoader> loaderClass)
AssetLoader
from loading its assigned extensions.
This undoes the effect of calling
registerLoader(java.lang.Class, java.lang.String[])
.loaderClass
- The loader class to unregister.registerLoader(java.lang.Class, java.lang.String[])
void registerLocator(java.lang.String rootPath, java.lang.Class<? extends AssetLocator> locatorClass)
AssetManager
. AssetLocator
s are invoked in the order
they were registered, to locate the asset by the AssetKey
.
Once an AssetLocator
returns a non-null AssetInfo, it is sent
to the AssetLoader
to load the asset.
Once a locator is registered, it can be removed via
unregisterLocator(java.lang.String, java.lang.Class)
.rootPath
- Specifies the root path from which to locate assets
for the given AssetLocator
. The purpose of this parameter
depends on the type of the AssetLocator
.locatorClass
- The class type of the AssetLocator
to register.AssetLocator.setRootPath(java.lang.String)
,
AssetLocator.locate(com.jme3.asset.AssetManager, com.jme3.asset.AssetKey)
,
unregisterLocator(java.lang.String, java.lang.Class)
void unregisterLocator(java.lang.String rootPath, java.lang.Class<? extends AssetLocator> locatorClass)
registerLocator(java.lang.String, java.lang.Class)
.rootPath
- Should be the same as the root path specified in registerLocator(java.lang.String, java.lang.Class)
.locatorClass
- The locator class to unregisterregisterLocator(java.lang.String, java.lang.Class)
void addAssetEventListener(AssetEventListener listener)
AssetEventListener
to receive events from this
AssetManager
.listener
- The asset event listener to addvoid removeAssetEventListener(AssetEventListener listener)
AssetEventListener
from receiving events from this
AssetManager
listener
- The asset event listener to removevoid clearAssetEventListeners()
AssetInfo locateAsset(AssetKey<?> key)
AssetKey
.
This method should be used for debugging or internal uses.
AssetLocator
that are registered with this AssetManager
,
in the same way that the loadAsset(com.jme3.asset.AssetKey)
method locates assets.key
- The AssetKey
to locate.AssetInfo
object returned from the AssetLocator
that located the asset, or null if the asset cannot be located.<T> T loadAssetFromStream(AssetKey<T> key, java.io.InputStream inputStream)
InputStream
.
In some cases it may be required to load an asset from memory
or arbitrary streams so that registering a custom locator and key
type is not necessary.T
- The object type that will be loaded from the AssetKey instance.key
- The AssetKey. Note that the asset will not be cached -
following the same behavior as if AssetKey.getCacheType()
returned null.inputStream
- The input stream from which the asset shall be loaded.AssetLoadException
- If the AssetLoader
has failed
to load the asset due to an IOException
or another error.<T> T loadAsset(AssetKey<T> key)
AssetLocator
implementations provided in the
registerLocator(java.lang.String, java.lang.Class)
call. If located successfully, it will be loaded via the appropriate
AssetLoader
implementation based on the file's extension, as
specified in the call
registerLoader(java.lang.Class, java.lang.String[])
.T
- The object type that will be loaded from the AssetKey instance.key
- The AssetKeyAssetNotFoundException
- If all registered locators have failed
to locate the asset.AssetLoadException
- If the AssetLoader
has failed
to load the asset due to an IOException
or another error.java.lang.Object loadAsset(java.lang.String name)
loadAsset(new AssetKey(name))
.name
- The name of the asset to load.loadAsset(com.jme3.asset.AssetKey)
Texture loadTexture(TextureKey key)
key
- The TextureKey
to use for loading.loadAsset(com.jme3.asset.AssetKey)
Texture loadTexture(java.lang.String name)
name
- The name of the texture to load.loadAsset(com.jme3.asset.AssetKey)
AudioData loadAudio(AudioKey key)
key
- Asset key of the audio file to loadloadAsset(com.jme3.asset.AssetKey)
AudioData loadAudio(java.lang.String name)
name
- Asset name of the audio file to loadloadAsset(com.jme3.asset.AssetKey)
Spatial loadModel(ModelKey key)
key
- Asset key of the model to loadloadAsset(com.jme3.asset.AssetKey)
Spatial loadModel(java.lang.String name)
name
- Asset name of the model to loadloadAsset(com.jme3.asset.AssetKey)
Material loadMaterial(java.lang.String name)
name
- Asset name of the material to loadloadAsset(com.jme3.asset.AssetKey)
BitmapFont loadFont(java.lang.String name)
name
- Asset name of the font to loadloadAsset(com.jme3.asset.AssetKey)
FilterPostProcessor loadFilter(FilterKey key)
key
- Asset key of the filter file to loadloadAsset(com.jme3.asset.AssetKey)
FilterPostProcessor loadFilter(java.lang.String name)
name
- Asset name of the filter file to loadloadAsset(com.jme3.asset.AssetKey)
void setShaderGenerator(ShaderGenerator generator)
generator
- the shaderGeneratorShaderGenerator getShaderGenerator(java.util.EnumSet<Caps> caps)
<T> T getFromCache(AssetKey<T> key)
T
- The object type that will be retrieved from the AssetKey instance.key
- The AssetKey to get from the cache.null
.java.lang.IllegalArgumentException
- If caching
is disabled for the key.<T> void addToCache(AssetKey<T> key, T asset)
T
- The object type of the asset.key
- The key where the asset shall be stored.asset
- The asset to inject into the cache.java.lang.IllegalArgumentException
- If caching
is disabled for the key.<T> boolean deleteFromCache(AssetKey<T> key)
T
- The object type of the AssetKey instance.key
- The asset key to remove from the cache.java.lang.IllegalArgumentException
- If caching
is disabled for the key.void clearCache()