| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using UnityEngine.Networking; |
| | 5 | |
|
| | 6 | | namespace DCL |
| | 7 | | { |
| | 8 | | public static class WrappedTextureUtils |
| | 9 | | { |
| | 10 | | public static IEnumerator GetHeader(string url, string headerField, Action<string> OnSuccess, Action<string> OnF |
| | 11 | | { |
| 12 | 12 | | using (var headReq = UnityWebRequest.Head(url)) |
| | 13 | | { |
| 12 | 14 | | yield return headReq.SendWebRequest(); |
| | 15 | |
|
| 12 | 16 | | if (headReq.WebRequestSucceded()) |
| | 17 | | { |
| 6 | 18 | | OnSuccess?.Invoke(headReq.GetResponseHeader(headerField)); |
| 6 | 19 | | } |
| | 20 | | else |
| | 21 | | { |
| 6 | 22 | | OnFail?.Invoke(headReq.error); |
| | 23 | | } |
| 12 | 24 | | } |
| 12 | 25 | | } |
| | 26 | |
|
| | 27 | | public static IEnumerator Fetch(string url, Action<IPromiseLike_TextureAsset> OnSuccess, Action OnFail = null) |
| | 28 | | { |
| 12 | 29 | | string contentType = null; |
| 18 | 30 | | yield return GetHeader(url, "Content-Type", type => contentType = type, null); |
| 12 | 31 | | yield return Fetch(contentType, url, OnSuccess, OnFail); |
| 12 | 32 | | } |
| | 33 | |
|
| | 34 | | public static IEnumerator Fetch(string contentType, string url, Action<IPromiseLike_TextureAsset> OnSuccess, Act |
| | 35 | | { |
| 12 | 36 | | if (contentType == "image/gif") |
| | 37 | | { |
| 0 | 38 | | AssetPromise_Gif gifPromise = new AssetPromise_Gif(url); |
| 0 | 39 | | gifPromise.OnSuccessEvent += texture => { OnSuccess?.Invoke(new PromiseLike_Gif(gifPromise)); }; |
| 0 | 40 | | gifPromise.OnFailEvent += (x) => OnFail?.Invoke(); |
| | 41 | |
|
| 0 | 42 | | AssetPromiseKeeper_Gif.i.Keep(gifPromise); |
| | 43 | |
|
| 0 | 44 | | yield return gifPromise; |
| 0 | 45 | | } |
| | 46 | | else |
| | 47 | | { |
| 12 | 48 | | AssetPromise_Texture texturePromise = new AssetPromise_Texture(url, storeTexAsNonReadable: false); |
| 24 | 49 | | texturePromise.OnSuccessEvent += texture => { OnSuccess?.Invoke(new PromiseLike_Texture(texturePromise)) |
| 18 | 50 | | texturePromise.OnFailEvent += (x) => OnFail?.Invoke(); |
| | 51 | |
|
| 12 | 52 | | AssetPromiseKeeper_Texture.i.Keep(texturePromise); |
| | 53 | |
|
| 12 | 54 | | yield return texturePromise; |
| | 55 | | } |
| 12 | 56 | | } |
| | 57 | | } |
| | 58 | | } |