Search Unity

Bug Positioning problem using PopupWindowContent

Discussion in 'UI Toolkit' started by Destruxion, Dec 3, 2022.

  1. Destruxion

    Destruxion

    Joined:
    Jun 5, 2019
    Posts:
    37
    I have encountered weird position behaviour when trying to open a PopupWindowContent using the right click context menu.

    I took the example from Unitys website:

    https://docs.unity3d.com/Manual/UIE-create-a-popup-window.html
    https://github.com/Unity-Technologi...ples/tree/master/create-a-popup-window/Editor

    ...and modified the PopupExample.cs script to add a context menu.

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine.UIElements;
    3. using PopupWindow = UnityEditor.PopupWindow;
    4.  
    5. public class PopupExample : EditorWindow
    6. {
    7.     Button button;
    8.  
    9.     // Add menu item
    10.     [MenuItem("Example/Popup Example")]
    11.     static void Init()
    12.     {
    13.         EditorWindow window = EditorWindow.CreateInstance<PopupExample>();
    14.         window.Show();
    15.     }
    16.  
    17.     private void CreateGUI()
    18.     {
    19.         var visualTreeAsset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>( "Assets/Next/Experimental/Node Graph/Scripts/_Editor/PopupExample.uxml" );
    20.         visualTreeAsset.CloneTree(rootVisualElement);
    21.  
    22.         button = rootVisualElement.Q<Button>();
    23.         button.clicked += OnClick;
    24.  
    25.         button.AddManipulator( new ContextualMenuManipulator( ( ContextualMenuPopulateEvent e ) =>
    26.         {
    27.             e.menu.AppendAction( "Show Popup", OnShowPopup, DropdownMenuAction.AlwaysEnabled, this );
    28.         } ) );
    29.     }
    30.  
    31.     private void OnClick()
    32.     {
    33.         PopupWindow.Show( button.worldBound, new PopupContentExample() );
    34.     }
    35.  
    36.     private void OnShowPopup( DropdownMenuAction action )
    37.     {
    38.         PopupWindow.Show( button.worldBound, new PopupContentExample() );
    39.     }
    40. }
    Now the fun part:
    • Left click on the button properly positions the popup right under the button element.
    • Right click -> context menu -> Shop Popup
      • Works find if Unitys game view is NOT DOCKED
      • Positions the popup in the game view if it IS DOCKED
        • There is some randomness regarding this behaviour. Sometimes the popup jumps to the upper left corner of the console window, which is also docked in my layout.
    Popup Bugged.png Popup OK.png

    Unity 2022.2.0b16
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,291
  3. Destruxion

    Destruxion

    Joined:
    Jun 5, 2019
    Posts:
    37
    Done. I could not include the complete project, but attached all scripts required to reproduce the problem.
     
    karl_jones likes this.