Unity 异步请求脚本协程

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;
        }
    }
}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容