| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | namespace DCL.Helpers |
| | 5 | | { |
| | 6 | | internal class TextureLoader : ITextureLoader |
| | 7 | | { |
| | 8 | | private AssetPromise_Texture currentPromise; |
| | 9 | | public event Action<Texture2D> OnSuccess; |
| | 10 | | public event Action<Exception> OnFail; |
| | 11 | |
|
| | 12 | | public Texture2D GetTexture() |
| | 13 | | { |
| 0 | 14 | | return currentPromise.asset.texture; |
| | 15 | | } |
| | 16 | |
|
| | 17 | | public void Load(string uri) |
| | 18 | | { |
| 0 | 19 | | if ( currentPromise != null ) |
| 0 | 20 | | AssetPromiseKeeper_Texture.i.Forget(currentPromise); |
| | 21 | |
|
| 0 | 22 | | currentPromise = new AssetPromise_Texture(uri); |
| | 23 | |
|
| 0 | 24 | | currentPromise.OnSuccessEvent += (x) => |
| | 25 | | { |
| 0 | 26 | | OnSuccess?.Invoke(x.texture); |
| 0 | 27 | | }; |
| | 28 | |
|
| 0 | 29 | | currentPromise.OnFailEvent += (x, e) => |
| | 30 | | { |
| 0 | 31 | | OnFail?.Invoke(e); |
| 0 | 32 | | Debug.LogError($"Texture loading failed! {uri} {e.Message}"); |
| 0 | 33 | | }; |
| | 34 | |
|
| 0 | 35 | | AssetPromiseKeeper_Texture.i.Keep(currentPromise); |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | public void Unload() |
| | 39 | | { |
| 210 | 40 | | if ( currentPromise != null ) |
| 0 | 41 | | AssetPromiseKeeper_Texture.i.Forget(currentPromise); |
| 210 | 42 | | } |
| | 43 | | } |
| | 44 | | } |