延时调用(Invoke)
当我们进行简单函数的延时调用不想使用协程时,我们可以使用Invoke()函数
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NO15_Invoke : MonoBehaviour
{
//显示在每次生成Gris以后的两秒Gris开始跑动
public Animator animator;
// Start is called before the first frame update
void Start()
{
Invoke("GrisRun", 2);//两秒后调用GrisRun函数
//第一次调用CreateGris()函数在一秒以后,之后以每两秒的频率再次调用CreateGris()函数
InvokeRepeating("CreateGris", 1, 2);
//停止延时调用CreateGris()函数
CancelInvoke("CreateGris");
//不加参数停止全部演示调用
//CancelInvoke();
}
// Update is called once per frame
void Update()
{
//判断是否正在调用CreateGris()函数
print(IsInvoking("CreateGris"));
//判断是否有延时函数执行
print(IsInvoking());
}
void CreateGris()
{
Instantiate(animator.gameObject);
Invoke("GrisRun", 2);//两秒后调用GrisRun函数
}
void GrisRun()
{
animator.Play("Gris_Run");
}
}