【发布时间】:2017-07-13 02:53:33
【问题描述】:
我有一个在 WebGL 中运行时失败的类,但它在 UNITY IDE(5.6.1f1 个人(加)版)中工作。代码在下面“修剪”,但产生相同的特征(作为 WebGL 失败并且在 UNITY IDE 中运行没有问题。)我将它指向一个服务 URL 并在测试时得到正确的响应,但是从 WebGL 运行时 Post 实际上永远不会发生,并且在没有响应时会崩溃(甚至没有收到错误)。我想从社区获得想法(也许我需要设置特定的构建参数或修改代码实现?)非常感谢有用的反馈。谢谢。
-------------------- 包装器和 JSON 实用程序类 ----------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JsonTest : MonoBehaviour {
JsonCommunicationManager jsonComm;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void OnMouseDown()
{
var jsonCommunications = gameObject.AddComponent<JsonCommunicationManager>();
string tempReturn = jsonCommunications.PostStartUpRequest("{\"userId\":1,\"id\":1}");
Debug.Log("JSON RequestStartParms: Response : " + tempReturn);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JsonCommunicationManager : MonoBehaviour
{
private WWW wwwForm;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public string PostStartUpRequest(string JsonPostMessage)
{
Debug.Log("Inside PostMessage:");
string JsonReturnMessage;
StartCoroutine(PostWWWMessage(JsonPostMessage));
bool boolResponse = false;
do
{
Debug.Log("Checking for Response ");
try
{
JsonReturnMessage = wwwForm.text;
boolResponse = true;
}
catch
{
WaitForResponse();
Debug.Log("Inside JsonPost Message: WAIT");
}
} while (!boolResponse);
JsonReturnMessage = wwwForm.text;
Debug.Log("Inside JsonPost Message: Messgae Response Received: ");
Debug.Log("Inside JsonPost Message: Messgae Response Data: " + JsonReturnMessage);
Debug.Log("Inside JsonPost Message: Messgae Response Received: ");
Debug.Log("Inside JsonPost Message: Messgae Response Error: " + wwwForm.error);
Debug.Log("Inside JsonPost Message: Messgae Response Received: ");
return JsonReturnMessage;
}
//private void PostWWWMessage(string JsonPostMessage) {
private IEnumerator PostWWWMessage(string JsonPostMessage)
{
Debug.Log("Inside PostWWWMessage:");
Dictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Content-Type", "application/json");
byte[] postData = System.Text.Encoding.ASCII.GetBytes(JsonPostMessage.ToCharArray());
string fullyQualifiedURL = "https://jsonplaceholder.typicode.com/posts";
Debug.Log("Inside PostWWWMessage: Posting Message: " + JsonPostMessage);
print("Posting start up request to: " + fullyQualifiedURL);
print("Post Data is: " + postData);
print("Post Header is: " + headers);
wwwForm = new WWW(fullyQualifiedURL, postData, headers);
WaitForResponse();
yield return null;
}
private IEnumerator WaitForResponse()
{
yield return new WaitForSeconds(1);
}
}
【问题讨论】:
-
试过调试了吗?
标签: c# json unity3d unity-webgl