如果没有生成图集的话,一般我在使用数字记数时都是用很多个Image组件来显示,
现在使用GetPixels32和SetPixels32将多张图片合并为一张,实测,十张60*90的图片,
合并成一张600*90的时间为2ms以内,还是非常快的,这样做的好处是不用生成图集,
时间显示:
现在使用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);
}
}
}
没有权限报错的请看图