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 UI Image real screen position

Discussion in 'Editor & General Support' started by cgutierrez71, Jun 28, 2023.

  1. cgutierrez71

    cgutierrez71

    Joined:
    Oct 15, 2013
    Posts:
    69
    Hi guys, is it possible to know the real screen position of a UI Image as a Rect? If yes, please how.

    Thanks
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    It's very misleading. Specifically, this method:

    https://docs.unity3d.com/ScriptReference/RectTransform.GetWorldCorners.html

    gives you the four corners of a RectTransform.

    In CanvasWorld Space that is true.

    In Overlay mode... well, the world is apparently the screen, so it gives you screen corners of that RectTransform

    "Image" is in WorldSpace right around (0,0,0)

    "Text" is in overlay mode

    Screen Shot 2023-06-28 at 4.40.29 PM.png
     
    cgutierrez71 likes this.
  3. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    "Real screen position" what do you mean by 'real', pixels or relative?

    Maybe you want to position (anchor) something on the screen, where you can use RectTransform anchors and pivots.
     
    cgutierrez71 likes this.
  4. cgutierrez71

    cgutierrez71

    Joined:
    Oct 15, 2013
    Posts:
    69
    Well sorry, maybe I didn't explain myself well. When I used to work with GUITextures, I could find out their actual position in pixels in relation to the screen size like this:

    For example:
    this.fireP = new Rect(Screen.width - ((130 * Screen.width) / this.width), (80 * Screen.height) / this.height, (110 * Screen.width) / this.width, (110 * Screen.width) / this.width);

    "this.with" and "this.height" are my reference resolution.

    I'd like to get the same position but from an UI Image, that's it :)
     
  5. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    Okay. I just wanted to check what youre trying to actually do, because I have quite a complicated UI, and some elements are floating on the screen and shown on top of gameobjects, etc. but I have never queried UI elements' coordinates because the workflow rather goes the way that UI elements are placed according to either static positions, or into positions relative to other elements, such as "30 pixels right from the left edge". They don't really float or move around freely and you'd need to know where they are. You just need to tell them where they need to be, and this can be expressed mostly in relative position not pixels, and this will automatically scale when your screen size changes et cetera, only when not using absolute pixels but relative position. The RectTransform is quite flexible to achieve this.

    So my point is just to check your workflow that are you sure you even want to do go into working with pixels.

    But if you are sure you want to do this, then how to calculate it is relative to your Canvas Render Mode, and you could eg go the way suggested by @Kurt-Dekker
     
  6. cgutierrez71

    cgutierrez71

    Joined:
    Oct 15, 2013
    Posts:
    69
    Well, I really don't understand well what Kurt says. I don't know how to change my code line above from GUITexture to UI Image. It's very confusing to me since I never worked with UI. Thanks anyway
     
  7. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    I suggest you watch some tutorial of how to work with UI and take a look at how RectTransform works. It is the base class of all UI elements.
     
  8. cgutierrez71

    cgutierrez71

    Joined:
    Oct 15, 2013
    Posts:
    69
    Yes I've seen a lot of them. I really miss the Guitextures....
     
  9. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    449
    You never worked with UI and you miss Guitextures
     
  10. cgutierrez71

    cgutierrez71

    Joined:
    Oct 15, 2013
    Posts:
    69

    Finally this worked for me, thank you very much.

    Now I need to change the UI Image position through the code, but this helped me a lot. :)