| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using System; |
| | 3 | | using System.Threading; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.Experimental.Rendering; |
| | 6 | | using UnityEngine.Networking; |
| | 7 | |
|
| | 8 | | namespace DCL.Controllers.LoadingScreenV2 |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// The HintTextureRequestHandler class is responsible for downloading textures for loading screen hints. |
| | 12 | | /// </summary> |
| | 13 | | public class HintTextureRequestHandler : IHintTextureRequestHandler |
| | 14 | | { |
| | 15 | | public async UniTask<Texture2D> DownloadTexture(string url, CancellationToken ctx, int timeout = 2) |
| | 16 | | { |
| 0 | 17 | | Texture2D tex = null; |
| 0 | 18 | | UnityWebRequest www = null; |
| | 19 | |
|
| | 20 | | try |
| | 21 | | { |
| 0 | 22 | | www = UnityWebRequest.Get(url); |
| | 23 | |
|
| 0 | 24 | | www.timeout = timeout; |
| | 25 | |
|
| 0 | 26 | | UnityWebRequestAsyncOperation operation = www.SendWebRequest(); |
| | 27 | |
|
| 0 | 28 | | while (!operation.isDone) |
| | 29 | | { |
| 0 | 30 | | await UniTask.Yield(); |
| | 31 | |
|
| 0 | 32 | | if (ctx.IsCancellationRequested) |
| | 33 | | { |
| 0 | 34 | | Debug.LogWarning("Hint DownloadTexture interrupted"); |
| 0 | 35 | | www.Abort(); |
| 0 | 36 | | return null; |
| | 37 | | } |
| | 38 | | } |
| | 39 | |
|
| 0 | 40 | | if (www.result != UnityWebRequest.Result.Success) { Debug.LogWarning("Hint DownloadTexture Error: " + ww |
| | 41 | | else |
| | 42 | | { |
| 0 | 43 | | tex = new Texture2D(2, 2, GraphicsFormatUtility.GetGraphicsFormat(TextureFormat.RGBA32, false), Text |
| | 44 | | { |
| | 45 | | wrapMode = TextureWrapMode.Clamp, |
| | 46 | | }; |
| | 47 | |
|
| 0 | 48 | | tex.LoadImage(www.downloadHandler.data); |
| 0 | 49 | | tex.Apply(false, false); |
| | 50 | | } |
| 0 | 51 | | } |
| 0 | 52 | | catch (Exception ex) { Debug.LogWarning("DownloadTexture Exception: " + ex.Message); } |
| | 53 | | finally |
| | 54 | | { |
| 0 | 55 | | if (www != null) { www.Dispose(); } |
| | 56 | | } |
| | 57 | |
|
| 0 | 58 | | return tex; |
| 0 | 59 | | } |
| | 60 | | } |
| | 61 | | } |