Unity 常用脚本:定时器

public void Invoke(string methodName, float time);

摘要:Invokes the method methodName in time seconds.

以时间秒为单位调用方法methodName。

参数:

  • methodName,方法名
  • time,等待时间
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestInvoke : MonoBehaviour
{
	void Start ()
    {
        Invoke("TestInvokeFunction",5);//五秒后执行TestInvokeFunction方法
    }
    void TestInvokeFunction()
    {
        Debug.Log("TestInvokeFunction");
    }
}

 public void InvokeRepeating(string methodName, float time, float repeatRate);

 摘要:Invokes the method methodName in time seconds, then repeatedly every repeatRate seconds.

以时间秒为单位调用方法methodName,然后重复每个重复率秒。

参数:

  • methodName,方法名
  • time,等待时间
  • repeatRate,时间间隔
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestInvoke : MonoBehaviour
{
	void Start ()
    {
        //三秒后执行TestInvokeFunction方法,以后每1秒执行一次
        InvokeRepeating("TestInvokeRepeatingFunction", 3,1);
    }
    void TestInvokeRepeatingFunction()
    {
        Debug.Log("TestInvokeRepeatingFunction");
    }
}

public void CancelInvoke();

摘要:Cancels all Invoke calls on this MonoBehaviour.

取消对此单行为的所有调用。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestInvoke : MonoBehaviour
{
	void Start ()
    {
        //三秒后执行TestFunctionA方法,以后每1秒执行一次
        InvokeRepeating("TestFunctionA", 3, 1);
        //五秒后执行TestFunctionB方法,以后每2秒执行一次
        InvokeRepeating("TestFunctionB", 5, 2);
        //十秒后执行TestCancelInvoke方法
        Invoke("TestCancelInvoke",10);
    }
    void TestFunctionA()
    {
        Debug.Log("TestFunctionA");
    }
    void TestFunctionB()
    {
        Debug.Log("TestFunctionB");
    }

    void TestCancelInvoke()
    {
        Debug.Log("TestCancelInvoke");
        CancelInvoke();//取消执行所有等待执行的方法
    }
}

 public void CancelInvoke(string methodName);

摘要:Cancels all Invoke calls with name methodName on this behaviour.

取消此行为上所有带有name methodName的调用。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestInvoke : MonoBehaviour
{
	void Start ()
    {
        //三秒后执行TestFunctionA方法,以后每1秒执行一次
        InvokeRepeating("TestFunctionA", 3, 1);
        //五秒后执行TestFunctionB方法,以后每2秒执行一次
        InvokeRepeating("TestFunctionB", 5, 2);
        //十秒后执行TestCancelInvoke方法
        Invoke("TestCancelInvoke",10);
    }
    void TestFunctionA()
    {
        Debug.Log("TestFunctionA");
    }
    void TestFunctionB()
    {
        Debug.Log("TestFunctionB");
    }

    void TestCancelInvoke()
    {
        Debug.Log("TestCancelInvoke");
        CancelInvoke("TestFunctionA");//取消执行TestFunctionA方法
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值