Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# foreach循环unity3dc中的协程#_C#_Unity3d_Upload_Ienumerator - Fatal编程技术网

C# foreach循环unity3dc中的协程#

C# foreach循环unity3dc中的协程#,c#,unity3d,upload,ienumerator,C#,Unity3d,Upload,Ienumerator,各位! 我正在尝试使用foreach上载多个文件。到目前为止,我成功上传了所有内容,但我想先完成协同程序,然后再继续for循环中的下一项。我将制作一个进度条。出于某种原因,协同程序都是一个一个地执行,而不是一个接一个地执行 这是我的密码: public void CallUploadFile() //Called by button { StartCoroutine(UploadScene()); } IEnumerator UploadScen

各位!

我正在尝试使用foreach上载多个文件。到目前为止,我成功上传了所有内容,但我想先完成协同程序,然后再继续for循环中的下一项。我将制作一个进度条。出于某种原因,协同程序都是一个一个地执行,而不是一个接一个地执行

这是我的密码:

    public void CallUploadFile() //Called by button
    {
        StartCoroutine(UploadScene());
    }

    IEnumerator UploadScene()
    {
        foreach(string str in item_list)
        {
            string item_path = str;
            yield return new StartCoroutine(StartUpload (item_path));
        }
    }

    IEnumerator StartUpload(string item_path)
    {
         byte[] image_bytes;
         image_bytes = File.ReadAllBytes(item_path);

         // upload using WWWForm;
        WWWForm form = new WWWForm ();
        form.AddBinaryData ("scene[image]", image_bytes);
        using (UnityWebRequest www = UnityWebRequest.Post("somelink.com/upload", form))
        {
             yield return www.Send();
            while (!www.isDone)
            {
               Debug.Log(www.progress);
               yield return null
            }
            Debug.Log("Success!");
        }
    }

我已经在各种论坛上读过关于这个主题的文章,这段代码应该可以工作,但由于某些原因,它不能正常工作。任何提示都将不胜感激。

使用下面的代码:这将使所有代码在返回null之前完成,这符合您的需要

IEnumerator UploadScene()
{
    foreach(string str in item_list)
    {
        string item_path = str;
        StartUpload (item_path));
    }
    yield return null;   //returns until work is done.
}

void StartUpload(string item_path)
{
     byte[] image_bytes;
     image_bytes = File.ReadAllBytes(item_path);

     // upload using WWWForm;
}

外部枚举可以生成内部枚举,如

IEnumerator UploadScene()
{
    foreach(string str in item_list)
    {
        string item_path = str;
        yield return StartUpload (item_path);
    }
}
编辑:我在文档中找不到发送方法,

你确定它会返回一条屈服指令吗?您可能可以跳过这一点,只需在未完成代码时使用yield返回null


edit2:存在一个异步SendWebRequest方法。其返回值与协同程序不兼容

我认为您的代码运行良好,但您打印错误

IEnumerator UploadScene()
{
    foreach(string str in item_list)
    {
        string item_path = str;
        yield return new StartCoroutine(StartUpload (item_path));
    }
}
一旦上一个协同过程结束,您就可以正确地开始每个协同过程。 我想说目前为止一切都很好

IEnumerator StartUpload(string item_path)
{
     // Code
    using (UnityWebRequest www = UnityWebRequest.Post("somelink.com/upload", form))
    {
         yield return www.Send();
        while (!www.isDone)
        {
           Debug.Log(www.progress);
           yield return null
        }
        Debug.Log("Success!");
    }
}
您正在接受发送请求,这将在此时以某种方式“暂停”协同路由,直到下载完全完成。 下一步,如果没有完成,打印,但直到下载结束

所以,我希望你们在印刷方面取得唯一的成功

您需要的是在循环内屈服:

        www.Send();
        while (!www.isDone)
        {
           Debug.Log(www.progress);
           yield return null
        }
这将调用发送请求并将继续。但是现在它将在while循环util does中产生,因此它将一个接一个地打印进度


请记住,这将使您的下载时间更长。您可以在同一个循环中触发它们,然后Unity将启动不同的线程(而不是协程,正确的线程),这样您就可以并行下载多个线程。

我不这么认为。foreach循环将启动协同路由。然后,当协同路由运行时,您的
上传场景
协同路由将只等待一帧。这是期望的结果吗?为什么不呢?:)OP希望
UploadScene
corroutine等待所有
startupboad
corroutine结束。您是否在
startupboad
corroutine中使用
yield return
?你能在
StartUpload
中提供完整的代码吗?@Hellium我编辑了我的问题,并将我的代码添加到开始上传方法中。我更新了我的答案。我不太熟悉Unity中发送的web请求。在中,他们使用
返回www.SendWebRequest()并且他们不使用
www.isDone
+
产生返回空值
Ahh是的,我将代码更改为www.SendWebRequest();所以这就是我不能发送任何东西的原因。我将修改我的上传代码。而且你的foreach循环代码是错误的,请参阅我的原始答案