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的更多相关文章

  1. Python json 读取 json 文件并转为 dict

    Python json 读取 json 文件并转为 dict 在 D 盘 新建 test.json: { "test": "测试\n换行", "dic ...

  2. Unity读取Excel文件(附源代码)

    今天想弄个Unity读取Excel的功能的,发现网上有许多方法,采用其中一种方法:加入库文件 Excel.dll 和ICSharpCode.SharpZipLib.dll库文件,(还有System.D ...

  3. unity读取json文件

    首先填表 [escel转json]注意,粘贴表之后,需要把最后的空行删掉 http://www.bejson.com/json/col2json/ [json格式化] http://www.bejso ...

  4. JavaScript 读取CSV文件并转为js对象

    html部分 <!-- 创建隐藏file input --><button type="button" name="seach" onclic ...

  5. unity 读取excel表 生成asset资源文件

    做unity 项目也有一段时间了,从unity项目开发和学习中也遇到了很多坑,并且也从中学习到了很多曾经未接触的领域.项目中的很多功能模块,从今天开始把自己的思路和代码奉上给学渣们作为一份学习的资料. ...

  6. unity Android 打包后读取 xml 文件

    原地址:http://www.cnblogs.com/wuzhang/p/wuzhang20140731.html 问题:    前天在做东西的过程中发现了一个让人很纠结的问题,为什么Unity 程序 ...

  7. Unity 用C#脚本读取JSON文件数据

    读取JSON文件数据网上有很多方法吗,这里采用SimpleJSON,关于SimpleJSON的介绍参考以下链接:http://wiki.unity3d.com/index.php/SimpleJSON ...

  8. Unity的Json解析<一>--读取Json文件

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/50373558 作者:car ...

  9. Unity 读取、写入自定义路径文件,调用System.Windows.Forms

    调用System.Windows.Forms DLL 首先在Unity新建Plugins文件夹添加System.Windows.Forms.dll 然后代码中添加引用 using System; us ...

随机推荐

  1. bootstrap Tab页切换

    <ul class="nav nav-tabs" id="otherInfoTab"> <li class="active" ...

  2. Appium脚本(4) 使用uiautomator方法定位元素

    from app.find_element.capability import driver from time import sleep # 使用uiautomator方法定位元素 accunt_i ...

  3. 部署TiDB集群

    架构图 节点规划 120.52.146.213 Control Machine 120.52.146.214 PD1_TiDB1 120.52.146.215 PD2_TiDB2 120.52.146 ...

  4. 【Spring Cloud学习之三】负载均衡

    环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 Spring Cloud 1.2 主流的负载均衡技术有nginx.LVS.HAproxy.F5,Spring Clou ...

  5. Guide of Apache Directory Studio

    Default User and Password admin secret

  6. mac下php配置

    打开/关闭服务 sudo apachectl start sudo apachectl stop 查看apche版本 apacectl -v 修改项目默认路径: 打开配置文件目录/private/et ...

  7. identity 基础表没有创建 aspnetuserclaims aspnetuserlogins

    protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuild ...

  8. Python使用队列实现Josephus问题

    Josephus问题,在这个古老的问题中,N个深陷绝境的人一致同意通过以下方式减少生存的人数.他们围坐一圈(位置记为0~N-1)并从第一个人报数,报到M的人会被杀死, 知道最后一个人留下来.传说中Jo ...

  9. scratch少儿编程第一季——09、声音模块:吹拉弹唱我也会

    各位小伙伴大家好: 上期我们学习了外观模块的指令,学会了制作特效. 本期我们来学习如何给游戏配音. 声音模块的指令不是很多,我们一起来看看吧. 首先第一个就是播放声音,里面默认插入了喵叫声. 我们点击 ...

  10. for循环与if条件语句的复习运用

    鉴于前面学了不少基础了,今天没有学新的内容.boyfriend给我出了几道简单的题目,慢慢的进步中. 1.# 计算1-100之间所有偶数的和 def sum(): sumone = 0 for i i ...