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 ...
随机推荐
- Mac JDK 卸载方法
卸载步骤 输入 sudo rm -fr /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin sudo rm -fr /Library/Prefere ...
- netty-websocket-spring-boot-starter不同url端口复用
netty-websocket-spring-boot-starter是一个基于netty的websocket服务端,目前笔者使用的版本依托于Springboot.官方网址https://github ...
- H3C 开局设置
简介: 系统菜单 即 [H3C] 模式 一:恢复出厂设置 这个没什么说的,必须Console连接. 连上以后,在启动时Ctrl+B,进入管理启动菜单,删除文件,删除配置文件然后重启即可. Ctrl+ ...
- Linux——xargs命令学习
有时候我们会遇到需要将指定命令返回结果进行处理的情况 这种情况下,可能就需要写for循环之类的脚本进行处理了(目前我只能想到这种方法) 但是想起来还有一个xargs命令,组合这个命令就比较省事了. 场 ...
- 查看Oracle表空间以及用户与其默认表空间情况
Oracle中一个表空间可能是多个用户的默认表空间,下面语句统计了用户及其默认表空间情况,如果用户多个,用户之间通过逗号分隔. select t.default_tablespace, to_char ...
- net core 环境部署的坑
1.supervisor “no such file” error. 检查指令是否正确,路径.dotnet环境是否正常 2.Couldn‘t find a valid ICU package inst ...
- Redis 分布式锁,C#通过Redis实现分布式锁(转)
目录(?)[+] 分布式锁一般有三种实现方式: 可靠性 分布式锁一般有三种实现方式: 1. 数据库乐观锁; 2. 基于Redis的分布式锁; 3. 基于ZooKeeper的分布式锁.本篇博客将介绍 ...
- java中的内存分配问题
class A{ int i; int j; } clsaa demo{ public static void main(String[] args){ A aa = new A(); A aa; / ...
- PHP设计模式 - 解释器模式
给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子. 角色: 环境角色(PlayContent):定义解释规则的全局信息. 抽象解释器(Empress):定 ...
- netty内存数据缓冲区使用策略
主要是通过AbstractByteBufAllocator类实现的ByteBuffer的申请. 代码如下: @Override public ByteBuf ioBuffer(int initialC ...