unity读取Texture文件并转为Sprit
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI; public class ImageTest : MonoBehaviour
{
/// <summary>
/// Image控件
/// </summary>
private Image image; void Start()
{
image = this.transform.Find("Image").GetComponent<Image>(); //为不同的按钮绑定不同的事件
this.transform.Find("LoadByWWW").GetComponent<Button>().onClick.AddListener
(
delegate () { LoadByWWW(); }
); this.transform.Find("LoadByIO").GetComponent<Button>().onClick.AddListener
(
delegate () { LoadByIO(); }
);
} /// <summary>
/// 以IO方式进行加载
/// </summary>
private void LoadByIO()
{
// double startTime = (double)Time.time;
//创建文件读取流
FileStream fileStream = new FileStream(Application.dataPath+ "/UI/Basic Information/Common/Add.png", FileMode.Open, FileAccess.Read);
fileStream.Seek(, SeekOrigin.Begin);
//创建文件长度缓冲区
byte[] bytes = new byte[fileStream.Length];
//读取文件
fileStream.Read(bytes, , (int)fileStream.Length);
//释放文件读取流
fileStream.Close();
fileStream.Dispose();
fileStream = null; //创建Texture
int width = ;
int height = ;
Texture2D texture = new Texture2D(width, height);
texture.LoadImage(bytes); //创建Sprite
Sprite sprite = Sprite.Create(texture, new Rect(, , texture.width, texture.height), new Vector2(0.5f, 0.5f));
image.sprite = sprite; //startTime = (double)Time.time - startTime;
//Debug.Log("IO加载用时:" + startTime);
} /// <summary>
/// 以WWW方式进行加载
/// </summary>
private void LoadByWWW()
{
StartCoroutine(Load());
} IEnumerator Load()
{
double startTime = (double)Time.time;
//请求WWW
//WWW www = new WWW("file://D:\\test.jpg");
string path= (Application.dataPath + "/UI/Basic Information/Common/Add.png");
WWW www=new WWW("file://"+path);
yield return www;
if (www != null && string.IsNullOrEmpty(www.error))
{
//获取Texture
Texture2D texture = www.texture; //创建Sprite
Sprite sprite = Sprite.Create(texture, new Rect(, , texture.width, texture.height), new Vector2(0.5f, 0.5f));
image.sprite = sprite; startTime = (double)Time.time - startTime;
Debug.Log("WWW加载用时:" + startTime);
}
}
} 原文链接http://blog.csdn.net/qinyuanpei/article/details/48262583
unity读取Texture文件并转为Sprit的更多相关文章
- Python json 读取 json 文件并转为 dict
		
Python json 读取 json 文件并转为 dict 在 D 盘 新建 test.json: { "test": "测试\n换行", "dic ...
 - Unity读取Excel文件(附源代码)
		
今天想弄个Unity读取Excel的功能的,发现网上有许多方法,采用其中一种方法:加入库文件 Excel.dll 和ICSharpCode.SharpZipLib.dll库文件,(还有System.D ...
 - unity读取json文件
		
首先填表 [escel转json]注意,粘贴表之后,需要把最后的空行删掉 http://www.bejson.com/json/col2json/ [json格式化] http://www.bejso ...
 - JavaScript 读取CSV文件并转为js对象
		
html部分 <!-- 创建隐藏file input --><button type="button" name="seach" onclic ...
 - unity 读取excel表 生成asset资源文件
		
做unity 项目也有一段时间了,从unity项目开发和学习中也遇到了很多坑,并且也从中学习到了很多曾经未接触的领域.项目中的很多功能模块,从今天开始把自己的思路和代码奉上给学渣们作为一份学习的资料. ...
 - unity Android 打包后读取 xml 文件
		
原地址:http://www.cnblogs.com/wuzhang/p/wuzhang20140731.html 问题: 前天在做东西的过程中发现了一个让人很纠结的问题,为什么Unity 程序 ...
 - Unity 用C#脚本读取JSON文件数据
		
读取JSON文件数据网上有很多方法吗,这里采用SimpleJSON,关于SimpleJSON的介绍参考以下链接:http://wiki.unity3d.com/index.php/SimpleJSON ...
 - Unity的Json解析<一>--读取Json文件
		
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/50373558 作者:car ...
 - Unity 读取、写入自定义路径文件,调用System.Windows.Forms
		
调用System.Windows.Forms DLL 首先在Unity新建Plugins文件夹添加System.Windows.Forms.dll 然后代码中添加引用 using System; us ...
 
随机推荐
- H5+js调用相机
			
在机缘巧合之下,了解到用HTML5和javascript调用摄像头来实现拍照功能,今天就把大致原理写下来.页面布局很简单,就是一个input标签,两个HTML5元素video.canvas和一个but ...
 - Linux performance monitor tool
			
https://www.tecmint.com/command-line-tools-to-monitor-linux-performance/ https://www.tecmint.com/lin ...
 - 安装opencv时ippicv下载超时
			
1.手动去下载: github地址为: https://github.com/opencv/opencv_3rdparty/tree/ippicv/master_20151201/ippicv 2.查 ...
 - Vue打包发布到Tomcat后,刷新报错404解决方法
			
在应用下面加 WEB-INF 建 web.xml 内容如下 <?xml version="1.0" encoding="ISO-8859-1"?> ...
 - Invalid prop: custom validator check failed for prop "pagination" <Table> vue.runtime.esm
			
错误如图 原因,返回数据中没有包括分布的属性
 - Android studio:URI is not registered 的解决办法
			
今天新导入一个android studio的工程,发现在布局文件中报URI is not registerd的错误,布局文件的命名空间变成红色的了. 解决方案: 进入 File->Setting ...
 - EasyNVR网页摄像机无插件H5、谷歌Chrome直播方案-Onvif(二)使用Onvif协议进行设备RTSP地址获取
			
背景介绍 EasyNVR最大的优势就是兼容性,通过RTSP协议接入传统网络摄像机.NVR.编码器等,使用RTSP协议接入能兼容市面上绝大多数网络摄像机等源设备,最大程度的提高整体方案的硬件设备的兼容性 ...
 - DELPHI 数据库控件心得
			
TField对象的SetText和GetText事件处理函数 使用TField对象的SetText和GetText事件处理函数可方便的解决字段的代码与代码所对应值的显示问题 TSimpleDatase ...
 - (十二)springboot中shiro的使用
			
一.引入maven配置 <dependency> <groupId>org.apache.shiro</groupId> <artifactI ...
 - 使用tomcat7-maven-plugin
			
2019-01-0714:16:44 功能: (使用maven中的tomcat插件,就可以将tomcat集成到项目中,效果就是:在不同平台中无需配置tomcat就可以直接运行web) 地址: tomc ...