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. Dismiss Notice

Question Get the world position of an UI element

Discussion in 'UI Toolkit' started by BeTechnology, Nov 2, 2022.

  1. BeTechnology

    BeTechnology

    Joined:
    Jul 12, 2020
    Posts:
    7
    Hello,

    I want to put my character (a game object) at the red dot position (who is a visual element), how get the world position of a visual element ? position.PNG
     
  2. CookieStealer2

    CookieStealer2

    Joined:
    Jun 25, 2018
    Posts:
    119
    Try:

    Code (CSharp):
    1. myVisualElement.worldTransform.GetPosition()
     
  3. BeTechnology

    BeTechnology

    Joined:
    Jul 12, 2020
    Posts:
    7
    Thanks I try it but I don't got the position of the red dot

    Code (CSharp):
    1.  if (player != null && playerPosition != null)
    2.         {  
    3.             var pos = playerPosition.worldTransform.GetPosition();
    4.             var positionObject = Camera.main.ScreenToWorldPoint(pos);
    5.             print(positionObject);
    6.             this.player.transform.position = new Vector3(positionObject.x, positionObject.y);
    7.         }
    world.PNG
     
  4. BeTechnology

    BeTechnology

    Joined:
    Jul 12, 2020
    Posts:
    7
    Seems better if I apply -positionObject.y
    Code (CSharp):
    1.           var pos = playerPosition.worldTransform.GetPosition();
    2.             var positionObject = Camera.main.ScreenToWorldPoint(pos);
    3.             print(positionObject);
    4.             player.transform.position = new Vector3(positionObject.x, -positionObject.y);
    better.PNG
     
  5. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    515
    UI toolkit is using ui coordinate that start from the top left corner, as most ui framework do.
     
  6. BeTechnology

    BeTechnology

    Joined:
    Jul 12, 2020
    Posts:
    7
    Oh okay, so the screen position start from bottom left corner, and I need to inverse the y to match it
     
  7. dusho_

    dusho_

    Joined:
    Jul 1, 2022
    Posts:
    1
    Code (CSharp):
    1. _visualElement.worldTransform.GetPosition();
    is returning always (0.00, 0.00, 0.00) in Start(), OnEnable() and even first frame of Update() - is that correct behavior?
    When are the positions and sizes of visual elements recalculated?

    Also if
    Code (CSharp):
    1. Camera.main.ScreenToWorldPoint(pos);
    is not working anymore with new UI toolkit coordinate system, there should be at least some overload method that accepts explicit origin of UI system
     
  8. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    515
    Yes, the layout is calculated at between the update and the lateUpdate phase as the iputs for the layout are set during the update, and we try to calculate the layout as few time as possible to minimize the performance requirement.
     
    dusho_ likes this.