当前位置:网站首页>Unity图片加载和保存
Unity图片加载和保存
2022-06-23 06:47:00 【牛神自】
加载:
1.异步:(其实还是在主线程加载)
使用UnityWebRequest(或者WWW)进行图片加载,可以加载本地(速度慢,2048*1024大小的图片加载时间780ms,不卡界面)和网络图片。
UnityWebRequest request = UnityWebRequestTexture.GetTexture(Application.streamingAssetsPath + "/Test3.jpg");
yield return request.SendWebRequest();
if (request.downloadHandler.isDone)
{
var tex = DownloadHandlerTexture.GetContent(request);
}
2.同步:
使用File进行读取(速度快,2048*1024大小的图片加载时间22ms,会卡主线程界面,小图片时间短,察觉不到)
var bytess = File.ReadAllBytes(Application.streamingAssetsPath + "/Test3.jpg");
var temp = new Texture2D(1, 1);
temp.LoadImage(bytess);
保存:
1.同步:
//这一步比较耗时,图片很大,会卡主线程
var bytes3 = tex.EncodeToJPG();
//下面的保存也可以用异步,但保存时间很短,也可以不用,2048*1024保存时间大约1ms,
//超大图片可以改用异步保存
File.WriteAllBytes(Application.streamingAssetsPath + "/Test3.jpg", bytes3);
2.异步:
var pixels = tex.GetPixels32(0);
GraphicsFormat format = tex.graphicsFormat;
uint width = (uint)tex.width;
uint height = (uint)tex.height;
Task.Run(() =>
{
var bytes3 = ImageConversion.EncodeArrayToJPG(pixels, format, width, height);
File.WriteAllBytes(Application.streamingAssetsPath + "/Test3.jpg", bytes3);
});
边栏推荐
- Yan's DP analysis
- 启发式的搜索策略
- Paddle version problem
- What is the experience of being a data product manager in the financial industry
- Live broadcast review | how can the container transformation of traditional applications be fast and stable?
- U-Net: Convolutional Networks for Biomedical Image Segmentation
- The List
- MySQL(二) — MySQL数据类型
- 传智教育 | 项目发布前如何打tag标签及标签命名规范
- User mode and kernel mode
猜你喜欢

100 GIS practical application cases (79) - key points of making multi plan integrated base map

Unet代码实现

Sstable details

MYSQL牛客刷题

deeplab v3 代码结构图

Product axure9 (English version), prototype design and production pull-down secondary menu

Advanced drawing skills of Excel lecture 100 (VIII) -excel drawing WiFi diagram

链游飞船开发 农民世界链游开发 土地链游开发

MySQL (VIII) - explain

RFID data security experiment: C # visual realization of parity check, CRC redundancy check and Hamming code check
随机推荐
微信多人聊天及轮盘小游戏(websocket实现)
启发式的搜索策略
Yolov5 detecting small targets (with source code)
Wechat multiplayer chat and Roulette Games (websocket Implementation)
细说Idea那些骚操作
MySQL(五) — 锁及事务
Download the OSS file and modify the file name
MySQL总结
作为思摩尔应对气候变化紧急事件的一项举措,FEELM加入碳披露项目
Both are hard disk partitions. What is the difference between C disk and D disk?
Live broadcast review | how can the container transformation of traditional applications be fast and stable?
313. super ugly number
SSTable详解
【星球精选】如何高效构建 Roam 与 theBrain 间细粒度双向链接?
Spock约束-调用频率/目标/方法参数
Mysql事务隔离级别
【AI实战】xgb.XGBRegressor之多回归MultiOutputRegressor调参2(GPU训练模型)
Nacos adapts Oracle11g create table DDL statement
Simpledateformat thread safety issues
MySQL (V) - locks and transactions