From c1f8fba67f8fff9671af788693b856f2eacb2be0 Mon Sep 17 00:00:00 2001 From: yoshidan Date: Mon, 7 Feb 2022 21:06:14 +0900 Subject: [PATCH] use UniTask instead of coroutine --- .../Assets/Scripts/ListController.cs | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/N0vaMacConfig/Assets/Scripts/ListController.cs b/src/N0vaMacConfig/Assets/Scripts/ListController.cs index 57ee92d..7baa1c8 100644 --- a/src/N0vaMacConfig/Assets/Scripts/ListController.cs +++ b/src/N0vaMacConfig/Assets/Scripts/ListController.cs @@ -39,8 +39,7 @@ async void Start() async UniTask> DownloadList(string mappingPath) { var pairs = new Dictionary(); - var mappingInfo = (await UnityWebRequest.Get(mappingPath).SendWebRequest() - .WithCancellation(this.GetCancellationTokenOnDestroy())).downloadHandler.text; + var mappingInfo = (await GetAsync(mappingPath)).text; foreach (var s in mappingInfo.Split('\n')) { var keyValue = s.Split('='); @@ -122,7 +121,7 @@ await DownloadVideo($"{baseUrl}game/{metadata.Value.dataName}.ndf", video, button.enabled = true; } } - catch (OperationCanceledException oce) + catch (OperationCanceledException) { //ignore console error } @@ -140,8 +139,7 @@ private async UniTask DownloadVideo(string url, string path, int chunkCount, Tex text.text = $"Downloading... {i}/{chunkCount}"; var chunkURL = $"{url}_chunk{i}"; Debug.Log($"download from {chunkURL}"); - var data = (await UnityWebRequest.Get(chunkURL).SendWebRequest() - .WithCancellation(this.GetCancellationTokenOnDestroy())).downloadHandler.data; + var data = (await GetAsync(chunkURL)).data; var offset = i == 1 ? 2 :0 ; await writer.WriteAsync(data, offset, data.Length - offset); } @@ -153,7 +151,7 @@ private async UniTask DownloadVideo(string url, string path, int chunkCount, Tex { text.text = "Downloading..."; Debug.Log($"download from {url}"); - var data = (await UnityWebRequest.Get(url).SendWebRequest().WithCancellation(this.GetCancellationTokenOnDestroy())).downloadHandler.data; + var data = (await GetAsync(url)).data; using (var writer = File.OpenWrite($"{path}_tmp")) { // I discovered that ndf is mp4 but first 2 bytes cause collapse. @@ -175,9 +173,18 @@ private async UniTask LoadTexture(string key) url = $"file://{texturePath}"; } Debug.Log(url); - var downloadHandler = - (await UnityWebRequestTexture.GetTexture(url).SendWebRequest().WithCancellation(this.GetCancellationTokenOnDestroy())).downloadHandler as DownloadHandlerTexture; - return downloadHandler.texture; + return (await DoGetAsync(UnityWebRequestTexture.GetTexture(url)) as DownloadHandlerTexture).texture; + } + + private async UniTask GetAsync(string url) + { + return await DoGetAsync(UnityWebRequest.Get(url)); + } + + private async UniTask DoGetAsync(UnityWebRequest request) + { + return (await request.SendWebRequest() + .WithCancellation(this.GetCancellationTokenOnDestroy())).downloadHandler; } }