设定 UnityWebRequest 在timeout
中指定秒数后尝试中止。
当超时发生时 error 返回“请求超时”。当timeout
被设定为0
时将不应用超时,且该属性默认为0
。
注意:设定的超时可能适用于 Android 上的每个 URL 重定向,这会导致更长的响应时间。
using UnityEngine; using System.Collections; using UnityEngine.Networking;
// Ask the website to deliver an image that is very large. // Set the download to take more than 1 second. This causes // the "request timeout" error message.
public class ExampleScript : MonoBehaviour { void Start() { StartCoroutine(GetText()); }
IEnumerator GetText() { using UnityWebRequest www = UnityWebRequest.Get("https://my-website.com/verylargeimage.jpg");
// wait up to one second to download the image www.timeout = 1; yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success) { Debug.LogError(www.error); } else { Debug.Log("image arrived"); } } }