//$ Copyright 2015-22, Code Respawn Technologies Pvt Ltd - All Rights Reserved $// using UnityEngine; using UnityEditor; using System.Collections.Generic; namespace DungeonArchitect.Editors { /// /// Manages the asset thumbnails to display in the visual nodes /// public class AssetThumbnailCache { Dictionary thumbnails = new Dictionary(); /// /// List of paths which we have alread requested for reimporting. /// This is used to disallow repeated reimport requests if a thumbnail is still not found /// HashSet reimportRequestPaths = new HashSet(); /// /// The texture to display if the thumbnail for an object cannot be created / retrieved /// Texture2D defaultTexture = null; private static AssetThumbnailCache instance = null; /// /// Singleton access /// public static AssetThumbnailCache Instance { get { if (instance == null) { instance = new AssetThumbnailCache(); } return instance; } } private AssetThumbnailCache() { Reset(); AssetPreview.SetPreviewTextureCacheSize(100); } /// /// Clears all the thumbnail from the cache /// public void Reset() { thumbnails.Clear(); reimportRequestPaths.Clear(); defaultTexture = AssetPreview.GetMiniTypeThumbnail(typeof(GameObject)); } /// /// Gets the thumbnail of the specified asset. Tries to retrieve it from the cache, if it was accessed earlier /// /// The asset to get the thumbnail for /// The thumbnail of the asset. If thumbnail cannot be created, returns the defaultTexture instead public Texture2D GetThumb(Object asset) { if (thumbnails.ContainsKey(asset)) { var thumbnail = thumbnails[asset]; if (thumbnail != null) { return thumbnail; } else { thumbnails.Remove(asset); } } var thumb = AssetPreview.GetAssetPreview(asset); thumbnails.Add(asset, thumb); return thumb == null ? defaultTexture : thumb; } // Update is called once per frame public void Update() { } } }