工作需要写的 小白一枚,不足地方希望大神指出 直接挂在场景中物体上mImageCacheMaxNumber 参数为本地可缓存的数量。 根据时间将时间长的删除。
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEngine.Networking;
public class CacheFileData
{
public int time;
public string path;
}
public class ImageLocalCacheMgr : Singleton<ImageLocalCacheMgr>
{
Dictionary<string,string> mAllImagePath = new Dictionary<string, string>();
List<string> mAllFilesPaths = new List<string>();
private string mAssetsDirectory;
[Header("允许本地缓存的数量")]
public int mImageCacheMaxNumber = 100;
private Texture2D mAssetIconTexture;
void Start()
{
Init();
}
private void Init()
{
mAssetsDirectory = Application.persistentDataPath + "/Assets/ImageCache/";
GetLocalImagePath(mAssetsDirectory);
}
void GetLocalImagePath(string path)
{
if (Directory.Exists(path))
{
mAllFilesPaths = Directory.GetFiles(path, ".", SearchOption.AllDirectories).ToList();
if (mAllFilesPaths.Count > mImageCacheMaxNumber)
{
SortCacheTimeList(mAllFilesPaths);
}
if (mAllFilesPaths.Count > 0)
{
for (int i = 0; i < mAllFilesPaths.Count; i++)
{
if (!mAllFilesPaths[i].Contains(".meta"))
{
string fileName = mAllFilesPaths[i].Substring(mAllFilesPaths[i].LastIndexOf("/") + 1);
mAllImagePath.Add(fileName, mAllFilesPaths[i]);
}
}
}
}
else
{
Directory.CreateDirectory(path);
}
}
#region 图片加载
public void GetImage(string iconNmae, string url, Action<Texture2D> iconCallback)
{
string tempUrl;
if (mAllImagePath.ContainsKey(iconNmae))
{
mAllImagePath.TryGetValue(iconNmae, out tempUrl);
LoadLocalCacheImage(tempUrl, iconCallback);
}
else
{
tempUrl = url;
StartCoroutine(GetAssetImageRequest(iconNmae, tempUrl, iconCallback));
}
}
private void LoadLocalCacheImage(string url, Action<Texture2D> iconCallback)
{
Texture2D cacheIcon = TextureEdition.GetTexture2DFromPath(url);
if (cacheIcon != null)
{
iconCallback.Invoke(cacheIcon);
}
}
IEnumerator GetAssetImageRequest(string cacheName, string url, Action<Texture2D> iconCallback)
{
using (UnityWebRequest wr = new UnityWebRequest(url))
{
DownloadHandlerTexture texDl = new DownloadHandlerTexture(true);
wr.downloadHandler = texDl;
yield return wr.SendWebRequest();
if (!wr.isNetworkError && !wr.isHttpError)
{
mAssetIconTexture = new Texture2D(texDl.texture.width, texDl.texture.height);
mAssetIconTexture = texDl.texture;
byte[] pngData = mAssetIconTexture.EncodeToPNG();
File.WriteAllBytes(mAssetsDirectory + cacheName, pngData);
if (mAllImagePath.ContainsKey(cacheName))
mAllImagePath[cacheName] = mAssetsDirectory + cacheName;
else
mAllImagePath.Add(cacheName, mAssetsDirectory + cacheName);
}
iconCallback.Invoke(mAssetIconTexture);
}
}
#endregion
#region 删除长时的缓存文件
List<CacheFileData> mSortTimeFileDatas = new List<CacheFileData>();
List<CacheFileData> mLongTimeFilePaths = new List<CacheFileData>();
private void SortCacheTimeList(List<string> paths)
{
for (int i = 0; i < paths.Count; i++)
{
FileInfo fi = new FileInfo(paths[i]);
int days = CompanyDate(DateTime.Now.ToLocalTime(), fi.LastWriteTime);
mSortTimeFileDatas.Add(new CacheFileData(){time = days,path = paths[i] });
}
mSortTimeFileDatas.Sort((x, y) => { return x.time.CompareTo(y.time); });
for (int i = mImageCacheMaxNumber; i < mSortTimeFileDatas.Count; i++)
{
mLongTimeFilePaths.Add(mSortTimeFileDatas[i]);
}
for (int i = 0; i < mLongTimeFilePaths.Count; i++)
{
mAllFilesPaths.Remove(mAllFilesPaths.Find(X => X.Equals(mLongTimeFilePaths[i].path)));
File.Delete(mLongTimeFilePaths[i].path);
}
}
public int CompanyDate(DateTime dateStr1, DateTime dateStr2)
{
return dateStr1.Subtract(dateStr2).Days;
}
#endregion
}
|