延迟函数

Destory函数:

复制代码
public static void Destroy(Object obj, [DefaultValue("0.0F")] float t);

Invoke函数:

复制代码
public void Invoke(string methodName, float time);

挂个空物体测试一下:

复制代码
public class DelayTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Invoke("TestInvoke",5);
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.A))transform.gameObject.SetActive(false);

        if(Input.GetKeyDown(KeyCode.B))Destroy(this);

        if(Input.GetKeyDown(KeyCode.C))Destroy(this.gameObject);
    }

    void TestInvoke()
    {
        transform.gameObject.SetActive(true);
        Debug.Log("TestInvoke");
    }
}

发现按下A键使物体失活,延迟函数Invoke仍会执行,但当前脚本或者脚本的游戏物体被销毁时,Invoke不会执行

我们可以通过CancelInvoke函数来取消Invoke:

复制代码
public void CancelInvoke();

无参情况下是取消当前代码内所有Invoke函数

复制代码
public void CancelInvoke(string methodName);

InvokeRepeating:重复调用Invoke

cs 复制代码
public void InvokeRepeating(string methodName, float time, float repeatRate);

time表示多少秒后执行methodName

repeatRate表示重复执行methodName的时间间隔

相关推荐
weixin_424294677 天前
Unity 调用Steamworks API 的 SteamUserStats.RequestCurrentStats()报错
unity·游戏引擎·steamwork
HoFunGames7 天前
Unity小地图,Easy Minimap System MT-GPS插件
unity·游戏引擎
wy3258643648 天前
Unity 新输入系统InputSystem(基本操作)
unity·c#·游戏引擎
WarPigs8 天前
着色器multi_compile笔记
unity·着色器
ECHO飞跃 0128 天前
Unity2019 本地推理 通义千问0.5-1.5B微调导入
人工智能·深度学习·unity·llama
Unity游戏资源学习屋8 天前
【Unity UI资源包】GUI Pro - Casual Game 专为休闲手游打造的专业级UI资源包
ui·unity
冰凌糕8 天前
Unity3D Shader 顶点法线外扩实现描边效果
unity
小菱形_8 天前
【Unity】TimeLine
unity·游戏引擎
小贺儿开发9 天前
Unity3D 自动化物流分拣模拟
运维·科技·unity·自动化·人机交互·传送带·物流分拣
EQ-雪梨蛋花汤9 天前
【3D可视化】基于 Unity 的智慧体育馆三维信息可视化大屏实践
3d·unity·信息可视化