UGUI 加载图片

图片是动态加载的,然后转换为sprite赋值到ugui的按钮上
代码如下
using UnityEngine;
using System.Collections;
using System.IO;
using UnityEngine.UI; public class UITexLoader : MonoBehaviour
{ public string texname;
private Texture2D m_Tex; private void LoadFromFile(string path)
{
m_Tex = new Texture2D(1, 1);
m_Tex.LoadImage(ReadPNG(path));
} private byte[] ReadPNG(string path)
{
FileStream fileStream = new FileStream(path, FileMode.Open, System.IO.FileAccess.Read); fileStream.Seek(0, SeekOrigin.Begin); byte[] binary = new byte[fileStream.Length]; //创建文件长度的buffer
fileStream.Read(binary, 0, (int)fileStream.Length); fileStream.Close(); fileStream.Dispose(); fileStream = null; return binary;
} // Use this for initialization
void Start()
{ LoadFromFile(texname);
Sprite tempSprite = new Sprite();
tempSprite = Sprite.Create(m_Tex, new Rect(0, 0, m_Tex.width, m_Tex.height), new Vector2(0, 0));
GetComponent<Image>().sprite =tempSprite;
} // Update is called once per frame
void Update ()
{ }
}
把这个脚本绑定到UGUI的按钮上面,运行就可以把按钮上的图片 动态改变了
UGUI 加载图片的更多相关文章
- UGUI加载图片优化方法之一:打包成图集
打包后的: 直接改变sprite中的packing tag,相同的packing tag就是同一张图集中的.改完运行会自动帮你打包
- CSS3学习总结——实现瀑布流布局与无限加载图片相册
首先给大家看一下瀑布流布局与无限加载图片相册效果图: 一.pic1.html页面代码如下: <!DOCTYPE html> <html> <head> <me ...
- JQuery实现无刷新下拉加载图片
最近做的一个项目需要做页面无刷新下拉加载图片,调研了一番,大多都采用检测滚动条达到底部,然后利用ajax加载下一页数据对页面数据进行添加,根据这一逻辑,自己写了一个,具体代码如下: JQu ...
- android 网络加载图片,对图片资源进行优化,并且实现内存双缓存 + 磁盘缓存
经常会用到 网络文件 比如查看大图片数据 资源优化的问题,当然用开源的项目 Android-Universal-Image-Loader 或者 ignition 都是个很好的选择. 在这里把原来 ...
- ImageLoader加载图片
先导universal-image-loader-1.9.3包 在application配置 android:name=".MyApplication" intent权限 1 pa ...
- chrome (failed) net::ERR_INCOMPLETE_CHUNKED_ENCODING ashx 加载图片
chrome (failed) net::ERR_INCOMPLETE_CHUNKED_ENCODING ashx文件加载图片的方法,发现在chrome浏览器里面出了异常: (failed) ne ...
- 在QtCreator 2.1.0 下使用opencv231库加载图片并显示
在.pro中库连接如上图,具体规则正在学习,注意debug下连接*d.lib.release下链接.lib.没有d的. 如果出现imread不可以加载图片,cvloadImage却可以,则是上面说的连 ...
- iOS两种方式加载图片的区别
加载图片的方式: imageNamed: imageWithContentsOfFile: 加载Assets.xcassets这里面的图片: 1> 打包后变成Assets.car 2> 拿 ...
- 利用Volley封装好的图片缓存处理加载图片
Volley 工具箱中提供了一种通过 DiskBasedCache 类实现的标准缓存.这个类能够缓存文件到磁盘的指定目录.但是为了使用 ImageLoader,我们应该提供一个自定义的内存 LRC b ...
随机推荐
- java中的数据结构[copy]
转自http://developer.51cto.com/art/201107/273003.htm java版数据结构与算法,希望对你有帮助. 线性表,链表,哈希表是常用的数据结构,在进行Java开 ...
- Python 连接数据库 mysql
python 连接 数据库 import pymysql db = pymysql.connect(host='127.0.0.1',port=3306,user='root',password='r ...
- KVM虚拟机的xml配置文件
在RHEL6中,用于从磁盘启动的XML文件 这里以dcs01.xml为例: <domain type='kvm'><name>dcs01</name><uui ...
- 有了#ifdef 为什么还需要#if defined
有了#ifdef 为什么还需要#if defined ? #include <stdio.h> #define A #define B void test(int a,int b) { ...
- 前端弹窗展示后台html文件
1,首先使用window.open函数,弹出返回jsp页面的窗口,对应viewZhengXinReport()方法,进行jsp页面的请求跳转forward 2,然后在jsp页面中使用ajax同步请求后 ...
- centos 6.5 升级php到5.6.17版本
1. 下载php5.6.17版本:编译安装, ./configure --prefix=/usr/local/php5. --with-config-/etc --with-mysql=/usr/lo ...
- 解决IDEA 中git 无法自动push 提交问题 Push failed: Failed with error: Could not read from remote repository.
Push failed: Failed with error: Could not read from remote repository.
- ubuntu配置JDK
1.下载JDK jdk-8u151-linux-x64.tar.gz 2.1.解压压缩包 tar -xzvf jdk-8u151-linux-x64.tar.gz 2.2.编辑~/.bashrc ex ...
- Java设计模式(15)备忘录模式(Memento模式)
Memento定义:memento是一个保存另外一个对象内部状态拷贝的对象,这样以后就可以将该对象恢复到原先保存的状态. Memento模式相对也比较好理解,我们看下列代码: public class ...
- apk签名打包时报master password is required to unlock the password database.错误,或者signtrue versions无法勾选,以及Error:Execution failed for task ':app:lintVitalRelease'.
1.如果在签名时android studio报"Master password is required to unlock the password database.The passwor ...