Unity 异步请求脚本协程

作者: zcwfeng | 来源:发表于2017-11-09 10:09 被阅读57次
    using Newtonsoft.Json;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using UnityEngine;
    
    
    public delegate void EventHandlerHTTPString(string text, Dictionary<string, string> responseHeaders);
    public delegate void EventHandlerHTTPTexture(Texture2D texture, Dictionary<string, string> responseHeaders);
    public delegate void EventHnadlerHTTPAssetBundle(AssetBundle assetBundle, Dictionary<string, string> responseHeaders);
    public delegate void EventHandlerOnError(string error);
    
    public class DownloadController : MonoBehaviour {
    
        public EventHandlerHTTPString stringCallback;
        public EventHandlerOnError errorCallback;
    
        public static DownloadController Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = GameObject.FindObjectOfType<DownloadController>();
                }
                return _instance;
            }
        }
        private static DownloadController _instance = null;
        private void Awake()
        {
        }
        // Use this for initialization
        void Start () {
    
        }
    
        public void PostForm(string url, WWWForm formData, EventHandlerHTTPString stringCallback, EventHandlerOnError errorCallback)
        {
            StartCoroutine(RunPostFormCoroutine(url, formData, stringCallback, errorCallback));
        }
    
        public void GetString(string url, EventHandlerHTTPString stringCallback, EventHandlerOnError errorCallback)
        {
            StartCoroutine(RunGetStringCoroutine(url, stringCallback, errorCallback));
        }
    
        public void GetTexture(string url, EventHandlerHTTPTexture textureCallback, EventHandlerOnError errorCallback)
        {
            StartCoroutine(RunGetTextureCoroutine(url, textureCallback, errorCallback));
        }
    
        public void GetAssetBundle(string url, EventHnadlerHTTPAssetBundle assetBuntleCallback, EventHandlerOnError errorCallback)
        {
            StartCoroutine(RunGetAssetBundleCoroutine(url, assetBuntleCallback, errorCallback));
        }
    
        public void GetFile(string url, string dbfilename)
        {
            StartCoroutine(RunGetFileCoroutine(url, dbfilename));
        }
    
        private IEnumerator RunPostFormCoroutine(string url, WWWForm formData, EventHandlerHTTPString stringCallback, EventHandlerOnError errorCallback)
        {
            WWW www = new WWW(url, formData);
    
            while (!www.isDone)
            {
                yield return null;
            }
    
            if (string.IsNullOrEmpty(www.error))
            {
                if (stringCallback != null)
                {
                    stringCallback(www.text, www.responseHeaders);
                }
                else
                {
                    LoggerHelper.Debug("<Color=#4f3c3c>no request callback method.</color>");
                    yield return null;
                }
            }
            else
            {
                if (errorCallback != null)
                    errorCallback(www.error);
            }
    
            if (www != null)
            {
                www.Dispose();
                www = null;
            }
        }
    
        private IEnumerator RunGetStringCoroutine(string url, EventHandlerHTTPString stringCallback, EventHandlerOnError errorCallback)
        {
            WWW www = new WWW(url);
    
            while (!www.isDone)
            {
                yield return null;
            }
    
            if (string.IsNullOrEmpty(www.error))
            {
                if (stringCallback != null)
                {
                    stringCallback(www.text, www.responseHeaders);
                }
                else
                {
                    LoggerHelper.Debug("<Color=#4f3c3c>no request callback method.</color>");
                    yield return null;
                }
            }
            else
            {
                if (errorCallback != null)
                    errorCallback(www.error);
            }
    
            if (www != null)
            {
                www.Dispose();
                www = null;
            }
        }
    
        private IEnumerator RunGetTextureCoroutine(string url, EventHandlerHTTPTexture textureCallback, EventHandlerOnError errorCallback)
        {
            WWW www = new WWW(url);
    
            while (!www.isDone)
            {
                yield return null;
            }
    
            if (string.IsNullOrEmpty(www.error))
            {
                if (textureCallback != null)
                {
                    textureCallback(www.texture, www.responseHeaders);
                }
                else
                {
                    LoggerHelper.Debug("<Color=#4f3c3c>no request callback method.</color>");
                    yield return null;
                }
            }
            else
            {
                if (errorCallback != null)
                    errorCallback(www.error);
            }
    
            if (www != null)
            {
                www.Dispose();
                www = null;
            }
        }
    
        private IEnumerator RunGetFileCoroutine(string url, string dbfilename)
        {
            string filename = string.Format("{0}/{1}", Application.persistentDataPath, dbfilename);
    
            if (!Directory.Exists(Application.persistentDataPath))
            {
                Directory.CreateDirectory(Application.persistentDataPath);
            }
    
            byte[] bytes;
    
            WWW www = new WWW(url);
            yield return www;
            bytes = www.bytes;
    
            if (bytes != null)
            {
                try
                {
                    using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
                    {
                        fs.Write(bytes, 0, bytes.Length);
                    }
    
                }
                catch (Exception e)
                {
                    LoggerHelper.Debug(e.Message);
                }
            }
        }
    
        private IEnumerator RunGetAssetBundleCoroutine(string url, EventHnadlerHTTPAssetBundle assetBundleCallback, EventHandlerOnError errorCallback)
        {
            WWW www = new WWW(url);
    
            while (!www.isDone)
            {
                yield return null;
            }
    
            if (string.IsNullOrEmpty(www.error))
            {
                if (assetBundleCallback != null)
                {
                    assetBundleCallback(www.assetBundle, www.responseHeaders);
                }
                else
                {
                    LoggerHelper.Debug("<Color=#4f3c3c>no request callback method.</color>");
                    yield return null;
                }
            }
            else
            {
                if (errorCallback != null)
                    errorCallback(www.error);
            }
    
            if (www != null)
            {
                www.Dispose();
                www = null;
            }
        }
    }
    
    
    

    相关文章

      网友评论

        本文标题:Unity 异步请求脚本协程

        本文链接:https://www.haomeiwen.com/subject/cyhpmxtx.html