Unity中有继承与MonoBehaviour的Invoke,InvokeReperting,isInvoke,CencelInvoke,这四个方法。
Unity中的Invoke方法,InvokeReperting方法调用只能调用本脚本中的方法体,可以设置调用时间。
InvokeRepeating(string methodName, float time, float repeatRate);
//time是等待多少秒后调用,repeatRate是间隔多少秒后调用
//停止某一个Invoke
CancelInvoke(string methodName);
//停止全部Invoke
CancelInvoke();
IsInvoking是用来检测一个方法是否被调用,返回一个bool值
bool IsInvoking(string methodName);
bool IsInvoking();
事件中的Invoke和BeginInvoke方法可以用来发布任务。
其中Invoke方法是同步调用,
BeginInvoke方法是异步调用。
使用BeginInvoke方法时需要注意:1.监听此方法的只能有一个
2.需要一个参数为System.IAsyncResult 的变量
测试代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InvokeFunction : MonoBehaviour
{
public delegate void InvokeDelegate();
public static event InvokeDelegate InvokeEvent;
private void Update