迭代多个UnityWebRequests

问题描述 投票:0回答:1

我一直在尝试进行一些API通信。我使用UnityWebRequests是因为我想作为WebGL进行构建(我尝试了System.Net.WebRequest,它在游戏模式下工作,但与WebGL不兼容)。这可能只是协程的问题,但我将以半伪的方式向您展示到目前为止我所拥有的以及我的问题是:

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using System.Collections.Generic;

public class scking : MonoBehaviour
{

    string tempText;
    string getUri = "https://api.opensensors.com/getProjectMessages";
    string jwtToken = "someToken";

    private void Start()
    {
        pullAllData();
    }

    IEnumerator GetDataRequest(string currentDateString, string nextDateString, string sensorID)
    {
        string requestParam = "myparameters: " + nextDateString + sensorID; // simplified dummy parameters

        using (UnityWebRequest webRequest = UnityWebRequest.Get(requestParam))
        {
            webRequest.SetRequestHeader("Authorization", "Bearer " + jwtToken);
            webRequest.SetRequestHeader("Content-Type", "application/json");

            yield return webRequest.SendWebRequest();

            // from unity api example
            string[] pages = getUri.Split('/');
            int page = pages.Length - 1;

            if (webRequest.isNetworkError)
            {
                Debug.Log(pages[page] + ": Error: " + webRequest.error);
            }
            else
            {
                Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
            }

            // getting the data i pulled out of the coroutine for further manipulation
            this.tempText = webRequest.downloadHandler.text;

            // show whats been pulled
            print(this.tempText);
        }
    }

    void pullAllData()
    {
        int allDaysCount = 10;
        List<int> sensorIds; // initialized with some ids

        for(int i = 0; i < allDaysCount - 1; i++)
        {
            for(int j = 0; j < sensorIds.Count; j++)
            {
                StartCoroutine(GetDataRequest(i, i + 1, sensorIds[j]));

                // show whats been pulled
                print(this.tempText);

                // parse json string this.tempText etc.
            }
        }

    }
}

输出是(按时间排序)

来自pullAddData中的打印:空

然后是协程中打印的下一个:jsonItem

基本上,协程花费的时间太长,太迟了,我的循环无法继续,而且因为我得到了null,所以我当然无法解析或处理我的数据。还是我的整个工作有缺陷,在这种情况下,如何正确地做到这一点?

非常感谢您对此提供的帮助。菲利普(亲切)

c# api unity3d webrequest unitywebrequest
1个回答
0
投票

如果您想等他们平行签出我对Wait for all requests to continue的回答


[如果您想仍然等待它们一对一,则可以将它们包装到一个更大的协程中:

void pullAllData()
{
    int allDaysCount = 10;
    List<int> sensorIds; // initialized with some ids

    // start the "parent" Coroutine
    StartCoroutine(GetAllData(allDaysCount, sensorIds));
}

IEnumerator GetAllData(int allDaysCount, List<int> sensorIds)
{
    for(int i = 0; i < allDaysCount - 1; i++)
    {
        for(int j = 0; j < sensorIds.Count; j++)
        {
            // Simply run and wait for this IEnumerator
            // in order to execute them one at a time
            yield return GetDataRequest(i, i + 1, sensorIds[j]);

            // show whats been pulled
            print(this.tempText);

            // parse json string this.tempText etc.
        }
    }

    // Maybe do something when all requests are finished and handled
}

IEnumerator GetDataRequest(string currentDateString, string nextDateString, string sensorID)
{
    string requestParam = "myparameters: " + nextDateString + sensorID; // simplified dummy parameters

    using (UnityWebRequest webRequest = UnityWebRequest.Get(requestParam))
    {
        webRequest.SetRequestHeader("Authorization", "Bearer " + jwtToken);
        webRequest.SetRequestHeader("Content-Type", "application/json");

        yield return webRequest.SendWebRequest();

        // from unity api example
        string[] pages = getUri.Split('/');
        int page = pages.Length - 1;

        if (webRequest.isNetworkError)
        {
            Debug.Log(pages[page] + ": Error: " + webRequest.error);
        }
        else
        {
            Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
        }

        // getting the data i pulled out of the coroutine for further manipulation
        this.tempText = webRequest.downloadHandler.text;

        // show whats been pulled
        print(this.tempText);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.