Unity计时器功能实现示例
程序员文章站
2022-03-03 08:42:41
目录demo展示介绍计时器功能unity计时器demo展示介绍游戏中有非常多的计时功能,比如:各种cd,以及需要延时调用的方法;一般实现有一下几种方式:1.手动计时float persisttime...
unity计时器
demo展示
介绍
游戏中有非常多的计时功能,比如:各种cd,以及需要延时调用的方法;
一般实现有一下几种方式:
1.手动计时
float persisttime = 10f float starttime = time.time; if(time.time - starttime > persisttime) { debug.log("计时结束"); } float curtime = 0; curtime += time.deltatime; if(curtime > persisttime) { debug.log("计时结束"); }
2.协程
private float persisttime = 10f; ienumerator delayfunc() { yield return persisttime; debug.log("计时结束"); } private void start() { startcoroutine(delayfunc()); }
3.invoke回调
private void start() { invoke("delayfunc", persisttime); }
计时器功能
计时是为了到特定的时间,执行某个功能或方法;
计时器(timer):设计了计时暂停,计时重置,计时开始方法,计时中方法,计时结束方法,固定间隔调用方法,计时器可设置复用或单次;
计时管理器(timerma):负责倒计时,以及执行计时器方法;
代码:
using system; using system.collections.generic; using unityengine; using object = system.object; public class timer { public delegate void intervalact(object args); //总时间和当前持续时间 private float curtime = 0; private float totaltime = 0; //激活 public bool isactive; //计时结束是否销毁 public bool isdestroy; //是否暂停 public bool ispause; //间隔事件和间隔事件——dot private float intervaltime = 0; private float curinterval = 0; private intervalact oninterval; private object args; //进入事件 public action onenter; private bool isonenter = false; //持续事件 public action onstay; //退出事件 public action onend; public timer(float totaltime, bool isdestroy = true, bool ispause = false) { curtime = 0; this.totaltime = totaltime; isactive = true; this.isdestroy = isdestroy; this.ispause = ispause; timerma.i.addtimer(this); } public void run() { //暂停计时 if (ispause || !isactive) return; if (onenter != null) { if (!isonenter) { isonenter = true; onenter(); } } //持续事件 if (onstay != null) onstay(); curtime += time.deltatime; //间隔事件 if (oninterval != null) { curinterval += time.deltatime; if (curinterval > intervaltime) { oninterval(args); curinterval = 0; } } //计时结束 if (curtime > totaltime) { curtime = 0; isactive = false; if (onend != null) { onend(); } } } //设置间隔事件 public void setinterval(float interval, intervalact intervalfunc, object args = null) { this.intervaltime = interval; oninterval = intervalfunc; curinterval = 0; this.args = args; } //重置计时器 public void reset() { curtime = 0; isactive = true; ispause = false; curinterval = 0; isonenter = false; } //获取剩余时间 public float getremaintime() { return totaltime - curtime; } }
using system; using system.collections; using system.collections.generic; using unityengine; public class timerma : monobehaviour { #region 单例 private static timerma instance; timerma() {} public static timerma i { get { if (instance == null) instance = new timerma(); return instance; } } #endregion private list<timer> timerlist; private void awake() { instance = this; timerlist = new list<timer>(); } public void addtimer(timer t) { timerlist.add(t); } void update() { for (int i = 0; i < timerlist.count;) { timerlist[i].run(); //计时结束,且需要销毁 if(!timerlist[i].isactive && timerlist[i].isdestroy) timerlist.removeat(i); else ++i; } } }
测试计时器
using system.collections; using system.collections.generic; using unityengine; using unityengine.ui; using object = system.object; public class test : monobehaviour { public text mtext1; public text mtext2; private timer timer; private int count = 0; void start() { timer = new timer(5f,false); timer.setinterval(1f, oninterval); timer.onenter = onstart; timer.onend = onexit; } void update() { debug.log(count); mtext1.text = timer.getremaintime().tostring("f2"); if (input.getkeydown(keycode.a)) { if (!timer.ispause) { timer.ispause = true; mtext2.text = "暂停计时"; } } if (input.getkeydown(keycode.s)) { if (timer.ispause) { timer.ispause = false; mtext2.text = "取消暂停计时"; } } if (input.getkeydown(keycode.d)) { timer.reset(); mtext2.text = "重置计时"; } } private void onstart() { mtext2.text = "开始计时"; } private void onexit() { mtext2.text = "结束计时"; } private void oninterval(object value) { count++; mtext2.text = $"间隔事件调用{count}"; } }
到此这篇关于unity计时器功能实现示例的文章就介绍到这了,更多相关unity计时器内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!