zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Unity/Assets/Model/Module/UnityWebRequest/UnityWebRequestAsync.cs

105 lines
1.8 KiB
C#

using System;
using UnityEngine.Networking;
namespace ET
{
public class UnityWebRequestUpdateSystem : UpdateSystem<UnityWebRequestAsync>
{
public override void Update(UnityWebRequestAsync self)
{
self.Update();
}
}
public class UnityWebRequestAsync : Entity
{
public class AcceptAllCertificate: CertificateHandler
{
protected override bool ValidateCertificate(byte[] certificateData)
{
return true;
}
}
public static AcceptAllCertificate certificateHandler = new AcceptAllCertificate();
public UnityWebRequest Request;
public bool isCancel;
public ETTaskCompletionSource tcs;
public override void Dispose()
{
if (this.IsDisposed)
{
return;
}
base.Dispose();
this.Request?.Dispose();
this.Request = null;
this.isCancel = false;
}
public float Progress
{
get
{
if (this.Request == null)
{
return 0;
}
return this.Request.downloadProgress;
}
}
public ulong ByteDownloaded
{
get
{
if (this.Request == null)
{
return 0;
}
return this.Request.downloadedBytes;
}
}
public void Update()
{
if (this.isCancel)
{
this.tcs.SetException(new Exception($"request error: {this.Request.error}"));
return;
}
if (!this.Request.isDone)
{
return;
}
if (!string.IsNullOrEmpty(this.Request.error))
{
this.tcs.SetException(new Exception($"request error: {this.Request.error}"));
return;
}
this.tcs.SetResult();
}
public async ETTask DownloadAsync(string url)
{
this.tcs = new ETTaskCompletionSource();
url = url.Replace(" ", "%20");
this.Request = UnityWebRequest.Get(url);
this.Request.certificateHandler = certificateHandler;
this.Request.SendWebRequest();
await this.tcs.Task;
}
}
}