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 异步请求脚本协程
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 0.回调函数 想要在ClassB中使用ClassA的方法(类似Objective-C的block),请使用Syst...
- Python3.3版本的PEP 380中添加了yield from语法,允许一个generator生成器将其部分操...
- 本文为博主原创文章,欢迎转载,请保留出处:http://blog.csdn.net/andrewfan Unity...
- 这篇文章不规范也不完整,重新整理的更详细规范的介绍见这里,非常不建议阅读下文。 网上aiohttp做爬虫的资料太少...