该
WWWAPI应该得到这个工作,但
UnityWebRequest取而代之,所以我会回答新的API。真的很简单。您必须使用协程通过Unity的API来执行此 *** 作,否则必须使用C#标准Web请求API和Thread之一。使用协程,您可以发出请求直到完成。这不会阻止主线程,也不会阻止其他脚本运行。
注意事项 :
对于以下示例,如果您使用的是 Unity 2017.2 以下的 版本
,请替换
SendWebRequest()为
Send(),然后替换
isNetworkError为
isError。这将适用于较低版本的Unity。另外,如果您需要以二进制形式访问下载的数据,请替换
uwr.downloadHandler.text为
uwr.downloadHandler.data。最后,该
SetRequestHeader函数用于设置请求的标头。
GET请求 :
void Start(){ StartCoroutine(getRequest("http:///www.yoururl.com"));}IEnumerator GetRequest(string uri){ UnityWebRequest uwr = UnityWebRequest.Get(uri); yield return uwr.SendWebRequest(); if (uwr.isNetworkError) { Debug.Log("Error While Sending: " + uwr.error); } else { Debug.Log("Received: " + uwr.downloadHandler.text); }}
带表单的POST请求 :
void Start(){ StartCoroutine(postRequest("http:///www.yoururl.com"));}IEnumerator PostRequest(string url){ WWWForm form = new WWWForm(); form.AddField("myField", "myData"); form.AddField("Game Name", "Mario Kart"); UnityWebRequest uwr = UnityWebRequest.Post(url, form); yield return uwr.SendWebRequest(); if (uwr.isNetworkError) { Debug.Log("Error While Sending: " + uwr.error); } else { Debug.Log("Received: " + uwr.downloadHandler.text); }}
与Json的POST请求 :
void Start() { StartCoroutine(postRequest("http:///www.yoururl.com", "your json")); } IEnumerator PostRequest(string url, string json) { var uwr = new UnityWebRequest(url, "POST"); byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json); uwr.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend); uwr.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer(); uwr.SetRequestHeader("Content-Type", "application/json"); //Send the request then wait here until it returns yield return uwr.SendWebRequest(); if (uwr.isNetworkError) { Debug.Log("Error While Sending: " + uwr.error); } else { Debug.Log("Received: " + uwr.downloadHandler.text); } }
带有Multipart FormData / Multipart表单文件的POST请求 :
void Start(){ StartCoroutine(postRequest("http:///www.yoururl.com"));}IEnumerator PostRequest(string url){ List<IMultipartFormSection> formData = new List<IMultipartFormSection>(); formData.Add(new MultipartFormDataSection("field1=foo&field2=bar")); formData.Add(new MultipartFormFileSection("my file data", "myfile.txt")); UnityWebRequest uwr = UnityWebRequest.Post(url, formData); yield return uwr.SendWebRequest(); if (uwr.isNetworkError) { Debug.Log("Error While Sending: " + uwr.error); } else { Debug.Log("Received: " + uwr.downloadHandler.text); }}
放置请求 :
void Start(){ StartCoroutine(putRequest("http:///www.yoururl.com"));}IEnumerator PutRequest(string url){ byte[] dataToPut = System.Text.Encoding.UTF8.GetBytes("Hello, This is a test"); UnityWebRequest uwr = UnityWebRequest.Put(url, dataToPut); yield return uwr.SendWebRequest(); if (uwr.isNetworkError) { Debug.Log("Error While Sending: " + uwr.error); } else { Debug.Log("Received: " + uwr.downloadHandler.text); }}
删除请求 :
void Start(){ StartCoroutine(deleteRequest("http:///www.yoururl.com"));}IEnumerator DeleteRequest(string url){ UnityWebRequest uwr = UnityWebRequest.Delete(url); yield return uwr.SendWebRequest(); if (uwr.isNetworkError) { Debug.Log("Error While Sending: " + uwr.error); } else { Debug.Log("Deleted"); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)