Class UnityAchievementScreenshotLoader
Reads a stored unlock screenshot back as a UnityEngine.Sprite the achievements view can show, and owns the textures it created until they are released.
Inherited Members
Namespace: Serenity.Achievements.Infrastructure.Services
Assembly: Serenity.UnityAchievements.Infrastructure.dll
Syntax
public class UnityAchievementScreenshotLoader
Remarks
Keyed by the screenshot key the unlock record carries rather than by achievement id: the key is what the record actually stores, it stays right when the signed-in player changes, and it is the only thing the blob store can be addressed by. Every key is checked against AchievementScreenshotKey first — it comes out of persisted data, and a view that renders whatever a save file names is a view that reads arbitrary files.
Every load is cached, misses and undecodable bytes included — a record whose blob is gone must not re-hit the disk once per list rebuild — and concurrent requests for one key share a single read. The cache is capped at MaximumCachedSprites and evicts the oldest entry, because a project with hundreds of achievements would otherwise hold every full-size thumbnail texture it ever showed for the rest of the session.
The shared read runs on None on purpose. One caller's token must not cancel a read every other caller is awaiting, and the read is a bounded blob fetch whose result is cached — so the worst a caller that walked away costs is one decoded sprite that the eviction policy will reclaim.
Sprites and their textures are created here, so nothing else may destroy them: call Release() when the view that showed them goes away. Main thread only, and unsynchronized because of it: everything it produces is a UnityEngine.Object, which may only be created there anyway.
Constructors
UnityAchievementScreenshotLoader(BlobAchievementScreenshotStore, ILogService)
Initializes a new instance of the UnityAchievementScreenshotLoader class.
Declaration
public UnityAchievementScreenshotLoader(BlobAchievementScreenshotStore screenshotStore, ILogService logService = null)
Parameters
| Type | Name | Description |
|---|---|---|
| BlobAchievementScreenshotStore | screenshotStore | The store the bytes are read from. |
| ILogService | logService | Service used to report a refused key or bytes that would not decode. May be |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when |
Fields
MaximumCachedSprites
How many decoded screenshots are held before the oldest is destroyed.
Declaration
public const int MaximumCachedSprites = 128
Field Value
| Type | Description |
|---|---|
| int |
Properties
CachedSpriteCount
How many decoded screenshots the loader is currently holding.
Declaration
public int CachedSpriteCount { get; }
Property Value
| Type | Description |
|---|---|
| int |
Methods
LoadAsync(string, CancellationToken)
Loads the screenshot stored under a key, or hands back null when there is none to show.
Declaration
public Task<Sprite> LoadAsync(string screenshotKey, CancellationToken cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| string | screenshotKey | The key the unlock record carries. |
| CancellationToken | cancellationToken | Token checked before the read starts; the shared read itself is not bound to it. |
Returns
| Type | Description |
|---|---|
| Task<Sprite> | The sprite to show, or |
Release()
Destroys every sprite and texture this loader created and forgets what it read.
Declaration
public void Release()
Remarks
Also the way to react to the signed-in player changing: the keys of the previous player mean nothing to the new one, and their textures are the loader's to free.
TryGetLoaded(string, out Sprite)
Hands back an already-loaded screenshot without touching the store, so a view can draw a row synchronously and only start a load for the rows it has never shown.
Declaration
public bool TryGetLoaded(string screenshotKey, out Sprite sprite)
Parameters
| Type | Name | Description |
|---|---|---|
| string | screenshotKey | The key the unlock record carries. |
| Sprite | sprite | The loaded sprite, or |
Returns
| Type | Description |
|---|---|
| bool | True when this key has already been loaded, successfully or not. |