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 position of a VisualElement?

Discussion in 'Scripting' started by unity_M0SlTBB5vVKbzA, Feb 20, 2020.

  1. unity_M0SlTBB5vVKbzA

    unity_M0SlTBB5vVKbzA

    Joined:
    Feb 14, 2020
    Posts:
    4
    How do I get the position of a VisualElement? I'm trying to get the position of elements in my OnFocus function and everything just returns 0s and NaNs?
     
  2. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    post code
     
  3. unity_M0SlTBB5vVKbzA

    unity_M0SlTBB5vVKbzA

    Joined:
    Feb 14, 2020
    Posts:
    4
    Trying to find the bounds of a group of VisualElements.
    Code (CSharp):
    1.  
    2. float[] bounds = { float.MaxValue, float.MinValue, float.MaxValue, float.MinValue };
    3. List<GraphElement> elements = graphElements.ToList();
    4. for (int i = 0; i < elements.Count; i++)
    5. {
    6.     Rect elementRect = elements[i].GetPosition(); // elements[i].contentRect? elements[i].? elements[i].layout? elements[i].worldBound?
    7.                    
    8.     Debug.Log(elementRect.x.ToString() + " " + elementRect.y.ToString());
    9.     float[] elementBounds = { elementRect.x, elementRect.x + elementRect.width, elementRect.y, elementRect.y + elementRect.height };
    10.     if (elementBounds[0] < bounds[0])
    11.         bounds[0] = elementBounds[0];
    12.     if (elementBounds[1] > bounds[1])
    13.         bounds[1] = elementBounds[1];
    14.     if (elementBounds[2] < bounds[2])
    15.         bounds[2] = elementBounds[2];
    16.     if (elementBounds[3] > bounds[3])
    17.         bounds[3] = elementBounds[3];
    18.  }
     
  4. unity_M0SlTBB5vVKbzA

    unity_M0SlTBB5vVKbzA

    Joined:
    Feb 14, 2020
    Posts:
    4
    Revisited this just now. Put a button in my UI and used it to check the state of the elements after OnFocus. It showed that VisualElements don't have any positional information until after OnFocus completes. Which is wildly inconvenient. Some more searching and I found this solution from damian:

    myElement.scheduler.Execute(() => { // some work });

    Which delays the function until after its info is ready. Then your initialization can take into consideration the positions of other elements.
     
    kernelkode likes this.
  5. SpencerLBuff

    SpencerLBuff

    Joined:
    Mar 6, 2021
    Posts:
    2
    could you explain this in a bit more detail? im running into the exact issue of getting either 0s or NaN as outputs and im really needing to be able to get the positions
     
  6. sol-erides

    sol-erides

    Joined:
    Dec 8, 2018
    Posts:
    17
    I'm sure someone more qualified could explain the details better, but I believe that the UI Toolkit's Layout Engine requires one frame to calculate all the elements' positions whenever a change to the layout is made (like when the element is first created.)