当前位置:网站首页>Unitywebrequest asynchronous Download
Unitywebrequest asynchronous Download
2022-06-13 00:50:00 【Small fish game development】
UnityWebRequest Asynchronous writing of
using System;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
public class Example : MonoBehaviour
{
/// <summary>
/// Task cancellation source
/// </summary>
CancellationTokenSource ts = new CancellationTokenSource();
void Start()
{
SynchronizationContext.SetSynchronizationContext(SynchronizationContext.Current);
Test();
}
private void OnDestroy()
{
// Cancel access
ts.Cancel();
}
async void Test()
{
string url = "http://www.baidu.com";
try
{
// Update progress
var progress = new Progress<float>(v =>
{
Debug.Log(v);
});
// ts.Token Create cancellation notification
// Access link
var www = await RequestHelper.GetAsync(url, null, progress, ts.Token, 5000);
// If the access is cancelled , Because asynchronism throws AggregateException abnormal , The following statement will not be executed
Debug.Log(www.downloadHandler.text.Length);
}
catch(Exception ex)
{
Debug.LogError(ex);
}
}
}
public class RequestHelper
{
public static async Task<UnityWebRequest> GetAsync(string url, int timeout = 10000)
{
var request = UnityGetRequest.Create(url, null, timeout);
while (!request.isDone)
{
await Task.Delay(16);
}
return request.unityWebRequest;
}
public static async Task<UnityWebRequest> GetAsync(string url, IProgress<float> progress, int timeout = 10000)
{
var request = UnityGetRequest.Create(url, null, timeout);
while (!request.isDone)
{
progress.Report(request.Progress);
await Task.Delay(16);
}
return request.unityWebRequest;
}
public static async Task<UnityWebRequest> GetAsync(string url, IProgress<float> progress, CancellationToken token, int timeout = 10000)
{
var request = UnityGetRequest.Create(url, null, timeout);
while (!request.isDone)
{
if (token.IsCancellationRequested)
{
token.ThrowIfCancellationRequested();
}
progress.Report(request.Progress);
await Task.Delay(16);
}
return request.unityWebRequest;
}
public static async Task<UnityWebRequest> GetAsync(string url, WWWForm formData, IProgress<float> progress, CancellationToken token, int timeout=10000)
{
var request = UnityGetRequest.Create(url, formData, timeout);
while (!request.isDone)
{
if (token.IsCancellationRequested)
{
token.ThrowIfCancellationRequested();
}
progress.Report(request.Progress);
await Task.Delay(16);
}
return request.unityWebRequest;
}
public static async Task<UnityWebRequest> PostAsync(string url, WWWForm formData, IProgress<float> progress, CancellationToken token, int timeout = 10000)
{
var request = UnityPostRequest.Create(url, formData, timeout);
while (!request.isDone)
{
if (token.IsCancellationRequested)
{
token.ThrowIfCancellationRequested();
}
progress.Report(request.Progress);
await Task.Delay(16);
}
return request.unityWebRequest;
}
interface IUnityWebRequest
{
bool isDone {
get; }
float Progress {
get; }
UnityWebRequest unityWebRequest {
get; }
}
class UnityGetRequest : IUnityWebRequest
{
UnityWebRequest request;
bool IUnityWebRequest.isDone => request.isDone;
float IUnityWebRequest.Progress => request.downloadProgress;
UnityWebRequest IUnityWebRequest.unityWebRequest => request;
public static IUnityWebRequest Create(string url, WWWForm formData, int timeout)
{
UnityGetRequest unityGetRequest = new UnityGetRequest();
unityGetRequest.request = UnityWebRequest.Get(url);
unityGetRequest.request.timeout = timeout;
if (formData != null)
{
foreach (var header in formData.headers)
{
unityGetRequest.request.SetRequestHeader(header.Key, header.Value);
}
}
unityGetRequest.request.SendWebRequest();
return unityGetRequest;
}
}
class UnityPostRequest : IUnityWebRequest
{
UnityWebRequest request;
bool IUnityWebRequest.isDone => request.isDone;
float IUnityWebRequest.Progress => request.uploadProgress;
UnityWebRequest IUnityWebRequest.unityWebRequest => request;
public static IUnityWebRequest Create(string url, WWWForm formData, int timeout)
{
UnityPostRequest unityPostRequest = new UnityPostRequest();
unityPostRequest.request = UnityWebRequest.Post(url, formData);
unityPostRequest.request.timeout = timeout;
unityPostRequest.request.SendWebRequest();
return unityPostRequest;
}
}
}
边栏推荐
- 单片机串口中断以及消息收发处理——对接受信息进行判断实现控制
- 深度学习每周期的步数多少合适?
- Summary of openstack installation problems
- 蓝桥杯单片机第七届决赛
- [JS component] calendar
- Target recognition gadget
- How to determine whether T is a value type in a generic type or a reference class- How to determine whether T is a value type or reference class in generic?
- [network protocol] problems and solutions in the use of LwIP
- People and gods are angry. Details of Tangshan "mass beating of women incident"
- [ciscn2019 North China Day2 web1]hack world --buuctf
猜你喜欢

Win10 home vs pro vs enterprise vs enterprise LTSC

Kotlin 协程,job的生命周期

AOF持久化

Canvas random bubbling background
![[network protocol] problems and solutions in the use of LwIP](/img/25/d064a761724936b8f35ee0c779e597.jpg)
[network protocol] problems and solutions in the use of LwIP
![[JS component library] drag sorting component](/img/f9/4090b52da1a5784b834cb7dbbb948c.jpg)
[JS component library] drag sorting component
![[JS component] previous queue prompt](/img/79/9839f68b191b0db490e9bccbeaae1d.jpg)
[JS component] previous queue prompt

Canvas airplane game

How many steps are appropriate for each cycle of deep learning?
![[JS component] create a custom horizontal and vertical scroll bar following the steam style](/img/c0/9ad8c6abf3dbb210f3d8a46d55b110.jpg)
[JS component] create a custom horizontal and vertical scroll bar following the steam style
随机推荐
Androi天氣
三栏简约typecho主题Lanstar/蓝星typecho主题
Aunt learning code sequel: ability to sling a large number of programmers
Penetration test summary
ImportError: cannot import name &#039; get_ ora_ doc&#039; from partially initialized module
Cards are unpredictable
3623. Merge two ordered arrays
[JS component] simulation framework
antdPro - ProTable 实现两个选择框联动效果
Sequence table - find main element
Physical orbit simulation
什么是 Meebits?一个简短的解释
[JS component] browse progress bar
People and gods are angry. Details of Tangshan "mass beating of women incident"
Et5.0 value type generation
pytorch是什么?解释pytorch的基本概念
Andersen Global通过在芬兰和丹麦的合作协议拓展北欧地区业务版图
Blinker FAQs
[virtual machine] notes on virtual machine environment problems
Hard (magnetic) disk (I)