Unity 如何实现一个定时器
【摘要】
using System;using UnityEngine; /// <summary>/// 时间单位/// </summary>public enum TimeUnit{ MILLSECOND, //毫秒 SECOND, //秒 MINUTE, //分钟 HOUR, ...
-
using System;
-
using UnityEngine;
-
-
/// <summary>
-
/// 时间单位
-
/// </summary>
-
public enum TimeUnit
-
{
-
MILLSECOND, //毫秒
-
SECOND, //秒
-
MINUTE, //分钟
-
HOUR, //小时
-
}
-
-
/// <summary>
-
/// 定时器
-
/// </summary>
-
public class Timer
-
{
-
/// <summary>
-
/// 计时结束回调事件
-
/// </summary>
-
public event Action OnEnd;
-
/// <summary>
-
/// 计时更新事件
-
/// </summary>
-
public event Action<float> OnUpdate;
-
-
//定时时长
-
private float duration;
-
//开始计时时间
-
private float beginTime;
-
//已计时间
-
private float elapsedTime;
-
//缓存时间(用于暂停)
-
private float cacheTime;
-
//是否循环
-
private bool loop;
-
//是否暂停
-
private bool isPaused;
-
//是否计时完成
-
private bool isFinished;
-
-
/// <summary>
-
/// 构造函数
-
/// </summary>
-
/// <param name="duration">定时时长</param>
-
/// <param name="timeUnit">时间单位</param>
-
/// <param name="loop">是否循环</param>
-
public Timer(float duration, TimeUnit timeUnit = TimeUnit.SECOND, bool loop = false)
-
{
-
switch (timeUnit)
-
{
-
case TimeUnit.MILLSECOND: this.duration = duration / 1000; break;
-
case TimeUnit.SECOND: this.duration = duration; break;
-
case TimeUnit.MINUTE: this.duration = duration * 60; break;
-
case TimeUnit.HOUR: this.duration = duration * 3600; break;
-
}
-
this.loop = loop;
-
beginTime = Time.realtimeSinceStartup;
-
}
-
/// <summary>
-
/// 更新定时器
-
/// </summary>
-
public void Update()
-
{
-
if(!isFinished && !isPaused)
-
{
-
elapsedTime = Time.realtimeSinceStartup - beginTime;
-
OnUpdate?.Invoke(Mathf.Clamp(elapsedTime, 0, duration));
-
if(elapsedTime >= duration)
-
{
-
OnEnd?.Invoke();
-
if (loop)
-
{
-
beginTime = Time.realtimeSinceStartup;
-
}
-
else
-
{
-
isFinished = true;
-
}
-
}
-
}
-
}
-
/// <summary>
-
/// 暂停定时器
-
/// </summary>
-
public void Pause()
-
{
-
if (!isFinished)
-
{
-
isPaused = true;
-
cacheTime = Time.realtimeSinceStartup;
-
}
-
}
-
/// <summary>
-
/// 恢复定时器
-
/// </summary>
-
public void Unpause()
-
{
-
if(!isFinished && isPaused)
-
{
-
beginTime += Time.realtimeSinceStartup - cacheTime;
-
isPaused = false;
-
}
-
}
-
/// <summary>
-
/// 停止定时器
-
/// </summary>
-
public void Stop()
-
{
-
isFinished = true;
-
}
-
}
Example:
-
Timer timer;
-
private void Start()
-
{
-
timer = new Timer(3f);
-
timer.OnEnd += () => Debug.Log("计时结束 总计时长3秒");
-
timer.OnUpdate += v => Debug.Log($"计时中 已计时长{v}秒");
-
}
-
-
private void Update()
-
{
-
timer.Update();
-
}
文章来源: coderz.blog.csdn.net,作者:CoderZ1010,版权归原作者所有,如需转载,请联系作者。
原文链接:coderz.blog.csdn.net/article/details/109058036
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)