TH1/Unity/Assets/Scripts/Loader/Helper/CoroutineHelper.cs
2025-07-17 18:26:28 +08:00

31 lines
913 B
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using UnityEngine;
using UnityEngine.Networking;
namespace ET
{
public static class CoroutineHelper
{
// 有了这个方法就可以直接await Unity的AsyncOperation了
public static async ETTask GetAwaiter(this AsyncOperation asyncOperation)
{
ETTask task = ETTask.Create(true);
asyncOperation.completed += _ => { task.SetResult(); };
await task;
}
public static async ETTask<string> HttpGet(string link)
{
try
{
UnityWebRequest req = UnityWebRequest.Get(link);
await req.SendWebRequest();
return req.downloadHandler.text;
}
catch (Exception e)
{
throw new Exception($"http request fail: {link.Substring(0,link.IndexOf('?'))}\n{e}");
}
}
}
}