簡體   English   中英

WebGL中的Unity WWW發布失敗

[英]Unity WWW Post failure in WebGL

我有一個在WebGL中運行時失敗的類,但它在UNITY IDE(5.6.1f1 Personal(Plus)Edition)中起作用。)下面的代碼被“修剪”了一下,但是產生了相同的特征(與WebGL失敗並運行w /在UNITY IDE中沒有問題。)我將其指向服務URL並在測試時獲得正確的響應,但是從WebGL運行時實際上從不發生發布,並且在沒有響應(甚至沒有錯誤)時將崩潰收到。 我想從社區中獲取想法(也許我需要設置一個特定的構建參數或修改代碼實現?)非常有用的反饋。 謝謝。

--------------------包裝和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);
    }



}

當您嘗試在不了解協程的情況下構建完整程序時,就會發生這種情況。 我鼓勵您創建一個新項目,找到一個協程教程並研究其工作原理。 我認為這是您了解協程的最佳方式。

您的代碼有問題:

1。嘗試使用協程在void函數中等待。 您在PostStartUpRequest函數中在此處調用WaitForResponse()PostStartUpRequest 這將不會等待,因為PostStartUpRequest是一個無效函數。 使PostStartUpRequestIEnumerator函數,然后等待yield return WaitForResponse();

2。void函數中有一個while循環,等待另一個變量狀態在另一個函數中更改。 while (!boolResponse); 除非您是從另一個線程執行此操作,否則不是一個好主意。 這將凍結Unity,因為您沒有給其他功能運行的機會。 您可以通過添加yield return null;來解決此問題yield return null; 在while循環中,每次檢查后等待幀。 這允許其他功能運行。 PostStartUpRequest必須將PostStartUpRequest函數更改為IEnumerator函數。

do
{
    //Wait for a frame
    yield return null;
    ....
} while (!boolResponse);

您可能會從此處崩潰。

3。當您調用WaitForResponse(); 函數從PostWWWMessage函數等待1秒鍾,這不起作用,因為您沒有屈服。 您必須等待WaitForResponse()函數完成等待。 您可以通過屈服來實現。

那應該是yield return WaitForResponse(); 而不是WaitForResponse()

4。未正確等待WWW請求完成。 Webrequest取決於設備和Internet的速度。 有時,它所花費的時間可能比您等待的秒數還要多。 您必須產生WWW請求,而不是等待1秒鍾。

//Make request
wwwForm = new WWW(fullyQualifiedURL, postData, headers);
//Wait for request to finish
yield return wwwForm;

//Now you can safely use it:
JsonReturnMessage = wwwForm.text;

5。在訪問Web請求結果之前不檢查錯誤。

您需要在訪問結果之前檢查可能的錯誤,否則,會發生任何事情,例如從服務器接收null值。

if (String.IsNullOrEmpty(wwwForm.error))
{
   //No Error. Access result
    JsonReturnMessage = wwwForm.text;
}else{
   //Error while making a request
   Debug.Log(wwwForm.error);
}

最后,我不知道為什么您對一個簡單的Web請求具有如此多的功能。 只需使用一個功能即可。

private WWW wwwForm;

// Use this for initialization
void Start()
{
    StartCoroutine(PostWWWMessage("Test"));
}

//private void PostWWWMessage(string JsonPostMessage) {
public 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);
    //Wait for the request
    yield return wwwForm;

    string JsonReturnMessage;
    //Check for error
    if (String.IsNullOrEmpty(wwwForm.error))
    {
        //No Error. Access result
        JsonReturnMessage = wwwForm.text;
        Debug.Log("Received: " + JsonReturnMessage);
    }
    else
    {
        //Error while making a request
        Debug.Log(wwwForm.error);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM