问题描述
我正在使用Unity和C#来构建游戏,当玩家将鼠标悬停在某个项目上时,将调用UnityWebRequest来处理SPARQL查询。我想为查询输出的JSON响应返回一个字符串。我在从协程中获取返回值时遇到麻烦。到目前为止,我还尝试使用“回调”,尽管我不太确定这是如何工作的。协程从webrequest检索数据后,我想操纵JSOn响应。有谁知道如何使它工作或有任何提示吗?谢谢!!
调用协程的脚本
internal class HighlightSelectionResponse : MonoBehaviour,ISelectionResponse
{
public TextMeshProUGUI gameText;
public RESTGet rest;
private static string itemName;
private string URL;
string returnData;
public void OnSelect(Transform selection)
{
// this method only works if a query toggle is checked
if (rest.queryUsage == true)
{
var itemName = selection.name.ToString(); // name of the gameobject
var URL = rest.queryURL + itemName;
Debug.Log(URL);
// START COROUTINE
StartCoroutine(rest.GetData2(URL,(value)=> { returnData = value; } ));
Debug.Log(returnData); <=== returns Null
}
}
带有协同程序的脚本
public IEnumerator GetData2(string uri,System.Action<string> callback)
{
UnityWebRequest webRequest = UnityWebRequest.Get(uri);
// Call/Request website and wait to finish
yield return webRequest.SendWebRequest();
if (webRequest.isNetworkError || webRequest.isHttpError)
{
Debug.Log("No response from website");
}
else
{
//process web result
Debug.Log("Data retrieved!");
callback(webRequest.downloadHandler.text);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)