gpt4 book ai didi

c# - 协程中值为 true 后无法返回字符串

转载 作者:行者123 更新时间:2023-12-02 00:49:51 24 4
gpt4 key购买 nike

我需要运行的协程存在问题。它不断吐出一个错误,说它无法将 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];
}

最佳答案

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

public IEnumerator WaitForFinnish(ref string value)
{
yield return new WaitUntil(() => true);
value = "value";
}

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

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];
}

关于c# - 协程中值为 true 后无法返回字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59421224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com