Search Unity

Feedback EditorWindow position not correct in CreateGUI

Discussion in 'UI Toolkit' started by CDF, Feb 9, 2023.

  1. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    Using Unity 2021.3.5. Bit frustrating that in the method:
    CreateGUI


    the windows position is not up-to-date.

    This makes it more complex than necessary to create split views, where the fixedPaneStartDimension is derived from the windows size.

    example:
    Code (CSharp):
    1.  
    2. class MyWindow : EditorWindow {
    3.  
    4.     void CreateGUI() {
    5.  
    6.         Debug.Log("Create GUI position width:" + position.width); //320
    7.  
    8.         var splitView = new TwoPaneSplitView(0, Mathf.Round(position.width * 0.7f), TwoPaneSplitViewOrientation.Horizontal);
    9.  
    10.         var v1 = new VisualElement();
    11.         var v2 = new VisualElement();
    12.  
    13.         splitView.Add(v1);
    14.         splitView.Add(v2);
    15.  
    16.         rootVisualElement.Add(splitView);
    17.  
    18.         void InitSplitViewSize(GeometryChangedEvent evt) {
    19.  
    20.             Debug.Log("After Geometry Changed position width: " + position.width); //1000
    21.  
    22.             splitView.fixedPaneInitialDimension = Mathf.Round(position.width * 0.7f);
    23.             splitView.UnregisterCallback<GeometryChangedEvent>(InitSplitViewSize);
    24.         }
    25.  
    26.         splitView.RegisterCallback<GeometryChangedEvent>(InitSplitViewSize);
    27.     }
    28. }
    29.  
    Is this expected?
     
    krubbles likes this.
  2. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    Looks like the
    CreateGUI
    method is called too quickly.

    Code (CSharp):
    1. private static void Open() {
    2.  
    3.     Debug.Log("Open");
    4.  
    5.     MyWindow window = GetWindow<MyWindow>("MyWindow");
    6.  
    7.     Debug.Log("After Create: " + window.position.width); //1000
    8.  
    9.     window.minSize = new Vector2(300, 200);
    10.  
    11.     Debug.Log("After Set MinSize: " + window.position.width); //1000
    12. }
    outputs:

    1. Open
    2. Create GUI position width: 320
    3. After Create: 1000
    4. After Set Min Size: 1000
    5. After Geometry Changed position width: 1000

    Also worth noting, this only happens on the initial show of the window.
    If the window is already open, CreateGUI gets the correct position up front
     
    krubbles likes this.