协程中的值为真后无法返回字符串

Sea*_*aMe 2 c# return coroutine unity-game-engine

我在运行这个协程时遇到了问题。它一直吐出一个错误,说它不能将 WaitUntil 转换为字符串,当我将 WaitUntil 添加到返回类型时,它又吐出另一个说它无法返回。我已经尝试研究了几个小时,但无济于事。这是代码的片段:

     public string OpenSaveDialog(string Title, string OpenLocation, string[] AllowedExtentions)
     {
         OpenBtn.GetComponentInChildren<TMPro.TMP_Text>().text = "Save";
         TitleText.text = Title;
         AllowFileNameTyping = true;
         MultiSelect = false;

         LoadIntoExplorer(PathInput, AllowedExtentions);

         return StartCoroutine(WaitForFinish()); // Error here: Cannot implicitly convert type 'UnityEngine.Coroutine' to 'string'
     }

     IEnumerator<string> WaitForFinish()
     {
         yield return new WaitUntil(() => Done == true); // Error here: Cannot implicitly convert type 'UnityEngine.WaitUntil' to 'string'
         yield return FilePathsSelected[0];
     }
Run Code Online (Sandbox Code Playgroud)

Men*_*yus 8

您不能从协程返回值,您的选项是进行回调,类范围变量,指示协程的值,自定义类,具有 IsDone & value 或 result 属性,您也不能使用 ref、in 或 out关键字也是因为迭代器不能使用它们:/所以这不起作用:

public IEnumerator WaitForFinnish(ref string value)
{
    yield return new WaitUntil(() => true);
    value = "value";
}
Run Code Online (Sandbox Code Playgroud)

所以在你的情况下,我会做这样的事情:

string filePath = string.Empty;

public void OpenSaveDialog(string Title, string OpenLocation, string[] AllowedExtentions)
{
     OpenBtn.GetComponentInChildren<TMPro.TMP_Text>().text = "Save";
     TitleText.text = Title;
     AllowFileNameTyping = true;
     MultiSelect = false;

     LoadIntoExplorer(PathInput, AllowedExtentions);

     StartCoroutine(WaitForFinish());
 }

 IEnumerator WaitForFinish()
 {
     yield return new WaitUntil(() => Done); // Also don't do bool == true or false,
                                             // it will trigger most of the programmers :D
     filePath = FilePathsSelected[0];
 }
Run Code Online (Sandbox Code Playgroud)