[简短篇] MonoBehaviour.Invok

[简短篇] MonoBehaviour.Invok

​ Invok是一个协程调用方式,通常表现为一个类似定时操作的函数。

Invok("函数名",[时间]);

注意,第一个参数是string类型,也就是说你完全可以这样做

public class Invoke : MonoBehaviour
{
    Dictionary<int, string> dic = new Dictionary<int, string>();
    // Start is called before the first frame update
    void Start()
    {
        dic.Add(0, "callThis");
        dic.Add(1, "callSec");
        dic.Add(2, "callTrd");
        for(int i= 0 ; i < dic.Count; i++)
        {
            Invoke(dic[i], 0.3f + i * 0.3f);
        }
    }
    void callThis()
    {
        Debug.Log("1");
    }

    void callSec()
    {
        Debug.Log("2");
    }
    void callTrd()
    {
        Debug.Log("3");
    }
}

结果如图:

104.gif

想反复间隔时间调用那就

InvokeRepeating("函数名", [时间], [执行后的时间间隔]);

想知道某个函数是否在计时,那就

isInvoking("函数名");

想打断计时那就

CancelInvoke("函数名");
//如果不传入参数则会直接打断所有计时

这部分涉及到协程的内容,目前暂时无法对其做分析。

posted @ 2023-07-02 22:51  ComputerEngine  阅读(12)  评论(0编辑  收藏  举报