Created
November 2, 2017 03:30
-
-
Save mosluce/9ca6e4c9a418096d903be52e37dd6ed5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using UnityEditor; | |
| using System; | |
| using UnityEngine.Networking; | |
| public enum HttpMethoed { | |
| POST, PUT, GET, DELETE | |
| } | |
| public class EditorWebRequest { | |
| UnityWebRequest request; | |
| AsyncOperation operation; | |
| Action<float> progressHandler; | |
| Action<string> successHandler; | |
| Action<string> failureHandler; | |
| private EditorWebRequest() { } | |
| public static void Start(HttpMethoed method, string url, string data, Action<float> progressHandler, Action<string> successHandler, Action<string> failureHandler) { | |
| var req = new EditorWebRequest { | |
| progressHandler = progressHandler, | |
| successHandler = successHandler, | |
| failureHandler = failureHandler | |
| }; | |
| switch (method) { | |
| case HttpMethoed.POST: | |
| req.request = UnityWebRequest.Post(url, data); | |
| break; | |
| case HttpMethoed.GET: | |
| req.request = UnityWebRequest.Get(url); | |
| break; | |
| case HttpMethoed.PUT: | |
| req.request = UnityWebRequest.Put(url, data); | |
| break; | |
| case HttpMethoed.DELETE: | |
| req.request = UnityWebRequest.Delete(url); | |
| break; | |
| } | |
| EditorApplication.update += req.Update; | |
| req.request.SetRequestHeader("content-type", "application/json"); | |
| req.operation = req.request.Send(); | |
| } | |
| public void Update() { | |
| if (!operation.isDone) { | |
| progressHandler?.Invoke(operation.progress); | |
| } else { | |
| EditorApplication.update -= Update; | |
| EditorUtility.ClearProgressBar(); | |
| if (request.isHttpError || request.isNetworkError) { | |
| failureHandler?.Invoke(request.error); | |
| } else { | |
| successHandler?.Invoke(request.downloadHandler.text); | |
| } | |
| request.Dispose(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment