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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Getting the Editor Position of an element?

Discussion in 'UI Toolkit' started by broots, Feb 4, 2021.

  1. broots

    broots

    Joined:
    Dec 20, 2019
    Posts:
    54
    Heya, pretty simple question. I'm creating a dropdown popup which I'd like to display underneath a UI element in an editor window like this:

    upload_2021-2-4_14-23-13.png

    However, I can't find any reliable way to get the absolute position to instantiate the dropdown window on. The world bound seems relative to the editor window, and from what I can see there's no actual way to get any sort of "absolute position".

    For reference, I'm using an editor window which simply instantiates a UI elements list view within the window:
    Code (CSharp):
    1.                
    2. newPos = ????; //How to get the correct position to instantiate the window?
    3. outfitDropdown = EditorWindow.CreateInstance<OutfitDropdownWindow>();
    4.                 outfitDropdown.ShowAsDropDown(newPos, new Vector2(200, 400));
    to create the dropdown window.

    I'm using this because the UI elements alternative to create a dropdown using a toolbar menu as shown here:
    https://github.com/Unity-Technologi...master/Assets/Examples/Editor/E05_Controls.cs
    I could not figure out how to get to work for my purposes, the documentation felt sparse in this area and didin't give me much of a clue on how I'd implement this control. In either case though, having some way to get the absolute position seems neccesary for many applications.

    Getting back to the point, how can I get the actual position of my UI element on screen or is their some other magic I can work to position this correctly?

    Cheers,
    Z.
     

    Attached Files:

  2. Nexer8

    Nexer8

    Joined:
    Dec 10, 2017
    Posts:
    271
    VisualElement.worldBound gives you the information you need in world space, such as x,y as well as width and height. You might want to schedule this a frame after the creation so that the layout has been calculated. Something like this should work
    Code (CSharp):
    1. VisualElement.schedule.Execute(() => Debug.Log("Your code here"));
     
  3. broots

    broots

    Joined:
    Dec 20, 2019
    Posts:
    54
    Unfortunately, the world bound is only relative to the parent EditorWindow, so if the editor window is only 1/2 the size of the editor then the instantiated dropdown is created in the wrong place.

    Code (CSharp):
    1.  
    2. var m = EditorWindow.GetWindow<DialogueGraph.DialogueGraph>();
    3. var newPos = new Rect(m.position.xMin + charDropdownThing.worldBound.position.x,
    4.     m.position.yMin + charDropdownThing.worldBound.position.y, 200, 400);
    5. outfitDropdown = EditorWindow.CreateInstance<OutfitDropdownWindow>();
    6.  
    This does work, but this code is not reusable at all and seems like a big ol' hack that shouldn't be neccesary. (I actually can't use this in the case I'm trying to solve because it requires a specific target window to be know ahead of time.)
     
  4. emily-StockSyn

    emily-StockSyn

    Joined:
    Aug 4, 2021
    Posts:
    2
    Bumping this as I'm also in need custom searchable drop down menu using VisualElement
     
  5. emily-StockSyn

    emily-StockSyn

    Joined:
    Aug 4, 2021
    Posts:
    2
    Just found the answer: use
    GUIUtility.GUIToScreenRect(worldBound)
     
  6. s_marcell

    s_marcell

    Joined:
    Mar 22, 2018
    Posts:
    16
    Spent half a day trying to solve this problem when I finally stumpled upon this thread. Can confirm this works!
    Maybe the docs on ShowAsDropDown() should be clarified?