代码之家  ›  专栏  ›  技术社区  ›  FutureCake

Unity WWW WebRequest未执行

  •  2
  • FutureCake  · 技术社区  · 7 年前

    public static string[] GetDBValues(int type, string[] data){
        string base_url = "http://localhost/artez/onderzoeks_opdracht/interface_test/get_elements.php";
        string typePrefix = "?type=";
        string dataPrefix = "data[]=";
        string uriString = base_url + typePrefix + type;
        foreach (string dataElement in data){
            uriString += "&" + dataPrefix + dataElement;
        }
        Debug.Log("executing url request");
        UrlData(uriString);
        return new string[] {"a"};
    }
    
    public static IEnumerator UrlData(string url){
        Debug.Log("searching the web");
        using (WWW www = new WWW(url)){
            Debug.Log(www.text);
            yield return www.text;
        }
    }
    

    此代码编译并执行,但我从未看到以下内容 Debug.Log("searching the web")
    我是C的新手。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Programmer    7 年前

    您的代码中有许多问题:

    UrlData StartCoroutine 调用它而不是像普通函数那样直接调用它。所以, UrlData(uriString); StartCoroutine(UrlData(uriString));

    你必须屈服或等待 WWW WWW.text 属性。那应该是 yield return www yield return www.text .

    public static IEnumerator UrlData(string url)
    {
        Debug.Log("searching the web");
        using (WWW www = new WWW(url))
        {
            yield return www;
            Debug.Log(www.text);
        }
    }
    

    GetDBValues 功能。如果这是真的,那么只需添加 Action 乌尔达塔

    像这样:

    public static IEnumerator UrlData(string url, Action<string> result)
    {
        Debug.Log("searching the web");
        using (WWW www = new WWW(url))
        {
            yield return www;
            if (result != null)
                result(www.text);
        }
    }
    

    result

    StartCoroutine(UrlData("url", (result) =>
    {
        Debug.Log(result);
    }));