Search Unity

Question Difficulties with creating a modal editor window without extra large height

Discussion in 'UI Toolkit' started by Xelnath, Jun 30, 2022.

  1. Xelnath

    Xelnath

    Joined:
    Jan 31, 2015
    Posts:
    402
    I was creating a modal popup window with a small canvas: upload_2022-6-29_19-10-57.png

    However, when I instantiated it via:

    window.ShowModalUtility();

    It would expand super tall:

    upload_2022-6-29_19-10-45.png

    In order to resolve this, I had to add a wrapper visual element inside of my UXML file and query it first. This seems.... weird to me.



    Code (CSharp):
    1.             TimelineEditorModalWindow window = ScriptableObject.CreateInstance(typeof(TimelineEditorModalWindow)) as TimelineEditorModalWindow;
    2.             window.PreloadGUI();
    3.            
    4.             var rootWrapper = window.rootVisualElement.Q<VisualElement>("rootWrapper");
    5.             window.position = window.position.CenterInsideOf(m_parentContainer.RootWindow.position).WithHeight(rootWrapper.resolvedStyle.height);
    6.  
    7.  
    8. public static Rect CenterInsideOf( this Rect source, Rect target )
    9. {
    10.     var y = target.y + (target.height - source.height) / 2;
    11.     var x = target.x + (target.width - source.width) / 2;
    12.     return new Rect( x, y, source.width, source.height );
    13. }
    14.  
    15. public static Rect WithHeight( this Rect source, float height )
    16. {
    17.     return new Rect( source.x, source.y, source.width, height );
    18. }
    19.