| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- 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 static AcceptAllCertificate certificateHandler = new AcceptAllCertificate();
-
- public UnityWebRequest Request;
- public bool isCancel;
- public ETTask 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 = ETTask.Create(true);
-
- url = url.Replace(" ", "%20");
- this.Request = UnityWebRequest.Get(url);
- this.Request.certificateHandler = certificateHandler;
- this.Request.SendWebRequest();
-
- await this.tcs;
- }
- }
- }
|