| | 1 | | using DCL; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.UI; |
| | 4 | |
|
| | 5 | | public class PlayerInfoCollectibleItem : MonoBehaviour |
| | 6 | | { |
| | 7 | | [SerializeField] private Image thumbnail; |
| | 8 | |
|
| | 9 | | internal WearableItem collectible; |
| | 10 | | private AssetPromise_Texture thumbnailPromise; |
| | 11 | | private bool finishedLoading = false; |
| | 12 | |
|
| | 13 | | public void Initialize(WearableItem collectible) |
| | 14 | | { |
| 150 | 15 | | this.collectible = collectible; |
| | 16 | |
|
| 150 | 17 | | if (this.collectible == null) |
| 0 | 18 | | return; |
| | 19 | |
|
| 150 | 20 | | if (gameObject.activeInHierarchy) |
| 0 | 21 | | GetThumbnail(); |
| 150 | 22 | | } |
| | 23 | |
|
| | 24 | | private void OnEnable() |
| | 25 | | { |
| 0 | 26 | | if (collectible == null) |
| 0 | 27 | | return; |
| | 28 | |
|
| 0 | 29 | | GetThumbnail(); |
| 0 | 30 | | } |
| | 31 | |
|
| | 32 | | private void OnDisable() |
| | 33 | | { |
| 0 | 34 | | if (collectible != null) |
| | 35 | | { |
| 0 | 36 | | ForgetThumbnail(); |
| | 37 | |
|
| 0 | 38 | | if (thumbnail.sprite != null && finishedLoading) |
| 0 | 39 | | Destroy(thumbnail.sprite); |
| | 40 | | } |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | private void GetThumbnail() |
| | 44 | | { |
| 0 | 45 | | string url = collectible.ComposeThumbnailUrl(); |
| | 46 | | //NOTE(Brian): Get before forget to prevent referenceCount == 0 and asset unload |
| 0 | 47 | | var newThumbnailPromise = ThumbnailsManager.GetThumbnail(url, OnThumbnailReady); |
| 0 | 48 | | ForgetThumbnail(); |
| 0 | 49 | | thumbnailPromise = newThumbnailPromise; |
| 0 | 50 | | } |
| | 51 | |
|
| 0 | 52 | | private void ForgetThumbnail() { ThumbnailsManager.ForgetThumbnail(thumbnailPromise); } |
| | 53 | |
|
| | 54 | | private void OnThumbnailReady(Asset_Texture texture) |
| | 55 | | { |
| | 56 | | // we override the previously stored placeholder image (a referenced asset), we don't destroy it as it |
| | 57 | | // references the asset and it will provoke a "Destroying assets is not permitted to avoid data loss" error |
| 0 | 58 | | thumbnail.sprite = ThumbnailsManager.CreateSpriteFromTexture(texture.texture); |
| | 59 | |
|
| 0 | 60 | | finishedLoading = true; |
| 0 | 61 | | } |
| | 62 | | } |