Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Why world space position is in screen space pixels for UI Button?

Discussion in 'UGUI & TextMesh Pro' started by hellobody, Apr 27, 2023.

  1. hellobody

    hellobody

    Joined:
    Jan 21, 2020
    Posts:
    4
    Hello there.

    Could you please help to clarify the following?

    I have root Canvas and a UI Button added and positioned to the top right corner of the screen.

    I'm interested in value of transform.position field.

    Transform class description says:

    upload_2023-4-27_13-8-23.png

    position - The world space position of the Transform.

    World space (world coordinate system) is 3D space measured in Unity Units, correct?

    When I have Canvas with Render Mode: Screen Space - Camera - all's good:

    upload_2023-4-27_13-9-0.png

    upload_2023-4-27_13-9-31.png

    button.transform.position "(4.50, 10.00, 0.00)"

    The position field of the button's transform shows me value in world space units.

    But when I change Canvas Render Mode to Screen Space - Overlay the position field seems to be displaying value in screen space pixels:

    upload_2023-4-27_13-14-27.png

    button.transform.position "(1080.00, 2400.00, 0.00)"

    Why is that? How could that be possible that where world space units are expected we get value in screen space pixels?

    Could you please give a definition to the notion of world space? What is world space and in what units it's expressed?

    Thank you.
     
  2. hellobody

    hellobody

    Joined:
    Jan 21, 2020
    Posts:
    4
    OK, nobody commented on that so here is what ChatGPT says:

    Yes, you are correct that world space (world coordinate system) is a 3D space measured in Unity Units. World space is the global coordinate system in which all the game objects are placed, and it is generally measured in meters.

    When you set the Canvas Render Mode to "Screen Space - Camera," the UI elements are rendered by a specific camera, and their positions are expressed in world space units, just like any other 3D game object.

    However, when you change the Canvas Render Mode to "Screen Space - Overlay," the UI elements are rendered directly on the screen without being affected by a camera's position or settings. In this mode, the UI elements are positioned relative to the screen's dimensions, which are expressed in pixels.
     
  3. hellobody

    hellobody

    Joined:
    Jan 21, 2020
    Posts:
    4
    Then why Transform class documentation says that "position" field is world space value?
     
  4. hellobody

    hellobody

    Joined:
    Jan 21, 2020
    Posts:
    4
    Clarification from ChatGPT:

    You are right, the Transform class documentation states that the "position" field represents the world space position of the Transform. In most cases, this is true. However, when dealing with UI elements in Unity, especially when the Canvas Render Mode is set to "Screen Space - Overlay," the behavior can be slightly different from what you might expect based on the documentation.

    In "Screen Space - Overlay" mode, even though the Transform's position field still represents a position, it is expressed in screen space (pixel) coordinates rather than world space units. This happens because the UI elements are rendered directly onto the screen, bypassing the camera's transformations.

    It's important to note that this deviation from the general behavior of the Transform class is specific to UI elements under a Canvas with "Screen Space - Overlay" render mode. In most other scenarios, the position field of the Transform will indeed represent the world space position of the object.

    This discrepancy between the documentation and the actual behavior in this particular case can be confusing. It would be helpful if the documentation specified the behavior of the Transform's position field when dealing with UI elements in different Canvas Render Modes.