【Unity】Timer properties and usage

Can replace coroutine to complete delayed operations

You can do timing without Update

  1. GitHub open source timing plugin

    URL: https://github.com/akbiggs/UnityTimer/tree/master

    Import: URL: https://github.com/akbiggs/UnityTimer.git
    Please add image description
    Please add image description

    basic skills:

    • Create a timer:

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

      Timer.Register(2f, () => Debug.Log("循环计时"), isLooped: true);
      
    • Cancel timer after calling

      Timer.Cancel(timer);
      或
      timer.Canel();
      
    • Uses real time and is not affected by game pauses

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

      Timer.Pause(timer);
      
    • Attach a timer to the MonoBehaviour so that the timer is destroyed when the MonoBehaviour is destroyed.

      public class CoolMonoBehaviour : MonoBehaviour {
              
              
         void Start() {
              
              
         	 this.AttachTimer(5f, () => {
              
              
              this.gameObject.transform.position = Vector3.zero;
         		});
        		}
         }
      
    • Use callbacks to gradually update values ​​over timeonUpdate

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

Guess you like

Origin blog.csdn.net/Xz616/article/details/135444879