Unity UGUI 多张图片合成一张 记时 显示ICO
2017-07-04 16:29
489 查看
如果没有生成图集的话,一般我在使用数字记数时都是用很多个Image组件来显示,
现在使用GetPixels32和SetPixels32将多张图片合并为一张,实测,十张60*90的图片,
合并成一张600*90的时间为2ms以内,还是非常快的,这样做的好处是不用生成图集,
来一张全家福:
![](https://img-blog.csdn.net/20170704162951040?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveXVoaWprMjA1NQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
再来个硬菜:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
///@brief
///文件名称: ImageTest
///功能描述:
///数据表:
///作者:YuXianQiang
///日期:#CreateTime#
///R1:
///修改作者:
///修改日期:
///修改理由:
namespace XianQiang.Yu
{
public class ImagePicture : MonoBehaviour
{
void Start()
{
//加载图片
Texture2D[] t = Resources.LoadAll<Texture2D>("Icos");
//合成图片
Texture2D tex = MergeImage(t);
//生成后的图自适应大小
GetComponent<RectTransform>().sizeDelta = new Vector2(tex.width, tex.height);
//显示图片
Sprite sp = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
GetComponent<Image>().sprite = sp;
}
/// <summary>
/// @brief 多张Texture2D合成一张Texture2D
/// </summary>
/// <param name="tex"></param>
/// <returns></returns>
public Texture2D MergeImage(Texture2D[] tex)
{
//这里测试合成图片所花的时间
//TimeSpan ts1 = new TimeSpan(DateTime.Now.Ticks);
if (tex.Length == 0) return null;
//定义新图的宽高
int width = 0, height = 0;
for (int i = 0; i < tex.Length; i++)
{
//Debug.Log(tex[i].ToString());
//新图的宽度
width += tex[i].width;
if (i > 0)
{
//新图的高度,这里筛选为最高
if (tex[i].height > tex[i - 1].height)
height = tex[i].height;
}
else height = tex[i].height; //只有一张图
}
//初始Texture2D
Texture2D texture2D = new Texture2D(width, height);
int x = 0, y = 0;
for (int i = 0; i < tex.Length; i++)
{
//取图
Color32[] color = tex[i].GetPixels32(0);
//赋给新图
if (i > 0) texture2D.SetPixels32(x += tex[i - 1].width, y, tex[i].width, tex[i].height, color);
else texture2D.SetPixels32(x, y, tex[i].width, tex[i].height, color);
}
//应用
texture2D.Apply();
//TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
//TimeSpan ts = ts2.Subtract(ts1).Duration();
////查看合成后的图所花时间 ms单位
//Debug.Log(ts.TotalMilliseconds);
return texture2D;
}
}
}
时间显示:
![](https://img-blog.csdn.net/20170704163057577?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveXVoaWprMjA1NQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
///@brief
///文件名称: UpdateTime
///功能描述:
///数据表:
///作者:YuXianQiang
///日期:#CreateTime#
///R1:
///修改作者:
///修改日期:
///修改理由:
namespace XianQiang.Yu
{
public class UpdateTime : MonoBehaviour
{
public Texture2D[] img;
private ImagePicture imgP;
private string curTime;
private string lastTime;
void Start ()
{
imgP = GetComponent<ImagePicture>();
}
void Update()
{
curTime = DateTime.Now.ToString("hh:mm:ss");
if (curTime != lastTime)//一秒更新一次
{
lastTime = curTime;
Texture2D tex = ShowTime();
//生成后的图自适应大小
GetComponent<RectTransform>().sizeDelta = new Vector2(tex.width, tex.height);
//显示图片
Sprite sp = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
GetComponent<Image>().sprite = sp;
}
}
Texture2D ShowTime()
{
Texture2D[] tex = new Texture2D[curTime.Length];
for (int i = 0; i < curTime.Length; i++)
{
string s = curTime.Substring(i, 1);
if (s == ":")
tex[i] = img[img.Length - 1];
else
tex[i] = img[Convert.ToInt32(s)];
}
return imgP.MergeImage(tex);
}
}
}
没有权限报错的请看图
现在使用GetPixels32和SetPixels32将多张图片合并为一张,实测,十张60*90的图片,
合并成一张600*90的时间为2ms以内,还是非常快的,这样做的好处是不用生成图集,
来一张全家福:
再来个硬菜:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
///@brief
///文件名称: ImageTest
///功能描述:
///数据表:
///作者:YuXianQiang
///日期:#CreateTime#
///R1:
///修改作者:
///修改日期:
///修改理由:
namespace XianQiang.Yu
{
public class ImagePicture : MonoBehaviour
{
void Start()
{
//加载图片
Texture2D[] t = Resources.LoadAll<Texture2D>("Icos");
//合成图片
Texture2D tex = MergeImage(t);
//生成后的图自适应大小
GetComponent<RectTransform>().sizeDelta = new Vector2(tex.width, tex.height);
//显示图片
Sprite sp = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
GetComponent<Image>().sprite = sp;
}
/// <summary>
/// @brief 多张Texture2D合成一张Texture2D
/// </summary>
/// <param name="tex"></param>
/// <returns></returns>
public Texture2D MergeImage(Texture2D[] tex)
{
//这里测试合成图片所花的时间
//TimeSpan ts1 = new TimeSpan(DateTime.Now.Ticks);
if (tex.Length == 0) return null;
//定义新图的宽高
int width = 0, height = 0;
for (int i = 0; i < tex.Length; i++)
{
//Debug.Log(tex[i].ToString());
//新图的宽度
width += tex[i].width;
if (i > 0)
{
//新图的高度,这里筛选为最高
if (tex[i].height > tex[i - 1].height)
height = tex[i].height;
}
else height = tex[i].height; //只有一张图
}
//初始Texture2D
Texture2D texture2D = new Texture2D(width, height);
int x = 0, y = 0;
for (int i = 0; i < tex.Length; i++)
{
//取图
Color32[] color = tex[i].GetPixels32(0);
//赋给新图
if (i > 0) texture2D.SetPixels32(x += tex[i - 1].width, y, tex[i].width, tex[i].height, color);
else texture2D.SetPixels32(x, y, tex[i].width, tex[i].height, color);
}
//应用
texture2D.Apply();
//TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
//TimeSpan ts = ts2.Subtract(ts1).Duration();
////查看合成后的图所花时间 ms单位
//Debug.Log(ts.TotalMilliseconds);
return texture2D;
}
}
}
时间显示:
代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
///@brief
///文件名称: UpdateTime
///功能描述:
///数据表:
///作者:YuXianQiang
///日期:#CreateTime#
///R1:
///修改作者:
///修改日期:
///修改理由:
namespace XianQiang.Yu
{
public class UpdateTime : MonoBehaviour
{
public Texture2D[] img;
private ImagePicture imgP;
private string curTime;
private string lastTime;
void Start ()
{
imgP = GetComponent<ImagePicture>();
}
void Update()
{
curTime = DateTime.Now.ToString("hh:mm:ss");
if (curTime != lastTime)//一秒更新一次
{
lastTime = curTime;
Texture2D tex = ShowTime();
//生成后的图自适应大小
GetComponent<RectTransform>().sizeDelta = new Vector2(tex.width, tex.height);
//显示图片
Sprite sp = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
GetComponent<Image>().sprite = sp;
}
}
Texture2D ShowTime()
{
Texture2D[] tex = new Texture2D[curTime.Length];
for (int i = 0; i < curTime.Length; i++)
{
string s = curTime.Substring(i, 1);
if (s == ":")
tex[i] = img[img.Length - 1];
else
tex[i] = img[Convert.ToInt32(s)];
}
return imgP.MergeImage(tex);
}
}
}
没有权限报错的请看图
相关文章推荐
- Unity UGUI——显示图片(Raw Image)
- 多张图片合成一张,显示时只显示一张,只显示图片中的一部分
- unity uGui使用图片显示数字(CustomFont)记录备忘
- Java/Android怎么让一张图片最简单的显示出来?不用布局.
- NSIS脚本 在进度条下显示一张图片
- 求助:想实现点击按钮“下一张”就会显示下一张图片的函数
- 如何在用户区显示一张图片
- SWT图片显示,支持JPEG,GIF(动画),BMP,ICO,PNG和TIFF
- 在scrollview里面显示imageview (一张超大图片), scrollview的上下显示多余的空白
- 上一张图片显示
- py2exe 打包 pyqt程序的图片(ico等) 不显示的问题
- 显示图片实现上一张下一张功能
- 24小时学DX之第二小时——显示一张背景图片
- 在程序中如何把两张图片合成为一张图片
- 用仿ActionScript的语法来编写html5——第一篇,显示一张图片
- 用flex将两张图片合成一张图片
- 在首页的地址栏中显示一个.ico图片
- 在xp和win2003下一张显示奇怪的图片
- SWT图片显示,支持JPEG,GIF(动画),BMP,ICO,PNG和TIFF
- android从网络中获得一张图片,并显示在屏幕上