Version: 2023.2
public int timeout ;

描述

将 UnityWebRequest 设置为在经过 timeout 中的秒数后尝试中止。

如果发生超时,error 将返回“Request timeout”。当 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() { 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.Log(www.error); } else { Debug.Log("image arrived"); } } }