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