Ste*_*eve 2 c# unity-game-engine
我正在尝试在Unity中发送一个POST请求,但我不能这样做,因为应该跳过的功能被忽略了,我不知道为什么.我添加了日志以了解发生了什么,并且"(json)被称为"永远不会出现,即使文本说已超过功能(发送游戏):postjson之后出现在日志中.
private static void SendGamePlayed() {
int incrementedGamePlayed = GetGamePlayed () + 1;
EventObject anEvent = CreateEvent(EVENT_GAME_PLAYED, incrementedGamePlayed, null, null);
AbcEvent aAbcEvent = new AbcEvent (bundleID, appToken, anEvent);
string json = JsonUtility.ToJson (aAbcEvent);
// test
Debug.Log("(send game played): " + json);
PostJSON (json, EVENT_ROUTE);
Debug.Log ("(send game played): after postjson");
SetGamePlayed (incrementedGamePlayed);
}
private static IEnumerator PostJSON(string jsonString, string route) {
// test
Debug.Log("(post json) called");
string url = SERVER_URL + route;
var request = new UnityWebRequest(url, "POST");
byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
request.uploadHandler = (UploadHandler) new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
Debug.Log ("(post json) before sending");
yield return request.Send();
Debug.Log ("(post json) after sending");
if (request.error != null) {
Debug.Log("request error: " + request.error);
} else {
Debug.Log("request success: " + request.downloadHandler.text);
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
您正在使用Unity的功能进行异步操作.要理解这些概念,请阅读生成器和Unity协同程序.要运行您的函数,请调用以下StartCoroutine函数:
StartCoroutine(PostJSON (json, EVENT_ROUTE));
Run Code Online (Sandbox Code Playgroud)
编辑:
更具体地说:您正在创建一个调用异步函数(发送)的协程(PostJson).
| 归档时间: |
|
| 查看次数: |
964 次 |
| 最近记录: |