Unity 如何实现一个定时器

举报
CoderZ1010 发表于 2022/09/25 05:18:37 2022/09/25
【摘要】 using System;using UnityEngine; /// <summary>/// 时间单位/// </summary>public enum TimeUnit{ MILLSECOND, //毫秒 SECOND, //秒 MINUTE, //分钟 HOUR, ...

  
  1. using System;
  2. using UnityEngine;
  3. /// <summary>
  4. /// 时间单位
  5. /// </summary>
  6. public enum TimeUnit
  7. {
  8. MILLSECOND, //毫秒
  9. SECOND, //秒
  10. MINUTE, //分钟
  11. HOUR, //小时
  12. }
  13. /// <summary>
  14. /// 定时器
  15. /// </summary>
  16. public class Timer
  17. {
  18. /// <summary>
  19. /// 计时结束回调事件
  20. /// </summary>
  21. public event Action OnEnd;
  22. /// <summary>
  23. /// 计时更新事件
  24. /// </summary>
  25. public event Action<float> OnUpdate;
  26. //定时时长
  27. private float duration;
  28. //开始计时时间
  29. private float beginTime;
  30. //已计时间
  31. private float elapsedTime;
  32. //缓存时间(用于暂停)
  33. private float cacheTime;
  34. //是否循环
  35. private bool loop;
  36. //是否暂停
  37. private bool isPaused;
  38. //是否计时完成
  39. private bool isFinished;
  40. /// <summary>
  41. /// 构造函数
  42. /// </summary>
  43. /// <param name="duration">定时时长</param>
  44. /// <param name="timeUnit">时间单位</param>
  45. /// <param name="loop">是否循环</param>
  46. public Timer(float duration, TimeUnit timeUnit = TimeUnit.SECOND, bool loop = false)
  47. {
  48. switch (timeUnit)
  49. {
  50. case TimeUnit.MILLSECOND: this.duration = duration / 1000; break;
  51. case TimeUnit.SECOND: this.duration = duration; break;
  52. case TimeUnit.MINUTE: this.duration = duration * 60; break;
  53. case TimeUnit.HOUR: this.duration = duration * 3600; break;
  54. }
  55. this.loop = loop;
  56. beginTime = Time.realtimeSinceStartup;
  57. }
  58. /// <summary>
  59. /// 更新定时器
  60. /// </summary>
  61. public void Update()
  62. {
  63. if(!isFinished && !isPaused)
  64. {
  65. elapsedTime = Time.realtimeSinceStartup - beginTime;
  66. OnUpdate?.Invoke(Mathf.Clamp(elapsedTime, 0, duration));
  67. if(elapsedTime >= duration)
  68. {
  69. OnEnd?.Invoke();
  70. if (loop)
  71. {
  72. beginTime = Time.realtimeSinceStartup;
  73. }
  74. else
  75. {
  76. isFinished = true;
  77. }
  78. }
  79. }
  80. }
  81. /// <summary>
  82. /// 暂停定时器
  83. /// </summary>
  84. public void Pause()
  85. {
  86. if (!isFinished)
  87. {
  88. isPaused = true;
  89. cacheTime = Time.realtimeSinceStartup;
  90. }
  91. }
  92. /// <summary>
  93. /// 恢复定时器
  94. /// </summary>
  95. public void Unpause()
  96. {
  97. if(!isFinished && isPaused)
  98. {
  99. beginTime += Time.realtimeSinceStartup - cacheTime;
  100. isPaused = false;
  101. }
  102. }
  103. /// <summary>
  104. /// 停止定时器
  105. /// </summary>
  106. public void Stop()
  107. {
  108. isFinished = true;
  109. }
  110. }

Example:


  
  1. Timer timer;
  2. private void Start()
  3. {
  4. timer = new Timer(3f);
  5. timer.OnEnd += () => Debug.Log("计时结束 总计时长3秒");
  6. timer.OnUpdate += v => Debug.Log($"计时中 已计时长{v}秒");
  7. }
  8. private void Update()
  9. {
  10. timer.Update();
  11. }

文章来源: coderz.blog.csdn.net,作者:CoderZ1010,版权归原作者所有,如需转载,请联系作者。

原文链接:coderz.blog.csdn.net/article/details/109058036

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。