unity 發布web GL讀取MySQL數據庫,跨域問題已解決——小白版


更新問題

發現如果unity發布后的webGL,在網頁端讀取服務器的數據,會出現報錯

Access to XMLHttpRequest at '服務器數據地址t' from origin '本機地址' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

這是由於瀏覽器的權限問題,造成的后果就是在unity 的編輯器下可以順利運行,但是一旦發布,在網頁上查詢時,就后出現CORS權限錯誤,

這個權限問題的出現是請求的地址端口與數據地址端口不一致所造成,

有兩種解決方法

第一種方法是最簡單的,也是我現在采用的:將webgl的網頁,與后台的數據放在一塊,同地址,同端口(一定要同一端口,同一IP不同端口也是會報錯的),這個問題就迎刃而解了。

第二種方法需要后台數據處理開發CORS權限,設定Access-Control-Allow-Origin=“你的IP”,這樣也能夠解決這個問題

 

------------------------------------------------------------------------------------------------------我是一條分割線---------------------------------------------------------------------------------------------------------------------------

 

工作需要,項目需要刷新讀取sql數據,且unity需要發布webgl版本,但是unity在webgl的平台下無法支持直連MySql

所以想來很多的解決辦法。

 socket連接,適用於多客戶端,網上的資料大部分都是聊天室的,因此不予考慮。

在Java同事的幫助下,找到了正式的解決辦法。

整個數據流如下:

unity——>javascript——>mysql

javaspcrit刷新讀取sql數據,並以json的格式保存在頁面上,unity用UnityWebRequest(WWW在2018版已棄用)讀取json網頁,獲取下來的值,進行json解析,並賦值給dataset

json代碼

[
    {"id":"37",
"rfid":null,
"material_id":"M000000001",
"type":"2",
"create_time":"2019-04-10 17:21:00",
"start_time":null,
"finish_time":null,
"is_start":"0",
"is_finish":"0",
"is_delete":"0",
},

    {"id":"38",
"rfid":null,
"material_id":"M000000002",
"type":"1",
"create_time":"2019-04-10 17:21:00",
"start_time":null,
"finish_time":null,
"is_start":"1",
"is_finish":"0",
"is_delete":"0",
}
]

unity UnityWebRequest訪問代碼

 string path = "http://192.168.1.100/Data.aspx";
    IEnumerator GetWebInfo()
    {
        UnityWebRequest webInfo = UnityWebRequest.Get(path);
            yield return webInfo.SendWebRequest();
        if (webInfo.isHttpError || webInfo.isNetworkError)
        {
            Debug.Log(webInfo.error);
            StopAllCoroutines();
            yield return null;
        }
        else
        {
            info = webInfo.downloadHandler.text;                     
            Debug.Log(webInfo.downloadHandler.text);
        }
    }

由於json數據是多層嵌套,所以用的是Newtonsoft來解析。引用using Newtonsoft.Json.Linq;

    public void InfoParsing() {

        //解析info
        JArray jar = JArray.Parse(info);
         Debug.Log(jar[jar.Count - 1]);
      
        //新建數據表的列名,與json值對應
        DataTable table = new DataTable("Info");
        DataColumnCollection columns = table.Columns;
        columns.Add("rfid", typeof(string));
        columns.Add("material_id", typeof(string));
        columns.Add("type", typeof(string));
        columns.Add("create_time", typeof(string));
        columns.Add("start_time", typeof(string));
        columns.Add("finish_time", typeof(string));
        columns.Add("is_start", typeof(string));
        columns.Add("is_finish", typeof(string));
        columns.Add("is_delete", typeof(string));

        //for循環依次解析{ …… }單元內容
        for (int i = 0; i < jar.Count; i++)
        {
            //解析每一個單元的值
            JObject obj = JObject.Parse(jar[i].ToString());
            Debug.Log(obj["material_id"]);
            
            //新建dataset行,並賦值
            DataRow infoRow = table.NewRow();
            infoRow["rfid"] = obj["rfid"];
            infoRow["material_id"] = obj["material_id"];
            infoRow["type"] = obj["type"];
            infoRow["create_time"] = obj["create_time"];
            infoRow["start_time"] = obj["start_time"];
            infoRow["finish_time"] = obj["finish_time"];
            infoRow["is_start"] = obj["is_start"];
            infoRow["is_finish"] = obj["is_finish"];
            infoRow["is_delete"] = obj["is_delete"];

            table.Rows.Add(infoRow);

        }
        //將存儲完成的表Add到table中
        infoData.Tables.Add(table);
        Debug.Log(infoData.Tables["Info"].Rows.Count);

        Debug.Log(infoData.Tables["Info"].Rows[0][3]);

        WebText.text = infoData.Tables["Info"].Rows[0][3].ToString();
    }

至此,所有的值都在 infoData.Tables["Info"] 中,可以正常使用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM