Unity UI实现循环播放序列图
程序员文章站
2022-06-15 13:24:20
一、思路1.获取播放组件一般我们使用ui的raw image或者image来显示图片image:仅支持sprite类型图片,需要更改图片的格式(注意:在streamingassets文件夹里的图片是更...
一、思路
1.获取播放组件
一般我们使用ui的raw image或者image来显示图片
image:仅支持sprite类型图片,需要更改图片的格式(注意:在streamingassets文件夹里的图片是更改不了类型的,在这里必须放在assets/resources路径下)
raw image:支持图片的原格式,一般我们将其转换成 texture2d使用
2.加载图片
resources提供了一个load方法,可以从resources文件夹里加载图片。
!!!!!注意一定要在resources路径下,否则找不到
resources.load(path, typeof(texture2d)) as texture2d; resources.load(path, typeof(sprite)) as sprite;
3.循环加载
记录当前到哪一张,判断是不是到了最后一张,是,加载第一张
二、示例代码
using system; using system.collections; using system.collections.generic; using system.io; using unityeditor; using unityengine; using unityengine.ui; public class framescontroller : monobehaviour { [system.serializable] public struct namerules { [header("基础名称(基础名字就是序列图名称中绝对相同的)")] public string basename; [header("有效数字的位数(代表排序的有效数字位数)")] public int significantdigits; [header("开始数(开始的数)")] public int start; [header("总数(一共多少张图片)")] public int count; public namerules(string _name,int _digits,int _start,int _count) { basename = _name; significantdigits = _digits; start = _start; count = _count; } } //图片类型 public enum widgettype { image, rawimage } /// [header("图片显示控件(rawimage[支持原始图片类型] or image[仅支持sprite图片])")] public widgettype imgwidget = widgettype.rawimage; //要求文件夹必须在assets/resources文件夹里面,modename填写后面到文件夹的路径 [header("模式名称(和文件夹名称相同,路径必须在resources里面)")] public string modename = "请填写文件夹路径"; [header("命名规则(序列图的命名规则)")] public namerules rules; [header("fps(一秒内显示多少张图片)")] public int framespersecond = 24; [header("循环播放(默认开启)")] public bool loop=true; [header("ui可用时自动播放(默认开启)")] public bool playonwake=true; /// <summary> /// 私有变量 /// </summary> /// /// 显示图片的ui控件 private image imagecomponent = null; private rawimage rawimgcomponent = null; private int currentframes;//当前播放的图片帧数 private float showtime = 0.0f; private float ratetime = 0.0f; private bool playing; // start is called before the first frame update void start() { initwidget(); } // update is called once per frame void update() { if (!playing) return; showtime += time.deltatime; if (showtime >= ratetime) { showtime = 0; currentframes++; if (currentframes >= rules.count) { if(loop) { currentframes = rules.start; }else { playing = false; } } if(imgwidget == widgettype.image) { imagecomponent.sprite = getcurrentsprite(); } else { rawimgcomponent.texture = getcurrenttexture2d(); } } } /// /更换播放的序列图 public void changemode(string _mode, namerules _rules, int _fps=24) { modename = _mode; rules=_rules; framespersecond = _fps; currentframes = rules.start; ratetime = 1.0f / framespersecond; if (imgwidget == widgettype.image) { imagecomponent.sprite = getcurrentsprite(); } else { rawimgcomponent.texture = getcurrenttexture2d(); } } //开始播放 public void play(bool needloop=true) { playing = true; loop = needloop; } //停止播放 public void stop() { playing = false; } private sprite getcurrentsprite() { /这个是重点,显示不出来图片的话,大概率问题在这个函数 string formatstr = "{0:d" + rules.significantdigits + "}";//保留有效数字,不够前面加0 string imagename = modename + "/" + rules.basename + string.format(formatstr, currentframes); return loadsprite(imagename); } private texture2d getcurrenttexture2d() { /这个是重点,显示不出来图片的话,大概率问题在这个函数 string formatstr = "{0:d"+ rules .significantdigits+ "}";//保留有效数字,不够前面加0 string imagename = modename+"/"+rules.basename + string.format(formatstr, currentframes); return loadtexture2d(imagename); } private texture2d loadtexture2d(string path) { return resources.load(path, typeof(texture2d)) as texture2d; } private sprite loadsprite(string path) { return resources.load(path, typeof(sprite)) as sprite; } /// <summary> /// 初始化图片显示组件 /// </summary> private void initwidget() { if(imgwidget== widgettype.image) { imagecomponent = transform.gameobject.getcomponent<image>(); if(imagecomponent==null) { editorbox("此组件上没有找到<image>!请检查后重试!"); editorstop(); } } else { rawimgcomponent = transform.gameobject.getcomponent<rawimage>(); if (rawimgcomponent == null) { editorbox("此组件上没有找到<rawimage>!请检查后重试!"); editorstop(); } } playing = playonwake; currentframes = rules.start; ratetime = 1.0f / framespersecond; if (imgwidget == widgettype.image) { imagecomponent.sprite = getcurrentsprite(); } else { rawimgcomponent.texture = getcurrenttexture2d(); } } /// <summary> /// unity编辑器的messagebox /// </summary> private void editorbox(string msg) { #if unity_editor editorutility.displaydialog("framescontroller", msg, "确认", "取消"); #endif } /// <summary> /// unity编辑器停止当前正在运行的程序 /// </summary> private void editorstop() { #if unity_editor unityeditor.editorapplication.isplaying = false; #endif } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: 高并发服务限流实践(一)