一、
MonoBehaviour基类的成员方法 StartCoroutine开启异步任务是不支持静态方法中调用的,所以我们需要一个开启协程的工具类
CoroutineHelper.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoroutineHelper : MonoBehaviour
{
private static CoroutineHelper m_instance;
public static CoroutineHelper GetInstance()
{
if (m_instance == null)
{
GameObject obj = new GameObject("CoroutineHelper");
GameObject.DontDestroyOnLoad(obj);
m_instance = obj.AddComponent<CoroutineHelper>();
}
return m_instance;
}
public void Dispose()
{
m_instance = null;
GameObject.Destroy(gameObject);
}
}
二、
http请求的工具类
Http.cs
using UnityEngine;
using System;
using System.Collections;
using LuaInterface;
public class Http
{
static Http m_instance;
static public Http Instance
{
get
{
if (m_instance == null)
m_instance = new Http();
return m_instance;
}
}
public static void Get(string url, Action<WWW> onReceive)
{
CoroutineHelper.GetInstance().StartCoroutine(Instance.EHttpGet(url, onReceive));
}
public static void Post(string url, string data, Action<WWW> onReceive)
{
CoroutineHelper.GetInstance().StartCoroutine(Instance.EHttpPost(url, data, onReceive));
}
public static void Post(string url, byte[] bytes, Action<WWW> onReceive)
{
CoroutineHelper.GetInstance().StartCoroutine(Instance.EHttpPost(url, bytes, onReceive));
}
public static void Post(string url, WWWForm wwwForm, Action<WWW> onReceive)
{
CoroutineHelper.GetInstance().StartCoroutine(Instance.EHttpPost(url, wwwForm, onReceive));
}
IEnumerator EHttpGet(string url, Action<WWW> onReceive)
{
using (WWW www = new WWW(HttpUtils.URLAntiCacheRandomizer(url)))
{
yield return www;
if (!string.IsNullOrEmpty(www.error))
{
Debug.LogWarning(": url: " + www.url + " error: " + www.error);
onReceive(null);
}
else
{
onReceive(www);
}
}
}
IEnumerator EHttpPost(string url, string data, Action<WWW> onReceive)
{
using (WWW www = new WWW(HttpUtils.URLAntiCacheRandomizer(url), System.Text.Encoding.UTF8.GetBytes(data)))
{
yield return www;
if (!string.IsNullOrEmpty(www.error))
{
Debug.LogWarning(": url: " + www.url + " error: " + www.error);
onReceive(null);
}
else
{
onReceive(www);
}
}
}
IEnumerator EHttpPost(string url, byte[] bytes, Action<WWW> onReceive)
{
using (WWW www = new WWW(HttpUtils.URLAntiCacheRandomizer(url), bytes))
{
yield return www;
if (!string.IsNullOrEmpty(www.error))
{
Debug.LogWarning(": url: " + www.url + " error: " + www.error);
onReceive(null);
}
else
{
onReceive(www);
}
}
}
IEnumerator EHttpPost(string url, WWWForm wwwForm, Action<WWW> onReceive)
{
using (WWW www = new WWW(HttpUtils.URLAntiCacheRandomizer(url), wwwForm))
{
yield return www;
if (!string.IsNullOrEmpty(www.error))
{
Debug.LogWarning(": url: " + www.url + " error: " + www.error);
onReceive(null);
}
else
{
onReceive(www);
}
}
}
}
三、
加载图片,这里我们需要一个url和图片的唯一id,我一般是用rul的hashcode或者md5
CacheImage.cs
using UnityEngine;
using System.Collections;
using System.IO;
using System;
using UnityEngine.Events;
/// <summary>
/// 图片缓存
/// </summary>
public class CacheImage
{
private static CacheImage instences = null;
private string path = //Application.persistentDataPath;
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
Application.dataPath + "/StreamingAssets/Cache";
#elif UNITY_IPHONE || UNITY_ANDROID
Application.persistentDataPath;
#else
string.Empty;
#endif
private string name = "{0}.png";
private static UnityAction<Texture2D> cacheEvent;
public static CacheImage Cache(UnityAction<Texture2D> callback)
{
cacheEvent = callback;
if (instences == null)
{
instences = new CacheImage();
}
return instences;
}
public void DownLoad(string url, string identifyId)
{
LoadTexture(url, identifyId);
}
/// <summary>
/// 判断是否本地有缓存
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private void LoadTexture(string url, string identifyId)
{
if (!string.IsNullOrEmpty(url))
{
if (!File.Exists(Path.Combine(path, string.Format(name, identifyId))))
{
LoadNetWorkTexture(url, identifyId);
}
else
{
LoadLocalTexture(url, identifyId);
}
}
}
/// <summary>
/// 本地已缓存
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private void LoadLocalTexture(string url, string identifyId)
{
string filePath = "file:///" + Path.Combine(path, string.Format(name, identifyId));
Http.Get(url, delegate(WWW www)
{
cacheEvent.Invoke(www.texture);
});
}
/// <summary>
/// 本地未缓存
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private void LoadNetWorkTexture(string url, string identifyId)
{
Http.Get(new Uri(url).AbsoluteUri,delegate(WWW www){
if (!string.IsNullOrEmpty(www.error))
{
Debug.Log(string.Format("Failed to load image: {0}, {1}", url, www.error));
}
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);//创建新路径
}
#endif
Texture2D image = www.texture;
//将图片保存至缓存路径
byte[] pngData = image.EncodeToPNG();
File.WriteAllBytes(Path.Combine(path, string.Format(name, identifyId)), pngData);
cacheEvent.Invoke(www.texture);
});
}
}
四、
调用
public static void SetUrlTexture(GameObject go, string url)
{
CacheImage.Cache(delegate(Texture2D texture)
{
go.GetComponent<UITexture>().mainTexture = texture;
}).DownLoad(url, url.GetHashCode().ToString());
}
网友评论