Unity FixedUpdate 与 Update 的线程关系实验

本文通过实验验证了Unity引擎中FixedUpdate与Update方法运行在同一线程上,并探讨了FixedUpdate阻塞对Update的影响及Unity生命周期管理机制。

先上结论:FixedUpdate 与 Update 在同一个线程上。

 

实验过程:

1、打印 FixedUpdate 和 Update 的线程编号

    void FixedUpdate ()
    {
        Debug.Log ("FixedUpdate => " + Thread.CurrentThread.ManagedThreadId);
    }

    void Update ()
    {
        Debug.Log ("Update => " + Thread.CurrentThread.ManagedThreadId);
    }

得到结果如下:

由此可见,FixedUpdate 和 Update 是运行在同一个线程上的。这样我们在处理 FixedUpdate 的时候,就不需要考虑和 Update 之间线程同步的问题了。

 

2、再来,我们把 FixedUpdate 帧率调低到 1 秒

结果同样是在一个线程上

 

3、我们再做个坏事,先把 FixedUpdate 的帧率调回到 0.02,然后在 FixedUpdate 的时候执行 Thread.Sleep(1000)

    void FixedUpdate ()
    {
        Debug.Log ("FixedUpdate => " + Thread.CurrentThread.ManagedThreadId);
        Thread.Sleep(1000);
    }

    void Update ()
    {
        Debug.Log ("Update => " + Thread.CurrentThread.ManagedThreadId);
    }

再执行的时候发现——Update 也同时被 FixedUpdate 阻塞了,整个 Unity 软件的 UI 都一卡一卡的……

 

4、再来,我们不睡 FixedUpdate 了,换成睡 Update

    void FixedUpdate ()
    {
        Debug.Log ("FixedUpdate => " + Thread.CurrentThread.ManagedThreadId);
    }

    void Update ()
    {
        Debug.Log ("Update => " + Thread.CurrentThread.ManagedThreadId);
        Thread.Sleep (1000);
    }

看看结果

线程唤醒后,Unity拼命的执行 FixedUpdate,然后再执行一次 Update。

 

由此可以猜想:Unity 在整个生命周期中可能建了一个托管代码执行队列,通过生命周期管理器往这个队列中添加执行方法的 delegate,然后启动一个托管线程循环的取队列中的方法(delegate)并执行。

 

希望这个实验对您有帮助。

转载于:https://www.cnblogs.com/softcat/p/6136290.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值