| | 1 | | using System; |
| | 2 | | using DCL; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace NFTShape_Internal |
| | 6 | | { |
| | 7 | | public class NFTAsset_Image : INFTAsset |
| | 8 | | { |
| | 9 | | private const int RESOLUTION_HQ = 1024; |
| 0 | 10 | | public bool isHQ => hqTexture != null; |
| 0 | 11 | | public ITexture previewAsset { get; } |
| 0 | 12 | | public ITexture hqAsset => hqTexture?.asset; |
| | 13 | |
|
| | 14 | | private AssetPromise_Texture hqTexture; |
| | 15 | | public event Action<Texture2D> OnTextureUpdate; |
| | 16 | |
|
| 0 | 17 | | public NFTAsset_Image(Asset_Texture previewTexture) |
| | 18 | | { |
| 0 | 19 | | previewAsset = previewTexture; |
| 0 | 20 | | } |
| | 21 | |
|
| | 22 | | public void Dispose() |
| | 23 | | { |
| 0 | 24 | | if (hqTexture == null) |
| 0 | 25 | | return; |
| | 26 | |
|
| 0 | 27 | | AssetPromiseKeeper_Texture.i.Forget(hqTexture); |
| 0 | 28 | | hqTexture = null; |
| 0 | 29 | | OnTextureUpdate = null; |
| 0 | 30 | | } |
| | 31 | |
|
| | 32 | | public void FetchAndSetHQAsset(string url, Action onSuccess, Action<Exception> onFail) |
| | 33 | | { |
| 0 | 34 | | string finalUrl = $"{url}=s{RESOLUTION_HQ}"; |
| 0 | 35 | | hqTexture = new AssetPromise_Texture($"{finalUrl}"); |
| | 36 | |
|
| 0 | 37 | | hqTexture.OnSuccessEvent += asset => |
| | 38 | | { |
| 0 | 39 | | OnTextureUpdate?.Invoke(asset.texture); |
| 0 | 40 | | onSuccess?.Invoke(); |
| 0 | 41 | | }; |
| 0 | 42 | | hqTexture.OnFailEvent += (asset, error) => |
| | 43 | | { |
| 0 | 44 | | hqTexture = null; |
| 0 | 45 | | onFail?.Invoke(error); |
| 0 | 46 | | }; |
| | 47 | |
|
| 0 | 48 | | AssetPromiseKeeper_Texture.i.Keep(hqTexture); |
| 0 | 49 | | } |
| | 50 | |
|
| | 51 | | public void RestorePreviewAsset() |
| | 52 | | { |
| 0 | 53 | | if (hqTexture != null) |
| | 54 | | { |
| 0 | 55 | | AssetPromiseKeeper_Texture.i.Forget(hqTexture); |
| 0 | 56 | | hqTexture = null; |
| | 57 | | } |
| | 58 | |
|
| 0 | 59 | | OnTextureUpdate?.Invoke(previewAsset.texture); |
| 0 | 60 | | } |
| | 61 | | } |
| | 62 | | } |