Unity FixedUpdate 与 Update 的线程关系实验

先上结论: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