Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Execution Order: yield WaitForEndOfFrame executed before Rendering / OnGUI

Discussion in 'Scripting' started by cogsysrtl, Feb 26, 2020.

  1. cogsysrtl

    cogsysrtl

    Joined:
    Feb 26, 2020
    Posts:
    2
    I just noticed that WaitForEndOfFrame does not work as I expected it to. According to the documentation the code here should be executed last, after rendering and OnGUI. But in some tests it is executed before rendering. Here is a small example script that shows the behavior:

    Code (CSharp):
    1. public class TestEndOfFrame : MonoBehaviour
    2. {
    3.     // Start is called before the first frame update
    4.     void Start()
    5.     {
    6.        
    7.     }
    8.  
    9.     // Update is called once per frame
    10.     public void Update()
    11.     {
    12.         StartCoroutine(EndOfFrameMethod());
    13.     }
    14.  
    15.     IEnumerator EndOfFrameMethod()
    16.     {
    17.         yield return new WaitForEndOfFrame();
    18.         Debug.Log($"EndOfFrameMethod: {Time.frameCount}");
    19.     }
    20.  
    21.     public void LateUpdate()
    22.     {
    23.         Debug.Log($"LateUpdate: {Time.frameCount}");
    24.     }
    25.  
    26.     private void OnPostRender()
    27.     {
    28.         Debug.Log($"OnPostRender: {Time.frameCount}");
    29.     }
    30.  
    31.     public void OnGUI()
    32.     {
    33.         Debug.Log($"OnGUI: {Time.frameCount}");
    34.     }
    35. }
    36.  
    Using this Script I get the following debug output:
    Code (CSharp):
    1. LateUpdate: 1
    2. LateUpdate: 2
    3. EndOfFrameMethod: 2
    4. EndOfFrameMethod: 2
    5. OnPostRender: 2
    6. OnGUI: 2
    7. OnGUI: 2
    8. LateUpdate: 3
    9. EndOfFrameMethod: 3
    10. OnPostRender: 3
    11. OnGUI: 3
    12. [...]
    I would expect a order like LateUpdate -> OnPostRender -> OnGUI -> EndOfFrameMethod or do I misunderstand something?

    I'm using Unity 2019.3.0f1 on Linux.
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,065
    Don't use OnGui for anything, it might even get called multiple times per frame, it's horrible, it sucks, I don't know why it's still around.

    Second, you should add a yield return null; below that Debug.Log in the IEnumerator.
     
  3. cogsysrtl

    cogsysrtl

    Joined:
    Feb 26, 2020
    Posts:
    2
    Thanks for your response. I included OnGUI because it should be the last event called befor the yield return new WaitForEndOfFrame();
    I just tried to run the script above in Unity 2019.3.0f1 on Windows and it runs like I would expect it to run. Windows results:

    Code (CSharp):
    1. LateUpdate: 1
    2. LateUpdate: 2
    3. OnPostRender: 2
    4. OnGUI: 2
    5. OnGUI: 2
    6. EndOfFrameMethod: 2
    7. EndOfFrameMethod: 2
    8. LateUpdate: 3
    9. [...]
    So it seems like there is a problem with the execution order on Linux.
     
    AcidArrow likes this.
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,065
    Fair point. Submit a bug report if you want to have a Random.value*100f; % chance of having the issue fixed in the next Random.Range(1,10000000000000000000000); units of time of your choice.
     
    cogsysrtl likes this.