Unity3D-下载资源至本地缓存
来源:互联网 发布:linux下不保存退出 编辑:程序博客网 时间:2024/06/10 00:39
本示例使用BestHTTP插件请求url地址下载至本地,并做缓存管理,易于扩展
using System;using UnityEngine;using BestHTTP;using System.IO;using System.Security.Cryptography;public class DownloadCachesMgr : Singleton<DownloadCachesMgr>{ public string AssetCachesDir { get { string dir = "";#if UNITY_EDITOR dir = Application.dataPath + "Caches/";//路径:/AssetsCaches/#elif UNITY_IOS dir = Application.temporaryCachePath + "/Download/";//路径:Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Library/Caches/#elif UNITY_ANDROID dir = Application.persistentDataPath + "/Download/";//路径:/data/data/xxx.xxx.xxx/files/#else dir = Application.streamingAssetsPath + "/Download/";//路径:/xxx_Data/StreamingAssets/#endif return dir; } } public string ImagePathName { get { return AssetCachesDir + "Image/"; } } public string TextPathName { get { return AssetCachesDir + "Config/"; } } private string GetFileName(string url) { string name = StrToMD5(url); return name; } public static string StrToMD5(string str) { byte[] data = System.Text.Encoding.GetEncoding("GB2312").GetBytes(str); MD5 md5 = new MD5CryptoServiceProvider(); byte[] OutBytes = md5.ComputeHash(data); string OutString = ""; for (int i = 0; i < OutBytes.Length; i++) { OutString += OutBytes[i].ToString("x2"); } // return OutString.ToUpper(); return OutString.ToLower(); } private bool CheckFileExists(string path , string name) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } FileInfo t = new FileInfo(path + "//" + name); if (!t.Exists) { return false; } else { return true; } } private void CreateFile(string path, string name,byte[] bytes) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } FileStream fs; FileInfo t = new FileInfo(path + "//" + name); if (!t.Exists) { fs = t.Create(); } else { t.Delete(); fs = t.Create(); } fs.Write(bytes, 0, bytes.Length); fs.Close(); fs.Dispose(); } public void LoadImage(string url, Action<int, Texture2D> callBack) { string name = GetFileName(url); if (CheckFileExists(ImagePathName,name)) { var bytes = File.ReadAllBytes(ImagePathName + "//" + name); Texture2D tex = new Texture2D(0,0); tex.LoadImage(bytes); callBack(0, tex); } else { new HTTPRequest(new Uri(url), (request, response) => { if (request.State == HTTPRequestStates.Finished) { int result = 0; Texture2D tex = null; if (response.IsSuccess) { byte[] bytes = response.Data; CreateFile(ImagePathName, name, bytes); tex = new Texture2D(0,0); tex.LoadImage(bytes); callBack(result, tex); } else { result = response.StatusCode; } if (callBack != null) { callBack(result, tex); } } }).Send(); } }}
阅读全文
0 0
- Unity3D-下载资源至本地缓存
- unity3d asset store 的资源下载到本地的目录
- Unity3d 资源断点下载
- Unity3D官方资源完全下载
- Unity3d 通过网页下载资源
- unity3d www下载资源包
- unity3d加密资源并缓存加载
- unity3d加密资源并缓存加载
- unity3d 加密资源并缓存加载
- unity3d 通过 http网络下载到本地
- 从缓存的源文件中读取资源文件,下载到本地(包括图片和样式脚本)
- Unity3D中文视频PDF资源下载地址
- unity3d之http多线程异步资源下载
- Unity3D之http多线程异步资源下载
- Unity3d的热更新和资源下载
- Unity3D-5.0+的标准资源包下载
- 静态资源本地缓存+静态资源延时加载
- ASIHTTPRequest 缓存+下载文件到本地
- ROC曲线原理及其matlab实现源码
- Java基础加强总结(一)——注解(Annotation)
- 大雁塔,青龙寺,樱花舞,落尘香
- Java中的静态方法具有多态性吗?
- 在rhel7的11.2.0.4 的GI上rootcrs.pl -unlock失败
- Unity3D-下载资源至本地缓存
- 生产者消费者
- 分布式服务Dubbo从入门到"精通"之Schema实现
- 使用信号监控 Django 模型对象字段值的变化
- MPAndroidChart使用详解
- 0604学习总结(进程,多进程,线程)
- Window-Screen对象
- 基于SpringMVC 和MyBatis的实现省份城市之间的动态切换
- 在 Android 的文字编辑控件 (TEdit) 中, 如何按下 Enter 就隐藏虚拟键盘