protected IEnumerator LoadAndRequestBytes(string method, string messageBody, BytePostRequestCallback callback) { Debug.Log("Loading request!"); yield return(StartCoroutine(Fade(1f))); loadIcon.blocksRaycasts = true; yield return(StartCoroutine(sendRequestCoroutine(method, messageBody, callback))); loadIcon.blocksRaycasts = false; yield return(StartCoroutine(Fade(0f))); }
/// <summary> /// Asynchronous HTTP Post request. /// src: http://stackoverflow.com/questions/27310201/async-requests-in-unity /// https://docs.unity3d.com/Manual/UnityWebRequest-CreatingDownloadHandlers.html /// </summary> /// <param name="method">REST method name.</param> /// <param name="messageBody">POST message body string.</param> /// <param name="callback">Callback handler that process the response string. </param> /// <returns></returns> protected IEnumerator sendRequestCoroutine(string method, string messageBody, BytePostRequestCallback callback) { var data = System.Text.Encoding.UTF8.GetBytes(messageBody); string urlString = getMethodURL(method); UnityWebRequest webRequest = UnityWebRequest.Post(urlString, UnityWebRequest.kHttpVerbPOST); webRequest.uploadHandler = new UploadHandlerRaw(data); webRequest.downloadHandler = new DownloadHandlerBuffer(); // Request and wait for the desired page. yield return(webRequest.SendWebRequest()); if (webRequest.isNetworkError) { print("Error: " + webRequest.error); } else { callback(webRequest.downloadHandler.data); } }