Unity UnityWebRequest不返回值

时间:2018-07-12 13:50:27

标签: c# unity3d httpwebrequest coroutine ienumerator

我有一些代码可以从数据库中获取一些数据。在我的游戏中使用。 我已经设置了一个协程,以便与WWW框架统一获取此数据。 但是,当我运行代码时,数据永远不会登录到收益返回函数中。为什么会这样呢?请参阅下面的代码以获取无法使用的要点:

match_data <- stringdist_left_join(db_a, db_b,
              by = "zipstreet",
              ignore_case = TRUE,
              method = "jaccard",
              max_dist = 1,
              distance_col = "dist"
) %>%
  Group_by(zipstreet.x)

我想要的是using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; public class Main : MonoBehaviour { void Start () { Debug.Log("run ma routine"); StartCoroutine(GetText("http://localhost/artez/praktijk_opdracht/interface_v4/app/php/get_fashion.php", (result) =>{ Debug.Log(result); // this log function is never logging a value.. Why is this? })); } void Update () { } IEnumerator GetText(string url, Action<string> result) { UnityWebRequest www = UnityWebRequest.Get(url); yield return www.SendWebRequest(); if(www.isNetworkError || www.isHttpError) { Debug.Log(www.error); } else { Debug.Log(www.downloadHandler.data); // this log is returning the requested data. } } } 而不是StartCoroutine()内部的debug.log记录数据

如果需要更多说明,我很乐意提供。

1 个答案:

答案 0 :(得分:2)

Action函数内完成UnityWebRequest请求之后,您必须调用结果GetText

if (result != null)
    result(www.downloadHandler.text);

新的GetText函数:

IEnumerator GetText(string url, Action<string> result)
{
    UnityWebRequest www = UnityWebRequest.Get(url);
    yield return www.SendWebRequest();

    if (www.isNetworkError || www.isHttpError)
    {
        Debug.Log(www.error);
        if (result != null)
            result(www.error);
    }
    else
    {
        Debug.Log(www.downloadHandler.data); // this log is returning the requested data. 
        if (result != null)
            result(www.downloadHandler.text);
    }
}