Skip to content

AssetRef Class

A typed, lazy asset reference. Declare fields of this type on an AssetManifest subclass, then call PreloadAsync(AssetManifest, IProgress<AssetLoadProgress>, CancellationToken) to resolve them all in parallel.

public sealed class AssetRef<T>
    where T : class

Type parameters

T

Inheritance System.Object → AssetRef\<T>

Example

public class GameAssets : AssetManifest
{
    public readonly AssetRef<ITexture>    Player    = Texture("assets/images/player.png");
    public readonly AssetRef<ISoundEffect> JumpSound = Sound("assets/audio/jump.wav");
    public readonly AssetRef<IMusic>       Theme     = Music("assets/audio/theme.ogg");
    public readonly AssetRef<Font>         UIFont    = Font("assets/fonts/ui.ttf", size: 16);
}

// In OnLoadAsync:
await _assets.PreloadAsync(_manifest, progress: loadingScreen.Progress, ct);

// After loading, implicit conversion; no .Value needed:
sprite.Texture = _manifest.Player;
Properties
IsLoaded Whether this ref has been resolved by PreloadAsync(AssetManifest, IProgress<AssetLoadProgress>, CancellationToken).
Path Path used to identify this asset (display/logging only).
Value The loaded asset. Throws System.InvalidOperationException if accessed before loading.
Methods
TryGetValue(T) Atomically checks whether this ref has been resolved and retrieves the value. Use this instead of IsLoaded + Value when a concurrent Unload(AssetManifest) could reset the ref between the two reads.
Operators
implicit operator T(AssetRef<T>) Implicit conversion so the ref can be used directly wherever T is expected, without .Value. Throws System.InvalidOperationException if the asset has not been loaded; only use this in code paths that run after PreloadAsync(AssetManifest, IProgress<AssetLoadProgress>, CancellationToken) has completed. For safe access in code that may race with Unload(AssetManifest), prefer TryGetValue(T).