unity 文件、图片加载与保存
来源:互联网 发布:数据库中自然连接 编辑:程序博客网 时间:2024/05/18 17:39
using UnityEngine;
02.
using System.Collections;
03.
using System.IO;
04.
05.
public
class
AsyncImageDownload :MonoBehaviour {
06.
07.
public
Texture placeholder;
08.
public
static
AsyncImageDownload Instance=
null
;
09.
10.
private
string path=Application.persistentDataPath+
"/ImageCache/"
;
11.
12.
//构建单例
13.
public
static
AsyncImageDownload CreateSingleton()
14.
{
15.
if
(!Directory.Exists(Application.persistentDataPath+
"/ImageCache/"
)) {
16.
Directory.CreateDirectory(Application.persistentDataPath+
"/ImageCache/"
);
17.
}
18.
19.
GameObject obj =
new
GameObject ();
20.
obj.AddComponent<AsyncImageDownload> ();
21.
22.
AsyncImageDownload loader= obj.GetComponent<AsyncImageDownload>();
23.
Instance=loader;
24.
loader.placeholder=Resources.Load(
"placeholder"
) as Texture;
25.
return
loader;
26.
27.
}
28.
29.
30.
public
void
SetAsyncImage(string url,UITexture texture){
31.
32.
33.
34.
//开始下载图片前,将UITexture的主图片设置为占位图
35.
texture.mainTexture = placeholder;
36.
37.
//判断是否是第一次加载这张图片
38.
39.
if
(!File.Exists (path + url.GetHashCode())) {
40.
//如果之前不存在缓存文件
41.
42.
43.
StartCoroutine (DownloadImage (url, texture));
44.
45.
46.
}
47.
else
{
48.
49.
StartCoroutine(LoadLocalImage(url,texture));
50.
51.
}
52.
53.
}
54.
55.
IEnumerator DownloadImage(string url,UITexture texture){
56.
Debug.Log(
"downloading new image:"
+path+url.GetHashCode());
57.
WWW www =
new
WWW (url);
58.
yield
return
www;
59.
60.
Texture2D image = www.texture;
61.
//将图片保存至缓存路径
62.
byte
[] pngData = image.EncodeToPNG();
63.
File.WriteAllBytes(path+url.GetHashCode(), pngData);
64.
65.
texture.mainTexture = image;
66.
67.
}
68.
69.
IEnumerator LoadLocalImage(string url,UITexture texture){
70.
string filePath =
"file:///"
+ path + url.GetHashCode ();
71.
72.
Debug.Log(
"getting local image:"
+filePath);
73.
WWW www =
new
WWW (filePath);
74.
yield
return
www;
75.
76.
77.
//直接贴图
78.
texture.mainTexture = www.texture;
79.
80.
}
81.
}
1.最近我也碰到过这个问题,我是这样解决的。
1.将文件下载完成2.通过URL获取文件名字(类型包括在里面了)
3.通过字节的形式,以写文件的方式写到本地。
直接上代码:
private IEnumerator DownLoadToLocal(string url, string number)
{
WWW www = new WWW(url);
string img_name = url.Substring(url.LastIndexOf('/') + 1); //根据URL获取文件的名字。
yield return www;
if (www.error == null)
{
FileStream fs = File.Create(path + img_name); //path为你想保存文件的路径。
fs.Write(www.bytes, 0, www.bytes.Length);
fs.Close();
}
else
{
Debug.Log(www.error);
}
}
0 0
- unity 文件、图片加载与保存
- unity 文件、图片加载与保存
- unity 加载图片文件
- 文件加载与保存
- Unity 加载本地图片文件
- Unity 保存和加载XML文件
- 13.文件加载与保存
- Unity 截屏,WWW加载图片,保存到本地
- Android 图片的加载与保存
- Android 图片的加载与保存
- MFC OPENCV 加载与保存图片
- android 图片的加载保存 与 二级缓存
- Unity 保存图片文理
- unity加载文件的方法-用加载图片举例
- Unity--动态加载网络图片和本地图片文件
- Unity--动态加载网络图片和本地图片文件
- 第十五章文件加载与保存
- 《黑马程序员》 文件加载与保存
- UItableview中型问题
- Hive数据导入HBase
- C# 线程 创建和使用
- 第12周项目1.4
- WORD2007参考文献自动生成
- unity 文件、图片加载与保存
- Unity MegaFiers 顶点动画
- linux下配置tomcat开机自启动
- Palindromic Subsequence - UVa 11404 dp
- 安卓 (1)教你怎么使用下拉刷新
- mac下命令行程序不能读取资源
- .c和.h文件的困惑
- noip2014被虐记
- 字符串转数组