简体   繁体   English

Unity WebGL UnityWebRequest 在上传和下载数据时不起作用

[英]Unity WebGL UnityWebRequest not working when uploading and downloading data

I can neither upload data nor download data by communicating with a web server using UnityWebRequest.我无法通过使用 UnityWebRequest 与 Web 服务器通信来上传数据或下载数据。 In my case I am trying to upload and download user high scores (I am using the dreamlo service).就我而言,我正在尝试上传和下载用户的高分(我使用的是 Dreamlo 服务)。 Both of the Coroutines below do not work as the Upload Coroutine simply does not upload the desired score, and the Download Coroutine does not return text.下面的两个协程都不起作用,因为上传协程根本不上传所需的分数,下载协程也不返回文本。

IEnumerator UploadHighscore(string name, int score, int gameMode){
        UnityWebRequest www = new UnityWebRequest (webURL + privateCode + "/add/" + UnityWebRequest.EscapeURL(name + "+" + score.ToString()) + "/" + score + "/" + gameMode);
        yield return www;

    if (string.IsNullOrEmpty(www.error))
    {
        print("Uploaded");
        uIManager.RegisterCallBack(true);
    }
    else {
        print ("Failed to Upload: " + www.error);
        uIManager.RegisterCallBack(false);
    }
}

IEnumerator DownloadHighscores(){
    UnityWebRequest www = UnityWebRequest.Get (webURL + publicCode + "/pipe/");
    yield return www.SendWebRequest();

    if (www.isNetworkError || www.isHttpError)
    {
        Debug.Log(www.error);
    }
    else
    {
        FormatHighScores(www.downloadHandler.text);
    }
}

Thank you for any help in advance!提前感谢您的任何帮助!

Edit: The code works in the Unity Editor.编辑:代码在 Unity 编辑器中工作。 For some reason it only stops working once embedding my WebGL build into my Website.出于某种原因,它只会在将我的 WebGL 构建嵌入我的网站后停止工作。

Try to use The WWW class.尝试使用 WWW 类。 If the game doesn't work when compiled, send the errors from the console of the browser.如果编译后游戏无法运行,请从浏览器的控制台发送错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM