官方原文地址:https://docs.unity3d.com/Manual/UnityWebRequest.html
UnityWebRequest
UnityWebRequest provides a modular system for composing HTTP requests and handling HTTP responses. The primary goal of the UnityWebRequest system is to allow Unity games to interact with web browser back-ends. It also supports high-demand features such as chunked HTTP requests, streaming POST/PUT operations, and full control over HTTP headers and verbs.
·UnityWebRequest 提供了一个发起HTTP请求和处理HTTP响应的系统模块。UnityWebRequest系统的主要目标是允许Unity游戏与web浏览器后端进行交互。它也支持一些高需求功能,比如分段HTTP请求,流式POST/PUT操作,和控制HTTP头部和HTTP verbs.
The system consists of two layers:
- A High-Level API (HLAPI) wraps the Low-Level API and provides a convenient interface for performing common operations
- A Low-Level API (LLAPI) provides maximum flexibility for more advanced users
·系统由两层组成:
1、高级API(HLAPI)封装了低级API,提供方便的接口去实现基本操作。
2、低级API(LLAPI)提供了最大的灵活性给高级用户。
Supported platforms
The UnityWebRequest system supports most Unity platforms:
- All versions of the Editor and Standalone players
- WebGL
- Mobile platforms: iOS, Android
- Universal Windows Platform
- PS4 and PSVita
- XboxOne
- Nintendo Switch
支持平台
UnityWebRequest 系统支持大多数unity平台:
·支持所有版本Editor和电脑版用户
·WebGL
·IOS,Android平台
·微软UWP
·PS4 and PSVita
·XboxOne
·任天堂switch
Architecture
The UnityWebRequest ecosystem breaks down an HTTP transaction into three distinct operations:
- Supplying data to the server
- Receiving data from the server
- HTTP flow control (for example, redirects and error handling)
架构
UnityWebRequest系统把一个HTTP事务分为三个独立操作:
·提供数据给服务器
·从服务器接受数据
·HTTP流控制(比如,重定向和错误处理)
To provide a better interface for advanced users, these operations are each governed by their own objects:
- An
UploadHandler
object handles transmission of data to the server - A
DownloadHandler
object handles receipt, buffering and postprocessing of data received from the server - A
UnityWebRequest
object manages the other two objects, and also handles HTTP flow control. This object is where custom headers and URLs are defined, and where error and redirect information is stored.
为了提供一个更好的接口给高级用户,以下操作分别被他们自己的对象实例管理:
·一个UploadHandler
对象控制传输数据给服务器
·一个DownloadHandler
对象控制接收,bu来自服务器的buffering 和postprocessing数据
·一个UnityWebRequest
对象管理其他两个对象,也控制HTTP flow控制。这个对象
For any HTTP transaction, the normal code flow is:
- Create a Web Request object
- Configure the Web Request object
- Set custom headers
- Set HTTP verb (such as GET, POST, HEAD - custom verbs are permitted on all platforms except for Android)
- Set URL
- (Optional) Create an Upload Handler and attach it to the Web Request
- Provide data to be uploaded
- Provide HTTP form to be uploaded
- (Optional) Create a Download Handler and attach it to the Web Request
- Send the Web Request
- If inside a coroutine, you may Yield the result of the
Send()
call to wait for the request to complete
- If inside a coroutine, you may Yield the result of the
- (Optional) Read received data from the Download Handler
- (Optional) Read error information, HTTP status code and response headers from the UnityWebRequest object