• UnityWebRequest_ZT


    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Networking;
    
    public class C_UnityWebRequest : MonoBehaviour
    {
        static C_UnityWebRequest instance;
        public static C_UnityWebRequest Instance
        {
            get
            {
                if (instance == null)
                {
                    GameObject mounter = new GameObject("C_UnityWebRequest");
                    instance = mounter.AddComponent<C_UnityWebRequest>();
                }
                return instance;
            }
        }
    
        /// <summary>
        /// get请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="actionResult"></param>
        public void Get(string url, Action<UnityWebRequest> actionResult)
        {
            StartCoroutine(_Get(url, actionResult));
        }
    
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="url"></param>
        /// <param name="downloadFilePathAndName"></param>
        /// <param name="actionResult"></param>
        public void DownloadFile(string url, string downloadFilePathAndName, Action<UnityWebRequest> actionResult)
        {
            StartCoroutine(_DownloadFile(url, downloadFilePathAndName, actionResult));
        }
    
        /// <summary>
        /// 请求图片
        /// </summary>
        /// <param name="url"></param>
        /// <param name="actionResult"></param>
        public void GetTexture(string url, Action<Texture2D> actionResult)
        {
            StartCoroutine(_GetTexture(url, actionResult));
        }
    
        /// <summary>
        /// 请求assetBuddle
        /// </summary>
        /// <param name="url"></param>
        /// <param name="actionResult"></param>
        public void GetAssetBundle(string url, Action<AssetBundle> actionResult)
        {
            StartCoroutine(_GetAssetBundle(url, actionResult));
        }
    
        /// <summary>
        /// 下载音效
        /// </summary>
        /// <param name="url"></param>
        /// <param name="actionResult"></param>
        /// <param name="audioType"></param>
        public void GetAudioClip(string url, Action<AudioClip> actionResult, AudioType audioType = AudioType.WAV)
        {
            StartCoroutine(_GetAudioClip(url, actionResult, audioType));
        }
    
        /// <summary>
        /// post form
        /// </summary>
        /// <param name="serverURL"></param>
        /// <param name="lstformData"></param>
        /// <param name="actionResult"></param>
        public void Post(string serverURL, List<IMultipartFormSection> lstformData, Action<UnityWebRequest> actionResult)
        {
            //List<IMultipartFormSection> formData = new List<IMultipartFormSection>(); 
            //formData.Add(new MultipartFormDataSection("field1=foo&field2=bar"));
            //formData.Add(new MultipartFormFileSection("my file data", "myfile.txt")); 
            StartCoroutine(_Post(serverURL, lstformData, actionResult));
        }
    
        /// <summary>
        /// put方法
        /// </summary>
        /// <param name="url"></param>
        /// <param name="contentBytes"></param>
        /// <param name="actionResult"></param>
        public void UploadByPut(string url, byte[] contentBytes, Action<bool> actionResult)
        {
            StartCoroutine(_UploadByPut(url, contentBytes, actionResult, ""));
        }
    
        /// <summary>
        /// get
        /// </summary>
        /// <param name="url"></param>
        /// <param name="actionResult"></param>
        /// <returns></returns>
        IEnumerator _Get(string url, Action<UnityWebRequest> actionResult)
        {
            using (UnityWebRequest uwr = UnityWebRequest.Get(url))
            {
                yield return uwr.SendWebRequest();
                if (actionResult != null)
                {
                    actionResult(uwr);
                }
            }
        }
    
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="url"></param>
        /// <param name="downloadFilePathAndName"></param>
        /// <param name="actionResult"></param>
        /// <returns></returns>
        IEnumerator _DownloadFile(string url, string downloadFilePathAndName, Action<UnityWebRequest> actionResult)
        {
            var uwr = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET);
            uwr.downloadHandler = new DownloadHandlerFile(downloadFilePathAndName);
            yield return uwr.SendWebRequest();
            if (actionResult != null)
            {
                actionResult(uwr);
            }
        }
    
        /// <summary>
        /// 下载图片
        /// </summary>
        /// <param name="url"></param>
        /// <param name="actionResult"></param>
        /// <returns></returns>
        IEnumerator _GetTexture(string url, Action<Texture2D> actionResult)
        {
            UnityWebRequest uwr = new UnityWebRequest(url);
            DownloadHandlerTexture downloadTexture = new DownloadHandlerTexture(true);
            uwr.downloadHandler = downloadTexture;
            yield return uwr.SendWebRequest();
            Texture2D t = null;
            if (!(uwr.isNetworkError || uwr.isHttpError))
            {
                t = downloadTexture.texture;
            }
            if (actionResult != null)
            {
                actionResult(t);
            }
        }
    
        /// <summary>
        /// 下载AssetBuddle
        /// </summary>
        /// <param name="url"></param>
        /// <param name="actionResult"></param>
        /// <returns></returns>
        IEnumerator _GetAssetBundle(string url, Action<AssetBundle> actionResult)
        {
            UnityWebRequest www = new UnityWebRequest(url);
            DownloadHandlerAssetBundle handler = new DownloadHandlerAssetBundle(www.url, uint.MaxValue);
            www.downloadHandler = handler;
    
            yield return www.SendWebRequest();
    
            AssetBundle bundle = null;
    
            if (!(www.isNetworkError || www.isHttpError))
            {
                bundle = handler.assetBundle;
            }
    
            if (actionResult != null)
            {
                actionResult(bundle);
            }
        }
    
        /// <summary>
        /// 下载音效
        /// </summary>
        /// <param name="url"></param>
        /// <param name="actionResult"></param>
        /// <param name="audioType"></param>
        /// <returns></returns>
        IEnumerator _GetAudioClip(string url, Action<AudioClip> actionResult, AudioType audioType = AudioType.WAV)
        {
            using (var uwr = UnityWebRequestMultimedia.GetAudioClip(url, audioType))
            {
                yield return uwr.SendWebRequest();
    
                if (!(uwr.isNetworkError || uwr.isHttpError))
                {
                    if (actionResult != null)
                    {
                        actionResult(DownloadHandlerAudioClip.GetContent(uwr));
                    }
                }
            }
        }
    
        /// <summary>
        /// 表单下载
        /// </summary>
        /// <param name="serverURL"></param>
        /// <param name="lstformData"></param>
        /// <param name="actionResult"></param>
        /// <returns></returns>
        IEnumerator _Post(string serverURL, List<IMultipartFormSection> lstformData, Action<UnityWebRequest> actionResult)
        {
            //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(serverURL, lstformData);
    
            yield return uwr.SendWebRequest();
    
            if (actionResult != null)
            {
                actionResult(uwr);
            }
        }
    
        /// <summary>
        /// put
        /// </summary>
        /// <param name="url"></param>
        /// <param name="contentBytes"></param>
        /// <param name="actionResult"></param>
        /// <param name="contentType"></param>
        /// <returns></returns>
        IEnumerator _UploadByPut(string url, byte[] contentBytes, Action<bool> actionResult, string contentType = "application/octet-stream")
        {
            UnityWebRequest uwr = new UnityWebRequest();
            UploadHandler uploader = new UploadHandlerRaw(contentBytes);
            // Sends header: "Content-Type: custom/content-type";
            uploader.contentType = contentType;
            uwr.uploadHandler = uploader;
    
            yield return uwr.SendWebRequest();
    
            bool res = true;
            if (uwr.isNetworkError || uwr.isHttpError)
            {
                res = false;
            }
    
            if (actionResult != null)
            {
                actionResult(res);
            }
        }
    }

    ZT:https://www.jianshu.com/p/3da9191f82a0

  • 相关阅读:
    Windows 10 IoT Core 17101 for Insider 版本更新
    Windows 10 IoT Core 17093 for Insider 版本更新
    Windows 10 IoT Serials 10 – 如何使用OCR引擎进行文字识别
    2017年全国大学生物联网设计竞赛(TI杯)华东分赛区决赛总结
    Windows 10 IoT Core 17083 for Insider 版本更新
    My Feedback for Windows 10 IoT Core on Feedback Hub App (4/1/2017-1/23/2018)
    为 Azure IoT Edge 设备部署 Azure Stream Analytics 服务
    Azure IoT Edge on Raspberry Pi 3 with Raspbian
    Azure IoT Edge on Windows 10 IoT Core
    Mixed Reality-宁波市VR/AR技术应用高研班总结
  • 原文地址:https://www.cnblogs.com/llstart-new0201/p/9364933.html
Copyright © 2020-2023  润新知