Unity+NGUI打造网络图片异步加载与本地缓存工具类(二)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Unity+NGUI打造网络图片异步加载与本地缓存工具类(二),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3492字,纯文字阅读大概需要5分钟。
内容图文
接上文,我们的工具类中的主要方法:
public void SetAsyncImage(string url,UITexture texture)
按照前文分析的图片加载步骤来
public void SetAsyncImage(string url,UITexture texture){ //开始下载图片前,将UITexture的主图片设置为占位图 texture.mainTexture = placeholder; //判断是否是第一次加载这张图片 if (!File.Exists (path + url.GetHashCode())) { //如果之前不存在缓存文件 StartCoroutine (DownloadImage (url, texture)); } else { StartCoroutine(LoadLocalImage(url,texture)); } }
这里判断缓存文件是否存在使用的是url.GetHashCode()方法,因为我们的图片文件名采用的是原URL的哈希码直接作为文件名来保存,重名概率可以忽略不计,也缩短了文件名的长度提高效率,这个做法借鉴于 iOS开源框架EGOImageView。
如果是第一次加载图片,这个URL对应的文件不存在,那么我们就去原URL下载图片然后赋值给控件
如果缓存文件夹中已有该文件,直接读取加载
由于前文的铺垫,我们的工具类已经是MonoBehaviour的单例子类,所以可以使用unity的异步函数StartCorutine()
接下来完成方法DownloadImage(string url,UITexture texture)
IEnumerator DownloadImage(string url,UITexture texture){ Debug.Log("downloading new image:"+path+url.GetHashCode()); WWW www = new WWW (url); yield return www; Texture2D image = www.texture; //将图片保存至缓存路径 byte[] pngData = image.EncodeToPNG(); File.WriteAllBytes(path+url.GetHashCode(), pngData); texture.mainTexture = image; }
这个方法很简单,然后是从缓存文件夹读取已经存在的图片方法LoadLocalImage(string url,UITexture texture)
注意这里不能使用Resources.Load()方法,因为我们的图片并没有存放在工程目录中,我仔细查阅了相关资料发现比较合适的方法应该还是使用unity的WWW类去加载文件url,即在文件路径的前方加上file:///使之成为一个文件url,然后使用www类读取,但是这个过程是本地的还是比较快
IEnumerator LoadLocalImage(string url,UITexture texture){ string filePath = "file:///" + path + url.GetHashCode (); Debug.Log("getting local image:"+filePath); WWW www = new WWW (filePath); yield return www; //直接贴图 texture.mainTexture = www.texture; }
随便找个Panel添加一个带UITexture组件的节点,然后调用我们的工具方法,测试下运行结果:
第一次运行:
打开这个文件所在的文件夹(我这里使用的是windows系统,不同系统路径不一样)
这个图片已经加载在我们的图片控件上并且已经保存至了本地路径,也就是说我们再次运行之后,不会再进入if的第一种情况了,我们关掉程序再次运行:
正如我们所想,实际上这次是没有产生网络请求的,说明我的缓存已经有用,而且图片一下就出来了,不像上一次要等一会
接下来我们删掉缓存文件再次执行,又会调用第一个方法~
这个工具类先做到这里了,接下来图片切换效果已经加载过程的等待HUD等后面再研究。完整代码:
using UnityEngine; using System.Collections; using System.IO; public class AsyncImageDownload :MonoBehaviour { public Texture placeholder; public static AsyncImageDownload Instance=null; private string path=Application.persistentDataPath+"/ImageCache/" ; //构建单例 public static AsyncImageDownload CreateSingleton() { if (!Directory.Exists(Application.persistentDataPath+"/ImageCache/")) { Directory.CreateDirectory(Application.persistentDataPath+"/ImageCache/"); } GameObject obj = new GameObject (); obj.AddComponent<AsyncImageDownload> (); AsyncImageDownload loader= obj.GetComponent<AsyncImageDownload>(); Instance=loader; loader.placeholder=Resources.Load("placeholder") as Texture; return loader; } public void SetAsyncImage(string url,UITexture texture){ //开始下载图片前,将UITexture的主图片设置为占位图 texture.mainTexture = placeholder; //判断是否是第一次加载这张图片 if (!File.Exists (path + url.GetHashCode())) { //如果之前不存在缓存文件 StartCoroutine (DownloadImage (url, texture)); } else { StartCoroutine(LoadLocalImage(url,texture)); } } IEnumerator DownloadImage(string url,UITexture texture){ Debug.Log("downloading new image:"+path+url.GetHashCode()); WWW www = new WWW (url); yield return www; Texture2D image = www.texture; //将图片保存至缓存路径 byte[] pngData = image.EncodeToPNG(); File.WriteAllBytes(path+url.GetHashCode(), pngData); texture.mainTexture = image; } IEnumerator LoadLocalImage(string url,UITexture texture){ string filePath = "file:///" + path + url.GetHashCode (); Debug.Log("getting local image:"+filePath); WWW www = new WWW (filePath); yield return www; //直接贴图 texture.mainTexture = www.texture; } }
原文:http://blog.csdn.net/mobanchengshuang/article/details/38062431
内容总结
以上是互联网集市为您收集整理的Unity+NGUI打造网络图片异步加载与本地缓存工具类(二)全部内容,希望文章能够帮你解决Unity+NGUI打造网络图片异步加载与本地缓存工具类(二)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。