计时器效果图:
timer用法:
第一种:脚本加到物体上,勾选自动计时。
第二种:脚本加到物体上,调用timer.start()方法启动。
第三种:代码中动态添加timer脚本。
using unityengine;public class timertest : monobehaviour { private void start () { // 创建一个timer并开始计时 gameobject.addcomponent<timer>().start(1.5f, ontimeup); // 倒计时3秒 gameobject.addcomponent<timer>().start(1, 3, oncd, oncdend); // 无限计数(repeatcount为<=0时 无限重复) gameobject.addcomponent<timer>().start(1, -1, oncount, null); // timer api timer timer = gameobject.addcomponent<timer>(); timer.delay = 10;// 延迟10秒开始 timer.start(); // 开始计时 timer.stop(); // 暂停计时 timer.reset(); // 重置已计时的时间和次数 timer.restart();// 重新开始计时 reset() + start() } /// <summary> 正常计时 </summary> private void ontimeup(timer timer) { print("计时完成"); } /// <summary> 倒计时间隔 </summary> private void oncd(timer timer) { print(timer.repeatcount - timer.currentcount); // 3, 2, 1 } /// <summary> 倒计时结束 </summary> private void oncdend(timer timer) { print(timer.repeatcount - timer.currentcount); // 0 } /// <summary> 无限计数 </summary> private void oncount(timer timer) { print(timer.currentcount); // 1, 2, 3…… }}
timer api:
// 开始/继续计时public void start() {}// 暂停计时public void stop() {}// 停止timer并重置数据public void reset() {}// 重置数据并重新开始计时public void restart() {}// 开始计时 time时间(秒) oncomplete(timer timer)计时完成回调事件public void start(float time, timercallback oncomplete) {}// 开始计时 interval计时间隔 repeatcount重复次数 oncomplete(timer timer)计时完成回调事件public void start(float interval, int repeatcount, timercallback oncomplete) {}// 开始计时 interval计时间隔 repeatcount重复次数// oninterval(timer timer)计时间隔回调事件// oncomplete(timer timer)计时完成回调事件public void start(float interval, int repeatcount, timercallback oninterval, timercallback oncomplete) {}
timer.cs
using unityengine;using unityengine.events;/// <summary>/// 计时器/// <para>zhangyu 2018-04-08</para>/// </summary>public class timer : monobehaviour { // 延迟时间(秒) public float delay = 0; // 间隔时间(秒) public float interval = 1; // 重复次数 public int repeatcount = 1; // 自动计时 public bool autostart = false; // 自动销毁 public bool autodestory = true; // 当前时间 public float currenttime = 0; // 当前次数 public int currentcount = 0; // 计时间隔 public unityevent onintervalevent; // 计时完成 public unityevent oncompleteevent; // 回调事件代理 public delegate void timercallback(timer timer); // 上一次间隔时间 private float lasttime = 0; // 计时间隔 private timercallback onintervalcall; // 计时结束 private timercallback oncompletecall; private void start () { enabled = autostart; } private void fixedupdate () { if (!enabled) return; addinterval(time.deltatime); } /// <summary> 增加间隔时间 </summary> private void addinterval(float deltatime) { currenttime += deltatime; if (currenttime < delay) return; if (currenttime - lasttime >= interval) { currentcount++; lasttime = currenttime; if (repeatcount <= 0) { // 无限重复 if (currentcount == int.maxvalue) reset(); if (onintervalcall != null) onintervalcall(this); if (onintervalevent != null) onintervalevent.invoke(); } else { if (currentcount < repeatcount) { //计时间隔 if (onintervalcall != null) onintervalcall(this); if (onintervalevent != null) onintervalevent.invoke(); } else { //计时结束 stop(); if (oncompletecall != null) oncompletecall(this); if (oncompleteevent != null) oncompleteevent.invoke(); if (autodestory && !enabled) destroy(this); } } } } /// <summary> 开始/继续计时 </summary> public void start() { enabled = autostart = true; } /// <summary> 开始计时 </summary> /// <param name="time">时间(秒)</param> /// <param name="oncomplete(timer timer)">计时完成回调事件</param> public void start(float time, timercallback oncomplete) { start(time, 1, null, oncomplete); } /// <summary> 开始计时 </summary> /// <param name="interval">计时间隔</param> /// <param name="repeatcount">重复次数</param> /// <param name="oncomplete(timer timer)">计时完成回调事件</param> public void start(float interval, int repeatcount, timercallback oncomplete) { start(interval, repeatcount, null, oncomplete); } /// <summary> 开始计时 </summary> /// <param name="interval">计时间隔</param> /// <param name="repeatcount">重复次数</param> /// <param name="oninterval(timer timer)">计时间隔回调事件</param> /// <param name="oncomplete(timer timer)">计时完成回调事件</param> public void start(float interval, int repeatcount, timercallback oninterval, timercallback oncomplete) { this.interval = interval; this.repeatcount = repeatcount; onintervalcall = oninterval; oncompletecall = oncomplete; reset(); enabled = autostart = true; } /// <summary> 暂停计时 </summary> public void stop() { enabled = autostart = false; } /// <summary> 停止timer并重置数据 </summary> public void reset(){ lasttime = currenttime = currentcount = 0; } /// <summary> 重置数据并重新开始计时 </summary> public void restart() { reset(); start(); }}
timereditor.cs
using unityeditor;using unityengine;/// <summary>/// 计时器 编辑器/// <para>zhangyu 2018-04-08</para>/// </summary>[caneditmultipleobjects][customeditor(typeof(timer))]public class timereditor : editor { public override void oninspectorgui() { timer script = (timer)target; // 重绘gui editorgui.beginchangecheck(); // 公开属性 drawproperty("delay", "延迟时间(秒)"); drawproperty("interval", "间隔时间(秒)"); drawproperty("repeatcount", "重复次数"); if (script.repeatcount <= 0) editorguilayout.labelfield(" ", "<=0 时无限重复", guilayout.expandwidth(true)); editorguilayout.beginhorizontal(); drawproperty("autostart", "自动计时"); drawproperty("autodestory", "自动销毁"); editorguilayout.endhorizontal(); // 只读属性 gui.enabled = false; drawproperty("currenttime", "当前时间(秒)"); drawproperty("currentcount", "当前次数"); gui.enabled = true; // 回调事件 drawproperty("onintervalevent", "计时间隔事件"); drawproperty("oncompleteevent", "计时完成事件"); if (editorgui.endchangecheck()) serializedobject.applymodifiedproperties(); } private void drawproperty(string property, string label) { editorguilayout.propertyfield(serializedobject.findproperty(property), new guicontent(label), true); }}
相关文章:
用pear::benchmarking之timer实现php程序计时
如何使用纯php实现定时器任务(timer),定时器timer
相关视频:
玩转javascript之有声计算器实例
以上就是简要分析unity计时器脚本timer的用法(附代码)的详细内容。