【Unity】Timer计时器属性及使用

可以代替协程完成延时操作

可以不用Update进行计时

  1. GitHub开源计时插件

    网址:https://github.com/akbiggs/UnityTimer/tree/master

    导入:URL:https://github.com/akbiggs/UnityTimer.git
    请添加图片描述
    请添加图片描述

    基本功能:

    • 创建计时器:

      Timer.Register(5f, () => Debug.Log("计时结束回调"));
      
    • 循环计时

      Timer.Register(2f, () => Debug.Log("循环计时"), isLooped: true);
      
    • 调用后取消计时器

      Timer.Cancel(timer);
      或
      timer.Canel();
      
    • 使用真实时间,不受游戏暂停影响

      Timer.Register(1f, () => Debug.Log("使用真实时间,不受游戏暂停影响"), useRealTime: true);
      
    • 暂停计时器

      Timer.Pause(timer);
      
    • 将计时器附加到 MonoBehaviour 上,以便在 MonoBehaviour 销毁时销毁计时器。

      public class CoolMonoBehaviour : MonoBehaviour {
              
              
         void Start() {
              
              
         	 this.AttachTimer(5f, () => {
              
              
              this.gameObject.transform.position = Vector3.zero;
         		});
        		}
         }
      
    • 使用回调随着时间的推移逐渐更新值onUpdate

      Timer.Register(5f,onComplete:  () => Debug.Log("计时结束回调"),onUpdate:Debug.Log("每帧回调") );
      

猜你喜欢

转载自blog.csdn.net/Xz616/article/details/135444879
今日推荐