资源简介
程序运行后,将获取网络上下载的照片,缓存更新至本地,再次开启无网状态下或网络图片传输异常情况下,程序将读取本地缓存图片。
代码片段和文件信息
using UnityEngine;
using System.Collections;
using System.IO;
using System;
using UnityEngine.Events;
///
/// 图片缓存
///
//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 cacheEvent;
private static MonoBehaviour mono;
public static CacheImage Cache(MonoBehaviour mb UnityAction 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);
}
}
///
/// 判断是否本地有缓存如果有则读取本地的资源,否则读取网络资源
///
///
///
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);
}
e
评论
共有 条评论