AssetManager
Declaration
public class Strawberry.Core.AssetManager
A hierarchical string-keyed registry for engine assets (textures, sprites, fonts, sounds, layers, prefabs, etc.). Supports reverse lookup (asset → name) Lookup order: 1. this manager 2. parent manager 3. return null.
Inherits: Strawberry.DisposableReferenceObject
Constructors
AssetManager
Creates a root asset manager with no parent. Use this for the global managers (Like IGameContext.Assets).
void AssetManager()
AssetManager
Creates an asset manager that falls back to parent for assets not found locally. (Like Scene.Assets)
void AssetManager(AssetManager parent)
Parameters:
| Name | Type | Description |
|---|---|---|
parent |
Strawberry.Core.AssetManager |
The parent manager to fall back to, or null for a root. |
Properties
Parent
Gets the parent asset manager, or null if this is a root (global) manager.
AssetManager Parent { get }
Count
Gets the number of assets registered directly in this manager (excluding assets in parent managers).
int Count { get }
Methods
Register
Registers an asset under name. The manager takes ownership of the asset (will dispose it on Dispose) unless owned is false. Names must be unique within a single manager.
void Register<T>(string name, T asset, bool owned = true)
Parameters:
| Name | Type | Description |
|---|---|---|
name |
System.String |
Unique name for the asset within this manager. |
asset |
T |
The asset object. |
owned |
System.Boolean = true |
If true (default), the asset will be disposed when this manager is disposed. If false, the asset's lifetime is managed elsewhere. |
Type Parameters:
T— The asset type.
Exceptions:
System.ArgumentNullException—nameorassetis null.System.ArgumentException— An asset with the same name is already registered in this manager.
Unregister
Unregisters an asset by name. The asset is NOT disposed.
object Unregister(string name)
Parameters:
| Name | Type | Description |
|---|---|---|
name |
System.String |
The name of the asset to unregister. |
Returns: The unregistered asset, or null if no asset with that name was found.
Unregister
Unregisters a specific asset instance (by reference identity). Will not unregister on parent managers (only the current one).
string Unregister<T>(T asset)
Parameters:
| Name | Type | Description |
|---|---|---|
asset |
T |
The asset instance to unregister. |
Returns: The name the asset was registered under, or null if not found.
Type Parameters:
T— The asset type.
Clear
Removes all assets from this manager.
void Clear()
Get
Gets an asset by name, walking the parent chain if not found locally.
T Get<T>(string name)
Parameters:
| Name | Type | Description |
|---|---|---|
name |
System.String |
The asset name. |
Returns: The asset.
Type Parameters:
T— The expected asset type.
Exceptions:
System.Collections.Generic.KeyNotFoundException— No asset with the given name exists in this manager or any parent.System.InvalidCastException— An asset with the name exists but is not of typeT.
TryGet
Tries to get an asset by name, walking the parent chain if not found locally.
bool TryGet<T>(string name, out T asset)
Parameters:
| Name | Type | Description |
|---|---|---|
name |
System.String |
The name of the asset |
asset |
out T |
The asset object if found and the type is T or null |
Returns: True if the asset is found and the type is T otherwise null
Type Parameters:
T— The type of the asset
GetObject
Non-generic get. Returns the raw object or null if not found.
object GetObject(string name)
Parameters:
| Name | Type | Description |
|---|---|---|
name |
System.String |
Contains
Checks if an asset with the given name exists in this manager or any parent.
bool Contains(string name)
Parameters:
| Name | Type | Description |
|---|---|---|
name |
System.String |
The asset name |
Returns: True if asset exists in the manager or its parent chain, otherwise false
Contains
Checks if an asset of type T with the given name exists in this manager or any parent.
bool Contains<T>(string name)
Parameters:
| Name | Type | Description |
|---|---|---|
name |
System.String |
The asset name |
Returns: True if asset exists in the manager or its parent chain, otherwise false
Type Parameters:
T— The expected asset type
GetName
Returns the name an asset is registered under, or null if the asset is not registered in this manager or any parent. If an asset is registered under multiple names in the same manager, the first registered name is returned.
string GetName<T>(T asset)
Parameters:
| Name | Type | Description |
|---|---|---|
asset |
T |
The asset instance. |
Returns: The asset's registered name, or null if not found.
Type Parameters:
T— The asset type.
TryGetName
Tries to get the name of an asset.
bool TryGetName<T>(T asset, out string name)
Parameters:
| Name | Type | Description |
|---|---|---|
asset |
T |
The asset object to look for |
name |
out System.String |
The name of the asset or null if the asset does not exists in the manager or its parent chain |
Returns: Returns true if the name was found, false otherwise
Type Parameters:
T— The expected asset type
GetLocalAssets
Returns an enumerable sequence of all (name, asset) pairs registered directly in this manager. Does NOT include parent assets.
IEnumerable<KeyValuePair<string, object>> GetLocalAssets()
GetLocalNames
Returns all asset names registered directly in this manager. Does NOT include parent assets.
IEnumerable<string> GetLocalNames()
CleanUnmanaged protected override
void CleanUnmanaged()