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. Dismiss Notice

Question NextFrameAsync vs EndOfFrameAsync

Discussion in 'UI Toolkit' started by dlorre, Aug 3, 2023.

  1. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    So, I have this code:

    Code (csharp):
    1.  
    2.         private void changeMode(FocusMode focus)
    3.         {
    4.  
    5.             if (focus == activeFocus)
    6.             {
    7.                 return;
    8.             }
    9.  
    10.  
    11.             panels[(int)activeFocus].RemoveFromClassList("mode-selected");
    12.            activeFocus = focus;
    13.            panels[(int)activeFocus].AddToClassList("mode-selected");
    14.             ...
    15.  
    16.             _ = modeChange();
    17.  
    18.         }
    19.        
    20.        private async Awaitable modeChange()
    21.         {
    22.             await Awaitable.EndOfFrameAsync();  
    23.             selectFocus(activeFocus);
    24.            ...
    25.        }
    26.  
    Basically what it does is wait for the panel to be visible ("display: none" removed) and then focus on the first visible element.

    That works but when I used Awaitable.NextFrameAsync() it didn't work, the panel was not visible yet. Isn't it counter-intuitive that the changes are performed in EndOfFrameAsync() and not in NextFrameAsync()?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,843
    UI Toolkit visual elements are updated towards the end of the player loop. I imagine NextFrameAsync is taking place before visual elements are updated, while EndOfFrameAsync does take place after.

    So it depends when this code runs. If it's running as the visual element is updated, NextFrameAsync is before the next time visual elements will be updated.
     
  3. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    Yeah I think so too. You'd think that the end of the frame is before the next frame though.
     
  4. kdchabuk

    kdchabuk

    Joined:
    Feb 7, 2019
    Posts:
    47
    dlorre likes this.