Пример #1
0
        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();
            }
        }
Пример #2
0
        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);
            }
        }
Пример #3
0
        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();
            }
        }
Пример #4
0
        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);
        }