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的用法(附代码)的细致内容,更多请关注ki4网别的相干文章!