Unity3D iOS 平台放弃 WWW 采用 UnityWebRequest
https://blog.****.net/ykr168age/article/details/68923331
使用的是 Unity 5.3.4, WWW 再 iOS 上加载资源出现卡死的问题:加载到一定程度卡死,重启APP后又可以跑过去,有些机型上甚至出现下载资源过不去的情况。解决方案,使用 UnityWebRequest 代替 WWW。当然 UnityWebRequest 次版本的 Dispose 有问题,更新到最新版就没问题了。
UnityWebRequest 架构
UnityWebRequest 由三个元素组成。
◾UploadHandler 处理数据 将数据发送到服务器 的对象
◾DownloadHandler 从服务器接收数据 的对象
◾UnityWebRequest 负责 HTTP 通信流量控制来管理上面两个对象的对象。
来说明这些对象之间的关系,如下所示。
基本用法
比较UnityWebRequest 和 WWW 类的基本用法。
GET
www 通过 url 的写法:
-
using UnityEngine;
-
using System.Collections;
-
class MyBehaviour : public MonoBehaviour {
-
void Start() {
-
StartCoroutine(GetText());
-
}
-
IEnumerator GetText() {
-
WWW request = new WWW("http://example.com");
-
yield return request;
-
if (! string .IsNullOrEmpty(request.error)) {
-
Debug.Log(request.error)
-
} else {
-
//
-
if (request.responseHeaders.ContainsKey("STATUS") &&
-
request.responseHeaders["STATUS"] == 200) {
-
//
-
string text = request.text;
-
//
-
byte [] results = request.bytes;
-
}
-
}
-
}
-
}
UnityWebRequest的书写方式
-
using UnityEngine;
-
using System.Collections;
-
using UnityEngine.Experimental.Networking;
-
//
-
// using UnityEngine.Networking;
-
class MyBehaviour : public MonoBehaviour {
-
void Start() {
-
StartCoroutine(GetText());
-
}
-
IEnumerator GetText() {
-
UnityWebRequest request = UnityWebRequest.Get("http://example.com");
-
//
-
// UnityWebRequest request = new UnityWebRequest("http://example.com");
-
//
-
// request.method = UnityWebRequest.kHttpVerbGET;
-
//
-
yield return request.Send();
-
//
-
if (request.isError) {
-
Debug.Log(request.error);
-
} else {
-
if (request.responseCode == 200) {
-
//
-
string text = request.downloadHandler.text;
-
//
-
byte [] results = request.downloadHandler.data;
-
}
-
}
-
}
-
}