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

How to get the screen position of a child visual element

Discussion in 'UI Toolkit' started by AbsoluteTundra, Mar 10, 2020.

  1. AbsoluteTundra

    AbsoluteTundra

    Joined:
    Sep 25, 2013
    Posts:
    7
    Hey All,

    I'm trying to get the screen position of a child visual element in my editor window.
    I currently use:

    List<Vector3> points = new List<Vector3>();

    points.Add(nodes.ElementAt(0).Value.root.Q("ConnectionPointsContainer").Q("ConnectionOut").Q<Button>().worldBound.center);
    points.Add(nodes.ElementAt(1).Value.root.Q("ConnectionPointsContainer").Q("ConnectionIn").Q<Button>().worldBound.center);
    Handles.color = Color.red;
    Handles.DrawAAPolyLine(5, points.ToArray());


    But for some reason, the y position of the line does not match with the visual element.
    The image below shows the line being drawn. the line should be going from the small knob on the left node to the small knob on the right node.
    UnityFormQuestionPicture.png

    Any help would be appreciated
     
  2. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    737
    Hello,

    In which callback are you using Handles to draw this line?
     
  3. AbsoluteTundra

    AbsoluteTundra

    Joined:
    Sep 25, 2013
    Posts:
    7
    This code is inside the OnGUI function of my editor window
     
  4. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    737
    Ok. I suspect this is an issue with coordinate spaces.

    VisualElement.worldBound in this context returns a point in the space of the window.
    However inside EditorWindow.OnGUI() the current coordinate space is relative to the area dedicated to the contents of the window.

    Can you try to convert points first by calling GUIUtility.ScreenToGUIPoint?
     
  5. AbsoluteTundra

    AbsoluteTundra

    Joined:
    Sep 25, 2013
    Posts:
    7
    That gives a worse result than before
    If I do
    GUIUtility.ScreenToGUIPoint(connectedNodeA.root.Q("ConnectionPointsContainer").Q("ConnectionOut").Q<Button>().worldBound.center)

    The converted point is out of the screen view
     
  6. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,203
    You might need to use the
    rootVisualElement
    's worldBound.top and subtract that from the worldBound.center of your element. This is to account for the window tab(s) bar at the top of the window.

    Better yet, don't use both OnGUI()/IMGUI and OnEnable()/UIElements at the same time. You can have an IMGUIContainer on top of your graph view area/viewport and draw the edges in that containers' gui loop (passed in its constructor as a lambda). Top put an element (the IMGUIContainer) on top of another element, use these styles:
    Code (CSharp):
    1. position: absolute;
    2. top: 0;
    3. left: 0;
    4. right: 0;
    5. bottom: 0;
    Also, make sure the IMGUIContainer doesn't eat all the mouse events by setting:
    Code (CSharp):
    1. myimguiContainer.pickingMode = PickingMode.Ignore;
    Better still, give our custom geometry drawing API a try and draw the lines directly in UIElements:
    https://docs.unity3d.com/2019.3/Doc...ents.VisualElement-generateVisualContent.html

    GraphView implementation of its edges:
    https://github.com/Unity-Technologi...master/Modules/GraphViewEditor/EdgeControl.cs
     
  7. AbsoluteTundra

    AbsoluteTundra

    Joined:
    Sep 25, 2013
    Posts:
    7
    Thanks for the reply using yMin(Since worldbound.top is deprecated) wields the correct result I was looking for!
    I will also take a look at generateVisualContent & try to remove all IMGUI code from my editor window script.
    Which was my plan anyway when I started moving my tool from IMGUI to UIElements.

    Thanks again for the reply!