forked from egametang/ET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnityWebRequestAsync.cs
More file actions
104 lines (86 loc) · 1.75 KB
/
Copy pathUnityWebRequestAsync.cs
File metadata and controls
104 lines (86 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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 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;
}
}
}