AssetManager class
Main asset manager class
Assets are kept by path and type: one file can be two assets (an atlas and the JSON it is read from). Supports two usage patterns:
- Simple — load / unload for direct management. A plain load keeps the asset until unload, unloadAll, retainOnly or dispose: no release lets it go.
- Reference-counted — acquire / release paired with an AssetScope or a MemoryScope for scene-based lifetimes. Each acquire is one count on that path and type; the last release unloads the asset — unless a plain load also asked for it.
unload, unloadAll and retainOnly unload whatever they name, counts or not: use them only for what nothing still draws or plays.
Each engine has one, in its context (engine.assets,
engine.context.assets); assets it loads read from its bundle.
Constructors
- AssetManager({AssetBundle? bundle})
-
A manager reading from
bundle— the app'srootBundlewhen null — with the built-in loaders registered.
Properties
- assetCount → int
-
Get number of loaded assets
no setter
- bundle → AssetBundle
-
Where the built-in assets read their bytes from.
final
-
cacheStats
→ Map<
String, dynamic> -
Get cache statistics
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
-
reloadListeners
→ List<
void Function(String path)> -
Called with a file's path after it was loaded again (a model edited on
disk, say), so what was built from it can follow: 3-D physics rebuilds
the colliders made from a model.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- totalMemoryUsage → int
-
Get total memory usage
no setter
Methods
-
acquire(
String path, AssetType type) → Future< Asset> - Load (or reuse) an asset and increment its reference count.
-
acquireAudio(
String path) → Future< AudioAsset> - Typed convenience for acquire.
-
acquireAudioScoped(
String path, MemoryScope scope) → Future< AudioAsset> - Convenience for acquireScoped.
-
acquireBinaryScoped(
String path, MemoryScope scope) → Future< BinaryAsset> - Convenience for acquireScoped.
-
acquireImage(
String path) → Future< ImageAsset> - Typed convenience for acquire.
-
acquireImageScoped(
String path, MemoryScope scope) → Future< ImageAsset> - Convenience for acquireScoped.
-
acquireJsonScoped(
String path, MemoryScope scope) → Future< JsonAsset> - Convenience for acquireScoped.
-
acquireScoped(
String path, AssetType type, MemoryScope scope) → Future< Asset> -
Load or reuse an asset and automatically release it when
scopeis disposed. -
acquireTextScoped(
String path, MemoryScope scope) → Future< TextAsset> - Convenience for acquireScoped.
-
createScope(
{String? debugLabel}) → MemoryScope - Creates a reusable memory scope for scene or subsystem lifetime tracking.
-
dispose(
) → void - Clean up all assets
-
get<
T extends Asset> (String path) → T? -
The
Tloaded atpath, or null. -
getAudio(
String path) → AudioAsset? - Get an audio asset
-
getBinary(
String path) → BinaryAsset? - Get a binary asset
-
getImage(
String path) → ImageAsset? - Get an image asset
-
getJson(
String path) → JsonAsset? - Get a JSON asset
-
getText(
String path) → TextAsset? - Get a text asset
-
hasLoader(
AssetType type) → bool -
Returns
trueif a loader is registered fortype. -
initialize(
) → void - Registers the built-in loaders (image, audio, text, JSON, binary), replacing any registered for those types. The constructor has done this already; call it again after dispose to use the manager again.
-
isLoaded(
String path) → bool - Check if an asset is loaded
-
load<
T extends Asset> (String path, AssetType type) → Future< Asset> - Load an asset of specified type, and keep it until unload, unloadAll, retainOnly or dispose — the last release of an acquire of the same asset does not unload it.
-
loadAudio(
String path) → Future< AudioAsset> - Load an audio asset
-
loadBinary(
String path) → Future< BinaryAsset> - Load a binary asset
-
loadImage(
String path) → Future< ImageAsset> - Load an image asset
-
loadJson(
String path) → Future< JsonAsset> - Load a JSON asset
-
loadMultiple(
List< String> paths, AssetType type) → Future<List< Asset> > - Load multiple assets
-
loadText(
String path) → Future< TextAsset> - Load a text asset
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
notifyReloaded(
String path) → void -
Tells reloadListeners that the file at
pathwas loaded again. -
refCount(
String path, AssetType type) → int -
The current reference count of the
typeasset atpath(0 if not tracked). -
registerLoader(
AssetType type, AssetLoader loader) → void -
Register (or replace) an AssetLoader for
type. -
release(
String path, AssetType type) → void -
Decrement the reference count of the
typeasset atpath. -
retainOnly(
Iterable< String> paths) → void -
Unloads every asset — kept or still loading — whose path is not in
pathsand that no acquire holds: between levels, keep what the next one uses and let the rest go. What an acquire holds stays until its release. -
toString(
) → String -
A string representation of this object.
inherited
-
unload(
String path, [AssetType? type]) → void -
Unload the assets at
path— of every type, or onlytype— counted or not. One still loading is let go when it arrives. -
unloadAll(
) → void - Unload all assets, and let go of those still loading when they arrive.
-
unregisterLoader(
AssetType type, [AssetLoader? loader]) → void -
Removes the loader registered for
type— only when it isloader, if that is given, so one registered since is left alone.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited