Unity UGUI 多张图片合成一张 记时 显示ICO

如果没有生成图集的话,一般我在使用数字记数时都是用很多个Image组件来显示,
现在使用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);
            }
        }
}

没有权限报错的请看图


  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值