Class UnityAssetPrefetcherService
Unity-specific asset prefetcher service with LRU cache and memory budget management. Designed to run as a single component instance per scene, providing efficient asset caching.
Namespace: Serenity.AssetPrefetcher.Infrastructure.Services
Assembly: Serenity.UnityAssetPrefetcher.Infrastructure.dll
Syntax
public class UnityAssetPrefetcherService : MonoBehaviour, IAssetPrefetcherService, IService, IFoundationSettings
Constructors
UnityAssetPrefetcherService()
Declaration
public UnityAssetPrefetcherService()
Properties
Guid
Declaration
public string Guid { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
Id
Declaration
public string Id { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
LogService
Declaration
public ILogService LogService { get; set; }
Property Value
| Type | Description |
|---|---|
| ILogService |
Methods
Clear()
Clears all cached assets and releases associated Addressables handles.
Used by the editor "Clear" button for debugging and testing.
Declaration
public void Clear()
Initialize(PrefetchProfile, IAssetKeyResolver)
Initializes the service with the specified prefetch profile and optional key resolver.
Declaration
public void Initialize(PrefetchProfile profile, IAssetKeyResolver keyResolver = null)
Parameters
| Type | Name | Description |
|---|---|---|
| PrefetchProfile | profile | Prefetch profile containing budget and policy settings |
| IAssetKeyResolver | keyResolver | Optional asset key resolver for key transformation |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when profile is null |
IsCached(AssetKey)
Checks whether the specified asset key is currently cached in memory.
Declaration
public bool IsCached(AssetKey key)
Parameters
| Type | Name | Description |
|---|---|---|
| AssetKey | key | Asset key to check cache status for |
Returns
| Type | Description |
|---|---|
| bool | True if the asset is cached and loaded, false otherwise |
PrefetchAsync(string, AssetKey, PrefetchPriority)
Asynchronously prefetches an asset with the specified priority.
Declaration
public Task PrefetchAsync(string contextId, AssetKey key, PrefetchPriority priority)
Parameters
| Type | Name | Description |
|---|---|---|
| string | contextId | Context identifier for grouping related assets |
| AssetKey | key | Asset key to prefetch |
| PrefetchPriority | priority | Priority level for the prefetch operation |
Returns
| Type | Description |
|---|---|
| Task | Task representing the asynchronous prefetch operation |
PrewarmContextAsync(string, AssetKey[], PrefetchPolicy)
Asynchronously prewarms multiple assets for a specific context with the given policy.
Declaration
public Task PrewarmContextAsync(string contextId, AssetKey[] keys, PrefetchPolicy policy)
Parameters
| Type | Name | Description |
|---|---|---|
| string | contextId | Context identifier for grouping the assets |
| AssetKey[] | keys | Array of asset keys to prewarm |
| PrefetchPolicy | policy | Prefetch policy to apply for this operation |
Returns
| Type | Description |
|---|---|
| Task | Task representing the asynchronous prewarm operation |
QueryCacheAsync(IEnumerable<AssetKey>, CancellationToken)
Queries the cache status for multiple assets asynchronously.
Declaration
public Task<IReadOnlyDictionary<AssetKey, CacheStatusInfo>> QueryCacheAsync(IEnumerable<AssetKey> keys, CancellationToken ct = default)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<AssetKey> | keys | Collection of asset keys to query |
| CancellationToken | ct | Cancellation token for the operation |
Returns
| Type | Description |
|---|---|
| Task<IReadOnlyDictionary<AssetKey, CacheStatusInfo>> | Task containing a dictionary mapping asset keys to their cache status information |
ReleaseContext(string)
Releases all assets associated with the specified context.
Declaration
public void ReleaseContext(string contextId)
Parameters
| Type | Name | Description |
|---|---|---|
| string | contextId | Context identifier for the assets to release |
SnapshotCache(int)
Takes a snapshot of the current cache state for editor inspection and debugging.
Declaration
public IEnumerable<CacheEntrySnapshot> SnapshotCache(int max = 4096)
Parameters
| Type | Name | Description |
|---|---|---|
| int | max | Maximum number of entries to include in the snapshot |
Returns
| Type | Description |
|---|---|
| IEnumerable<CacheEntrySnapshot> | Enumerable collection of cache entry snapshots ordered by most recent access |
TryGetDiagnostics(out PrefetcherDiagnostics)
Attempts to retrieve current diagnostic information about the prefetcher state.
Declaration
public bool TryGetDiagnostics(out PrefetcherDiagnostics diagnostics)
Parameters
| Type | Name | Description |
|---|---|---|
| PrefetcherDiagnostics | diagnostics | Output parameter containing diagnostic information if available |
Returns
| Type | Description |
|---|---|
| bool | True if diagnostics were successfully retrieved, false otherwise |