資源簡介
程序運(yùn)行后,將獲取網(wǎng)絡(luò)上下載的照片,緩存更新至本地,再次開啟無網(wǎng)狀態(tài)下或網(wǎng)絡(luò)圖片傳輸異常情況下,程序?qū)⒆x取本地緩存圖片。
代碼片段和文件信息
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))
????????????//{
????????????//?//判斷是否有緩存,無緩存則網(wǎng)絡(luò)加載
????????????//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);
????????}
????}
????????///?
????????///?判斷是否本地有緩存如果有則讀取本地的資源,否則讀取網(wǎng)絡(luò)資源
????????///?
????????///?
????????///?
????????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
評(píng)論
共有 條評(píng)論