Skip to content

Commit

Permalink
use UniTask instead of coroutine
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshidan committed Feb 7, 2022
1 parent 89d9201 commit c1f8fba
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/N0vaMacConfig/Assets/Scripts/ListController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ async void Start()
async UniTask<IDictionary<string, Metadata>> DownloadList(string mappingPath)
{
var pairs = new Dictionary<string, Metadata>();
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('=');
Expand Down Expand Up @@ -122,7 +121,7 @@ await DownloadVideo($"{baseUrl}game/{metadata.Value.dataName}.ndf", video,
button.enabled = true;
}
}
catch (OperationCanceledException oce)
catch (OperationCanceledException)
{
//ignore console error
}
Expand All @@ -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);
}
Expand All @@ -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.
Expand All @@ -175,9 +173,18 @@ private async UniTask<Texture> 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<DownloadHandler> GetAsync(string url)
{
return await DoGetAsync(UnityWebRequest.Get(url));
}

private async UniTask<DownloadHandler> DoGetAsync(UnityWebRequest request)
{
return (await request.SendWebRequest()
.WithCancellation(this.GetCancellationTokenOnDestroy())).downloadHandler;
}

}

0 comments on commit c1f8fba

Please # to comment.