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

Question How do you get the screen coordinates of objects on screen

Discussion in 'Scripting' started by dlorre, Apr 27, 2023.

  1. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    I have this setup, screen size is 800x600, the big square is a plane, the small blue square is a canvas with a RawImage inside it.

    upload_2023-4-27_14-50-14.png


    What I need is the coordinates of the 2 squares on the screen so I that I can adapt my UI to them.

    I can do that easily by taking a screenshot and looking for the coords in an app such as paint.net but of course I need to compute them in Unity.
    cameracapture.png

    So for the big rect I get: 67,147 ->327,406
    and for the small blue rect I get: 349,249->449,349

    But I can't get these coords by using the camera WorldToScreenPoint method. The coordinates that I get don't make any sense.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class ScreenCoords : MonoBehaviour
    6. {
    7.  
    8.     [SerializeField] private RawImage rawImage;
    9.  
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.         if (Input.GetKeyDown(KeyCode.S))
    15.         {
    16.             ScreenCapture.CaptureScreenshot(Application.dataPath + "/cameracapture.png");
    17.         }
    18.         if (Input.GetKeyDown(KeyCode.C))
    19.         {
    20.             screenCoords("Plane", gameObject.transform);
    21.             screenCoords("RawImage", rawImage.transform);
    22.         }
    23.     }
    24.  
    25.  
    26.     private void screenCoords(string title, Transform transform)
    27.     {
    28.         Debug.Log($"screen coords for {title}");
    29.         Debug.Log($"position->{transform.position}");
    30.         Debug.Log($"screen width->{Screen.width} height->{Screen.height}");
    31.         var pos = Camera.main.WorldToScreenPoint(transform.position);
    32.         Debug.Log($"pos->{pos}");
    33.         Debug.Log("=======================================================");
    34.     }
    35.  
    36.  
    37. }
    38.  
    39.  
    What am I doing wrong?
     
    Last edited: Apr 27, 2023
  2. Ruffian0305

    Ruffian0305

    Joined:
    Feb 23, 2015
    Posts:
    8
    You'll have to calculate the coordinates for the Canvas element (blue square) different than the object in the world (grey plane).

    For the plane, Camera.main.WorldToScreenPoint(worldPosition) should work
    You could also use WorldToViewportPoint(Vector3) if you want it in normalized screen sapce

    For the Canvas element, your options are a bit trickier and doing some searching for "How to get [normalized] screen position of UI element in unity" might be able to direct you better than I can.

    Good luck!
     
  3. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    Thanks, I think that the transform points to the center of the square with the y coordinate inverted. So, 197,323 becomes 197,277. I still need to figure out how to get the top left and the bottom right corners.
     
  4. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    The coordinates were wrong, this code seems okish:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class ScreenCoords : MonoBehaviour
    6. {
    7.  
    8.     [SerializeField] private RawImage rawImage;
    9.  
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.         if (Input.GetKeyDown(KeyCode.S))
    15.         {
    16.             ScreenCapture.CaptureScreenshot(Application.dataPath + "/cameracapture.png");
    17.         }
    18.         if (Input.GetKeyDown(KeyCode.C))
    19.         {
    20.             squareCoords("Plane", gameObject.transform, gameObject.GetComponent<Renderer>());
    21.             rawImageCoords("RawImage", rawImage.GetComponent<RectTransform>());
    22.         }
    23.     }
    24.  
    25.  
    26.     private void squareCoords(string title, Transform transform, Renderer renderer)
    27.     {
    28.         Debug.Log($"bounds->{renderer.bounds.extents.x}");
    29.         Vector3 topLeft = renderer.bounds.center - renderer.bounds.extents;
    30.         Vector3 bottomRight = renderer.bounds.center + renderer.bounds.extents;
    31.         Debug.Log($"topLeft->{Camera.main.WorldToScreenPoint(topLeft)}");
    32.         Debug.Log($"bottomRight->{Camera.main.WorldToScreenPoint(bottomRight)}");
    33.         Debug.Log("=======================================================");
    34.     }
    35.  
    36.     private void rawImageCoords(string title, RectTransform rt)
    37.     {
    38.         Debug.Log($"screen coords for {title}");
    39.         Debug.Log($"position->{rt.position}");
    40.         Debug.Log($"screen width->{Screen.width} height->{Screen.height}");
    41.         var pos = Camera.main.WorldToScreenPoint(rt.position);
    42.         Debug.Log($"pos->{pos}");
    43.         Debug.Log("=======================================================");
    44.         Debug.Log($"transform size->{rt.sizeDelta}");
    45.         var p2 = new Vector2(rt.position.x, rt.position.y);
    46.         var topLeft =  p2 - rt.sizeDelta / 2 ;
    47.         var bottomRight = p2 + rt.sizeDelta / 2;
    48.         Debug.Log($"topLeft->{topLeft}");
    49.         Debug.Log($"bottomRight->{bottomRight}");
    50.     }
    51.  
    52.  
    53. }
    54.  
    55.  
     
  5. Ruffian0305

    Ruffian0305

    Joined:
    Feb 23, 2015
    Posts:
    8
    1. var menuCorners = new Vector3[4];
    2. menu.GetComponent<RectTransform>().GetWorldCorners(menuCorners);

      that might help you get the corners ... you're in territory I am not super familiar with, so unfortunately I can only give little nudges :(
     
  6. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    Actually it seems that you have nothing to do at all in case of a canvas image. Of course, my case was super simple with default values.