private void SetImageToThumbnail() { if (file == null || hasRequestedThumbnail) { return; } LRUCache <string, Sprite> thumbnailCache = null; thumbnailCache = CacheManager.Instance.GetCache <string, Sprite>(THUMBNAIL_CACHE_NAME, THUMBNAIL_CACHE_CAPACITY); if (thumbnailCache != null) { Sprite thumbnail = thumbnailCache.Get(file.FullName); if (thumbnail != null) { SetThumbnailFromSprite(thumbnail); hasRequestedThumbnail = true; return; } } if (IsInTransition) { // There was no cached thumbnail so try to load it asynchronously. MediaHelpers.GetThumbnail(file.FullName, OnThumbnailLoaded); hasRequestedThumbnail = true; } }
private void OnThumbnailLoaded(string filePath, byte[] thumbnailBytes, bool cancelled) { if (file == null) { return; } if (file.FullName != filePath) { return; } if (thumbnailBytes == null) { if (cancelled) { MediaHelpers.GetThumbnail(file.FullName, OnThumbnailLoaded); } return; } Texture2D thumbnail = new Texture2D(2, 2); thumbnail.LoadImage(thumbnailBytes); Vector2 size = imageTransform.sizeDelta; float aspectRatio = size.x / size.y; float width = thumbnail.width; float height = thumbnail.height; height = width / aspectRatio; if (height > thumbnail.height) { height = thumbnail.height; width = height * aspectRatio; } float rectX = (thumbnail.width * 0.5f) - (width * 0.5f); float rectY = (thumbnail.height * 0.5f) - (height * 0.5f); Rect rect = new Rect(rectX, rectY, width, height); Sprite thumbnailSprite = Sprite.Create(thumbnail, rect, new Vector2(0.5f, 0.5f)); LRUCache <string, Sprite> thumbnailCache = null; thumbnailCache = CacheManager.Instance.GetCache <string, Sprite>(THUMBNAIL_CACHE_NAME, THUMBNAIL_CACHE_CAPACITY); thumbnailCache.Set(file.FullName, thumbnailSprite); SetThumbnailFromSprite(thumbnailSprite); }