Unity的移动端WebGL上传数据到服务器失败的处理方法

1.之前做过一个小项目,Unity打出的webgl工程在手机上运行,需要将Unity工程里的两个数据上传到服务器,并且服务器接收到这两个数据后,将其传入数据库中。因为我之前做过一个类似的项目,所以就直接写了,代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace WebTest0
{
    public class WebTest : MonoBehaviour
    {
        #region 参数
        //游戏管理类
        public ThisGameManager thisGameManager;
        //测试Text
        //public Text testText,testText0,testText1;
        #endregion

        #region 方法
        /// 
        /// 上传字符串到服务器
        /// 
        internal void ToSendString()
        {
            StartCoroutine(GetData());
        }

        /// 
        /// 上传字符串到服务器的协程
        /// 
        /// 
        IEnumerator GetData()
        {
            string hhh = "http://vredu.bit.edu.cn/bitvrlab/kehui/put?id=" + thisGameManager.peopleNumber + "&point=" + thisGameManager.thisScore.ToString("f0");
            WWW www = new WWW(hhh);
            yield return www;
            if (www.error != null)
            {
                Debug.Log(www.error);
                yield return null;
            }
            Debug.Log(www.text);
        }
    }
    #endregion
}

2.写完后,测试,发现电脑端的webgl可以将数据上传到数据库,但是移动端怎么也上传不上去。这就有点蛋疼了,当时觉得Unity的webgl真是个坑,所以就一直和JAVA协调测试,看看到底是什么问题。
3.后来经过测试,发现这种写法手机浏览器是不支持数据上传的,JAVA端改了一种写法,于是Unity这边上传数据的部分也改了写法,代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace WebTest0
{
    public class WebTest : MonoBehaviour
    {
        #region 参数
        //游戏管理类
        public ThisGameManager thisGameManager;
        //测试Text
        //public Text testText,testText0,testText1;
        #endregion

        #region 方法
        /// 
        /// 上传字符串到服务器
        /// 
        internal void ToSendString()
        {
            StartCoroutine(GetData());
        }

        /// 
        /// 上传字符串到服务器的协程
        /// 
        /// 
        IEnumerator GetData()
        {
            string hhh = "http://vredu.bit.edu.cn/bitvrlab/kehui/put/" + thisGameManager.peopleNumber + "/" + thisGameManager.thisScore.ToString("f0");
            WWW www = new WWW(hhh);
            yield return www;
            if (www.error != null)
            {
                Debug.Log(www.error);
                yield return null;
            }
            Debug.Log(www.text);
        }
    }
    #endregion
}

4.经过测试,完美解决移动端webgl不能上传数据的问题,项目顺利完成。

你可能感兴趣的:(Unity网络)