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 ...
随机推荐
- UICachedDeviceRGBColor CGImage]: unrecognized selector sent to instance 0xxxxxxxxxxx'
UICachedDeviceRGBColor CGImage]: unrecognized selector sent to instance 0xxxxxxxxxxx' 报错原因是 本来应该写空间的 ...
- 9个PNG透明图片免费下载网站推荐
9个PNG透明图片免费下载网站推荐 酷站推荐 2017.08.06 13:47 png格式的图片因为去掉了的背景,方便使用在任何颜色的背景,所以对于从事设计师的朋友来说,经常会用到png透明图片.相信 ...
- layui弹出层layer的area过大被遮挡
layer弹出窗口在弹出时指定了area,弹出后,如果当前页面(iframe)大小比弹出的窗口小,那么就会出现无法操作弹出窗口的尴尬情况.如图: 弹出窗口比当前页面大,这时,唯有放大整个页面才能看到 ...
- python工程设置工具(pipenv)
原始安装 pip工具 --- 包安装工具, 可以从Python包索引hub上安装,也可以使用自定义的hub. 命令: pip install xxx 缺点: 1.命令方式, 一次只能安装一个包, 对于 ...
- C# Task 暂停与取消 或 C#中可取消的Task
(1)https://www.cnblogs.com/zhengzc/p/10724839.html (2)https://blog.csdn.net/hxfhq1314/article/detail ...
- UE4 移动物体的几种方法
转自:https://dawnarc.com/2016/06/ue4%E7%A7%BB%E5%8A%A8%E7%89%A9%E4%BD%93%E7%9A%84%E5%87%A0%E7%A7%8D%E6 ...
- 机器学习之挖掘melb_data.csv数据
mel_data.csv是关于melb地区房屋的数据 mel_data.csv import pandas as pd melbourne_file_path = "E:\data\Melb ...
- JVM知识点总览-高级Java工程师面试必备
jvm 总体梳理 jvm体系总体分四大块: 类的加载机制 jvm内存结构 GC算法 垃圾回收 GC分析 命令调优 当然这些知识点在之前的文章中都有详细的介绍,这里只做主干的梳理 这里画了一个思维导图, ...
- PHP调用webServer接口遇到的坑
昨天公司分配给我一个任务,写一个中转接口,目标接口是一个webservice类型的接口,平时没有接触过,然后遇到一些坑, 一般情况下,能在浏览器打开并显示数据的接口是直接可以使用 CURL或者file ...
- drf源码系列
过滤器 对查询出来的数据进行筛选可写可不写 from rest_framework.filters import BaseFilterBackend 源码 ''' def filter_queryse ...