public static IEnumerator <float> Delete(string url, Action success, Action <string> failure) { UnityWebRequest www = UnityWebRequest.Delete(url); AsyncOperation asyncOp = www.SendWebRequest(); while (!asyncOp.isDone) { yield return(asyncOp.progress); } if (www.IsHttpError()) { failure?.Invoke(www.error); } else { success?.Invoke(); } }
public static IEnumerator <float> GetAudioClip(string url, Action <AudioClip> success, Action <string> failure) { UnityWebRequest www = UnityWebRequest.Get(url); AsyncOperation asyncOp = www.SendWebRequest(); while (!asyncOp.isDone) { yield return(asyncOp.progress); } if (www.IsHttpError()) { failure?.Invoke(www.error); } else { success?.Invoke(((DownloadHandlerAudioClip)www.downloadHandler).audioClip); } }
public static IEnumerator <float> Post(string url, List <IMultipartFormSection> formData, Action success, Action <string> failure) { UnityWebRequest www = UnityWebRequest.Post(url, formData); AsyncOperation asyncOp = www.SendWebRequest(); while (!asyncOp.isDone) { yield return(asyncOp.progress); } if (www.IsHttpError()) { failure?.Invoke(www.error); } else { success?.Invoke(); } }
public static bool DownloadAssetToFile(string url, string path) { Debug.Log($"Downloading asset from {url} to {path}..."); UnityWebRequest www = UnityWebRequest.Get(url); AsyncOperation asyncOp = www.SendWebRequest(); while (!asyncOp.isDone) { Thread.Sleep(500); } if (www.IsHttpError()) { Debug.LogError($"Failed to download asset from {url}: {www.error}"); return(false); } FileInfo fileInfo = new FileInfo(path); if (fileInfo.Exists) { fileInfo.Delete(); } DirectoryInfo dirInfo = fileInfo.Directory; if (null != dirInfo && !dirInfo.Exists) { dirInfo.Create(); } File.WriteAllBytes(path, www.downloadHandler.data); AssetDatabase.ImportAsset(path); return(true); }