Game.UI.Thumbnails.ThumbnailCache
Assembly: Assembly-CSharp
Namespace: Game.UI.Thumbnails
Type: class
Base: System.IDisposable
Summary:
ThumbnailCache is a simple in-memory cache for UI thumbnails (atlas frames / regions). It listens to atlas asset changes from the game's AssetDatabase, loads atlas frames and their entries into a dictionary keyed by ThumbnailKey (name + width + height), and exposes lookup and lifecycle operations (Initialize, Refresh, Dispose). Cached entries store a ThumbnailInfo that includes the atlas frame, region, an optional camera and a status flag used by UI code to determine whether a thumbnail is ready, pending, unavailable or needs refresh. The cache does not perform rendering itself — it only stores metadata and atlas references for quick lookup.
Fields
private Dictionary<ThumbnailKey, ThumbnailInfo> m_CacheData
Holds the cached thumbnails keyed by ThumbnailKey. Each value is a ThumbnailInfo describing the atlas frame and region for a thumbnail. Populated by LoadAtlas and pruned by UnloadAtlas.
ThumbnailInfo fields (nested public class, used as cached value):
-
public object baseObjectRef
Optional reference to the original object the thumbnail is associated with (cleared on Dispose). -
public Camera camera
Optional Camera reference associated with this thumbnail (not actively used by the cache for rendering). -
public AtlasFrame atlasFrame
Reference to the loaded atlas frame that contains the thumbnail image. -
public Rect region
Region within the atlas frame for the thumbnail (entry.region). -
public Status status
Thumbnail status enum value: Ready, Pending, Unavailable, Refresh. Used by UI logic to track whether thumbnail rendering or update is required.
Properties
ThumbnailKey (nested readonly struct) public properties:
-
public string name { get; }
Name of the atlas entry (used as part of the cache key). -
public int width { get; }
Width of the thumbnail (part of the cache key). -
public int height { get; }
Height of the thumbnail (part of the cache key).
Constructors
public ThumbnailCache()
Initializes the internal dictionary (m_CacheData = new Dictionary()). Call Initialize() to subscribe to atlas asset change events.
Methods
private void OnAtlasAssetChanged(AssetChangedEventArgs args)
Event handler subscribed to AssetDatabase.global.onAssetDatabaseChanged for AtlasAsset changes. Behavior:- If change == ChangeType.BulkAssetsChange: clears cache and reloads all atlas assets from AssetDatabase.global.
- If change == ChangeType.AssetAdded or AssetUpdated: calls LoadAtlas for the changed AtlasAsset.
-
If change == ChangeType.AssetDeleted: calls UnloadAtlas for the deleted AtlasAsset. Catches and logs exceptions using UnityEngine.Debug.LogException.
-
private void LoadAtlas(AtlasAsset asset)
Loads the atlas asset (asset.Load()) and iterates its entries. For each AtlasFrame.Entry it creates a ThumbnailKey using entry.name and entry.region size, and adds a new ThumbnailInfo with atlasFrame, region and status = Status.Ready. Exceptions are caught and logged. -
private void UnloadAtlas(AtlasAsset asset)
Safely unloads an atlas asset and removes matching entries from the cache. Uses a using(asset) block, calls asset.Load() to obtain the frame, iterates entries and removes corresponding keys from m_CacheData, then calls asset.Unload(). Exceptions are caught and logged. -
public void Initialize()
Subscribes OnAtlasAssetChanged to AssetDatabase.global.onAssetDatabaseChanged for AtlasAsset changes. This sets up the cache to keep in sync with asset changes. -
public ThumbnailInfo GetCachedThumbnail(ThumbnailKey key)
Attempts to return a cached ThumbnailInfo for the provided ThumbnailKey. Returns null if none is found. -
public void Refresh()
Marks all cached ThumbnailInfo.status values as Status.Pending, signaling that thumbnails should be (re)generated or refreshed by UI code. -
public ThumbnailInfo GetThumbnail(object obj, int width, int height, Camera camera = null)
Helper lookup that, if obj is a UnityEngine.Object, builds a key from obj.name and the width/height and returns the cached ThumbnailInfo (or null). Does not automatically create or render thumbnails — only retrieves cache entries. -
public void Update()
Empty method stub. Present in class but contains no logic. May be intended for per-frame maintenance in other versions. -
public void Dispose()
Clears object references stored in the cached ThumbnailInfo objects by setting baseObjectRef to null for each entry. Does not clear the dictionary itself. Intended to release references for GC.
Nested type method highlights:
ThumbnailInfo: - Equality operators and Equals compare baseObjectRef, camera, atlasFrame and region. - GetHashCode returns hash of tuple (baseObjectRef, camera, atlasFrame, region.GetHashCode()).
ThumbnailKey: - Readonly struct that implements equality by name, width and height. - GetHashCode uses tuple (name, width, height). - ToString returns $"{name}_{width}x{height}" — convenient for logging.
Usage Example
// Create and initialize the cache (subscribe to atlas asset changes)
var thumbnailCache = new Game.UI.Thumbnails.ThumbnailCache();
thumbnailCache.Initialize();
// Lookup by explicit key
var key = new Game.UI.Thumbnails.ThumbnailCache.ThumbnailKey("my_icon", 64, 64);
var info = thumbnailCache.GetCachedThumbnail(key);
if (info != null && info.status == Game.UI.Thumbnails.ThumbnailCache.Status.Ready)
{
// info.atlasFrame and info.region identify the texture/UV region to display
}
// Or lookup using a UnityEngine.Object
UnityEngine.Object someObject = ...;
var found = thumbnailCache.GetThumbnail(someObject, 64, 64);
if (found != null) { /* use found.atlasFrame / found.region */ }
// Mark all cached thumbnails as pending refresh
thumbnailCache.Refresh();
// When no longer needed
thumbnailCache.Dispose();
Notes and tips: - The cache stores atlas frame and region metadata but does not itself render thumbnails. UI code must use the stored AtlasFrame/region (and optional camera) to display or generate images. - Initialize must be called to start listening for atlas changes; Dispose should be used to clear references if the cache is being torn down. - LoadAtlas/UnloadAtlas handle exceptions and therefore are resilient to asset problems, but they operate on the asset database and can be relatively expensive when reloading many atlas assets.