Contents

延时功能的几种实现方式

Contents

1、Update函数

  float m_timer = 0;
    void Update()
    {
        m_timer += Time.time;
        if (m_timer >= 5)//延时五秒钟调用B();
        {
            ShowB();
            m_timer = 0;
        }
    }
    private void B()
    {
    }

2、Invoke函数

private void A(){
    Invoke("B",5f);
    //在A方法中延迟五秒调用B方法
}
private void B(){

}

3、Coroutine 协程

  private IEnumerator A()
    {
        yield return new WaitForSeconds(5);
        B();
    }

    private void B()
    {
    }

4、DOTween

  private void A()
    {
        GameObject go;
        go.transform.DOMoveZ(0.1f, 5f)
            .OnComplete(new TweenCallback(B));
    }
    private void B()
    {
    }

5、VP_Timer

vp_Timer.Handle Timer = new vp_Timer.Handle();
vp_Timer.In(5, new vp_Timer.Callback(() =>{Timer.Cancel();B(); }), Timer);