Search Unity

Question How can i force a re-layout after manually adding elements to a dialog?

Discussion in 'UI Toolkit' started by kork, Jun 2, 2020.

  1. kork

    kork

    Joined:
    Jul 14, 2009
    Posts:
    280
    I have a dialog which has a fixed upper part containing text and a dynamic lower part with options that the player can take. These options differ every time so I create the buttons manually creating them and adding them to a container element like this:

    Code (CSharp):
    1.            
    2. // get a reference to the container            
    3.            _optionsContainer = _storyDialog.Q<VisualElement>("options");
    4. // clear it
    5.             _optionsContainer.Clear();
    6. // add buttons for each option
    7.             foreach (var option in command.Descriptor.options)
    8.             {
    9.                 var button = new Button(() => { ChooseStoryOption(option); });
    10.                 _optionsContainer.Add(button);
    11.             }
    12. // show the dialog.
    13.             _storyDialog.visible = true;
    14.  
    However when the dialog shows the layout is really quite broken:
    layout1.png

    Only after i manually drag the viewport size in the Unity-editor the dialog properly re-layouts and looks the way it should:

    layout2.png

    I am not sure if this is a bug or if I am just not using the system correctly. How can I force a proper re-layout of the whole dialog after I modified the UIElement structure?
     
  2. uMathieu

    uMathieu

    Unity Technologies

    Joined:
    Jun 6, 2017
    Posts:
    398
    This is a issue in the current version of the runtime preview package, it will be fixed in the next release. Thanks for trying it out!
     
  3. kork

    kork

    Joined:
    Jul 14, 2009
    Posts:
    280
    I see, thank you. Good to know that I used the system correctly :)