从网络下载文件并保存至本地,及其文件的读取
来源:互联网 发布:英文文献阅读软件 编辑:程序博客网 时间:2024/05/16 13:40
最近因工作需要做了一个需求如下的功能:从网络下载图片,保存到本地,版本号不同时,更新本地文件,否则每次只从本地读取文件。
1、首先需要三个string类型的变量和一个WWW
WWW www = null;string m_localPath = string.Format("{0}/LocalDirectory", Application.persistentDataPath);string m_localKey = "LocalKey";string m_localFilePath = string.Empty;UITexture m_texture = null;//这是一个NGUI的UITexture,需要在代码中关联,此处不细表
2、下面介绍具体实现方法
void DownLoadFunction(string url, int version){if(!Directory.Exists(m_localPath)){Directory.CreatDirectory(m_localPath);}//取文件名string[] tempArray = url.Split(new char[]{'/'}, StringSplitOptions.RemoveEmptyEntries);string tempPictureName = string.Empty;if(tempArray != null){tempPictureName = tempArray[tempArray.Length - 1];}m_localFilePath = string.Format("{0}/{1}", m_localPath, tempPictureName);if(PlayerPerfs.HasKey(m_localKey) && PlayerPrefs.GetInt(m_localKey) == version && File.Exists(m_localFilePath)){www = new WWW(GetResDirByPath(m_localFilePath));}else{www = new WWW(url);PlayerPrefs.SetInt(m_localKey, version);}}3、下面是一个读取本地文件时的一个路径转换方法
voidGetResDirByPath(string path){string strBaseDir = string.Empty;if(RuntimePlatform.Android == Application.platform){strBaseDir = "file:///" + path;}else if(RuntimePlatform.IPhonePlayer == Application.platform){strBaseDir = "file://" + path;}else if(RuntimePlatform.WindowsWebPlayer == Application.platform ||RuntimePlatform.OSXWebPlayer == Application.platform)){strBaseDir = path;}else{strBaseDir = "file:///" + path;}return strBaseDir;}4、下面是每个游戏中都会存在的Update方法。
void Update(){if(www != null){if(www.error != null){Debug.LogError(www.error + "FROM URL:" + www.url);www = null;}else if(www.isDone && www.progress == 1){File.WriteAllBytes(m_localFilePath, www.texture.EncodeToPNG());m_texture.mainTexture = www.texture;www.Dispose();www = null;}}}5、以上就是针对此功能的主要功能代码,部分内容需要自己去查找和理解
阅读全文
1 0
- 从网络下载文件并保存至本地,及其文件的读取
- java 从服务器下载文件并保存到本地
- Java从网络读取图片并保存至本地
- Java从网络读取图片并保存至本地 .
- Java从网络读取图片并保存至本地
- Java从网络读取图片并保存至本地
- Java从网络读取图片并保存至本地
- Java从网络读取图片并保存至本地
- Java从网络读取图片并保存至本地
- Java从网络读取图片并保存至本地
- 【Java】Java从网络读取图片并保存至本地
- Java从网络(http)读取图片并保存至本地
- Java从网络上读取图片并保存至本地
- 从URL读取文件保存到本地
- VC下载网页文件,并保存至本地磁盘
- 从网络上进行下载图片,并保存到本地
- 如何从网络下载文件到本地
- JAVA从网络下载文件到本地
- svn基本操作
- org.aspectj.lang.JoinPoint-中文简要API
- Python实现udp协议的简单服务器和客户端通信
- VHDL 语法
- 判断一颗二叉树是否是平衡二叉树
- 从网络下载文件并保存至本地,及其文件的读取
- 4种最常用的诡辩方法
- static的用途
- 【CSS】福利!总结css中15种隐藏!
- 几种基本的线程池详解
- python学习(三)伪装成浏览器
- 一些有意思的模板
- 6、ssm实现分页、模态框的使用
- Polar码概述
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
皇家猫粮
狗吃猫粮
国产猫粮
凯瑞思猫粮
猫粮推荐
猫吐猫粮
自制猫粮
玫斯猫粮
发育宝猫粮
比瑞吉猫粮
猫粮怎么喂
毒猫粮
幼猫粮
好主人猫粮
猫粮品牌
雪山猫粮
天然猫粮
天然粮猫粮
猫粮怎么做
什么猫粮好
哪个猫粮好
evo猫粮
品牌猫粮
猫粮多少钱
珍宝猫粮
猫粮图片
波奇网猫粮
猫粮成分
猫粮什么好
那种猫粮好
好点的猫粮
猫粮厂家
猫粮罐头
推荐猫粮
妙好猫粮
猫粮加工厂
猫粮代理
猫粮加工
全猫粮
猫粮渴望
优瑞派猫粮