还在下载的时候从 UnityWebRequest 获取数据?

Obtain data from UnityWebRequest while still downloading?

我有这个代码可以进行 REST 调用:

public IEnumerator GetCooroutine(string route)
{
    string finalURL = URL + route;
    UnityWebRequest www = UnityWebRequest.Get(finalURL);
    yield return www.SendWebRequest();

    if (www.isNetworkError || www.isHttpError) {
        Debug.Log(www.error);
    }
    else {
        Debug.Log("GET succesfull. Response: " + www.downloadHandler.text);
    }
}

我需要在他们接收时访问请求的数据或正文,并将其用于其他内容。我不想等到它接收完再访问它们。

如果我使用 yield,代码会一直等到 send 完成它的执行,而我不希望这样。

可以使用 UnityWebRequest 来完成,但您必须执行额外的工作。秘诀是使用 DownloadHandlerScriptUnityWebRequest

创建一个单独的 class 并使其派生自 DownloadHandlerScript 而不是 MonoBehaviour。在下面的示例中,我将其称为 CustomWebRequest。覆盖其中的 bool ReceiveData(byte[] data, int dataLength)void CompleteContent()void ReceiveContentLength(int contentLength) 函数。

这里是 CustomWebRequest class 的例子:

public class CustomWebRequest : DownloadHandlerScript
{
    // Standard scripted download handler - will allocate memory on each ReceiveData callback
    public CustomWebRequest()
        : base()
    {
    }

    // Pre-allocated scripted download handler
    // Will reuse the supplied byte array to deliver data.
    // Eliminates memory allocation.
    public CustomWebRequest(byte[] buffer)
        : base(buffer)
    {
    }

    // Required by DownloadHandler base class. Called when you address the 'bytes' property.
    protected override byte[] GetData() { return null; }

    // Called once per frame when data has been received from the network.
    protected override bool ReceiveData(byte[] byteFromServer, int dataLength)
    {
        if (byteFromServer == null || byteFromServer.Length < 1)
        {
            Debug.Log("CustomWebRequest :: ReceiveData - received a null/empty buffer");
            return false;
        }

        //Do something with or Process byteFromServer here


        return true;
    }

    // Called when all data has been received from the server and delivered via ReceiveData
    protected override void CompleteContent()
    {
        Debug.Log("CustomWebRequest :: CompleteContent - DOWNLOAD COMPLETE!");
    }

    // Called when a Content-Length header is received from the server.
    protected override void ReceiveContentLength(int contentLength)
    {
        Debug.Log(string.Format("CustomWebRequest :: ReceiveContentLength - length {0}", contentLength));
    }
}

要使用它开始下载,只需创建 UnityWebRequest 的新实例并在调用 SendWebRequest 函数之前使用 CustomWebRequest 作为 downloadHandler。这不需要协程,您甚至不必产生任何东西。这是如何做到的:

UnityWebRequest webRequest;
//Pre-allocate memory so that this is not done each time data is received
byte[] bytes = new byte[2000];

void Start()
{
    string url = "http://yourUrl/mjpg/video.mjpg";
    webRequest = new UnityWebRequest(url);
    webRequest.downloadHandler = new CustomWebRequest(bytes);
    webRequest.SendWebRequest();
}

就是这样。

  • 当Unity第一次发出请求时,ReceiveContentLength(int contentLength)被调用。您可以使用 contentLength 参数来确定您将从该请求接收的字节总数。

  • 每次接收到新数据时都会调用ReceiveData函数 通过该函数,您可以从 byteFromServer 参数及其来自 dataLength 的长度 参数.

  • 当Unity完成接收数据后,CompleteContent函数是 打电话。