程序运行后,将获取网络上下载的照片,缓存更新至本地,再次开启无网状态下或网络图片传输异常情况下,程序将读取本地缓存图片。
using UnityEngine;
using System.Collections;
using System.IO;
using System;
using UnityEngine.Events;
/// <summary>
/// 图片缓存
/// </summary>
//namespace Tools.Cache
//{
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;
private static MonoBehaviour mono;
public static CacheImage Cache(MonoBehaviour mb, UnityAction<Texture2D> callback)
{
cacheEvent = callback;
mono = mb;
if (instences == null)
{
instences = new CacheImage();
}
return instences;
}
public void DownLoad(string url, string identifyId)
{
if (mono != null)
{
//if (!string.IsNullOrEmpty(url))
//{
// //判断是否有缓存,无缓存则网络加载
//mono.StartCoroutine(LoadTexture(url, identifyId));
//}
//else
//{
// mono.StartCoroutine(LoadLocalTexture(url, identifyId));
//}
mono.StartCoroutine(SlectLoadTexture(url, identifyId));
}
}
private IEnumerator SlectLoadTexture(string url, string identifyId)
{
if (!string.IsNullOrEmpty(url))
{
yield return LoadNetWorkTexture(url, identifyId);
}
else
{
yield return LoadLocalTexture(url, identifyId);
}
}
/// <summary>
/// 判断是否本地有缓存,如果有则读取本地的资源,否则读取网络资源
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private IEnumerator LoadTexture(string url, string identifyId)
{
if (!string.IsNullOrEmpty(url))
{
if (!File.Exists(Path.Combine(path, string.Format(name, identifyId))))
{
yield return LoadNetWorkTexture(url, identifyId);
}
else
{
yield return LoadLocalTexture(url, identifyId);
}
}
}
/// <summary>
/// 本地已缓存
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private IEnumerator LoadLocalTexture(string url, string identifyId)
{
//已在本地缓存
string filePath = "file:///" + Path.Combine(path, string.Format(name, identifyId));
WWW www = new WWW(filePath);
yield return www;
//Debug.Log(www.texture);
//if (true)
//{
cacheEvent.Invoke(www.texture);
//}
}
/// <summary>
/// 本地未缓存
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private IEnumerator LoadNetWorkTexture(string url, string identifyId)
{
WWW www = new WWW((new Uri(url)).AbsoluteUri);
yield return www;
if (!string.IsNullOrEmpty(www.error)&& File.Exists(Path.Combine(path, string.Format(name, identifyId))))
{
Debug.Log(string.Format("Failed to load image: {0}, {1}", url, www.error));
yield return LoadLocalTexture(url, identifyId);
//yield break;
}
else
{
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);//创建新路径
}
#endif
try
{
//判断网络状态
if ( Application.internetReachability != NetworkReachability.NotReachable)
{
Debug.Log(Application.internetReachability);
Texture2D image = www.texture;
//将图片保存至缓存路径
byte[] pngData = image.EncodeToPNG();
File.WriteAllBytes(Path.Combine(path, string.Format(name, identifyId)), pngData);
cacheEvent.Invoke(www.texture);
}
}
catch
{
Debug.Log(1111);
}
//else
//{
// File.WriteAllBytes(Path.Combine(path, string.Format(name, identifyId)),null);
//}
}
}
}
//}