• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
9
Question by rahulkk · Jul 23, 2015 at 04:03 AM · parentrecttransformparent-childscreenspacelocalspace

Convert RectTransform rect to screen space

I have a RectTransform that is the child of several other RectTransforms. Is there a way for me to convert the child RectTransform's rect property (which is in the local space of the transform) into global screen space (essentially pixel coordinates relative to a corner of the screen)?

Edit: My Canvas is set to Screen Space - Camera and is rendered by the main camera.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iHaveReturnd · Jul 23, 2015 at 03:55 PM 0
Share

Is this what you are looking for?

http://answers.unity3d.com/questions/826851/how-to-get-screen-position-of-a-recttransform-when.html

avatar image rahulkk · Jul 23, 2015 at 04:26 PM 0
Share

I have looked at that, but it doesn't seem to work. Here is the code I wrote, please let me know if I should change anything. $$anonymous$$y Canvas is set to Screen Space - Camera and is rendered by the main camera. This script is attached to the RectTransform whose rect I need to find in screen coordinates:

 Vector2 screenPos = RectTransformUtility.WorldToScreenPoint(Camera.main, transform.position);
 print(screenPos);

However, while my RectTransform is entirely on screen (its pivot point and anchors are on screen too), the script outputs:

(-116.3, -429.3), which is not what I would expect. $$anonymous$$aybe the RectTransform position isn't correct, but I don't know to fix that.

12 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Remal · Dec 29, 2019 at 12:08 PM

After many hours, this is what worked for my case:

 private static Rect GetScreenPositionFromRect(RectTransform rt, Camera camera)
         {
             // getting the world corners
             var corners = new Vector3[4];
             rt.GetWorldCorners(corners);
             
             // getting the screen corners
             for (var i = 0; i < corners.Length; i++)
                 corners[i] = camera.WorldToScreenPoint(corners[i]);
             
             // getting the top left position of the transform
             var position = (Vector2) corners[1];
             // inverting the y axis values, making the top left corner = 0.
             position.y = Screen.height - position.y;
             // calculate the siz, width and height, in pixle format
             var size = corners[2] - corners[0];
             
             return new Rect(position, size);
         }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by maroon3d · Jan 08, 2020 at 11:51 AM

I spent several hours and finally found this workaround:

Even when gameObject has a RectTransform, it also has a transform component that is invisible in the inspector, but still accessible through a script:

 // RECT TRANSFORM TO SCREEN SPACE
     public  Vector2 LocalPositionToScreenPosition(RectTransform _rectTransform)
     {
         Vector2 screenCenter = new Vector2(Screen.currentResolution.width / 2, 
                                            Screen.currentResolution.height / 2);

         Vector2 output = (Vector2)cam.WorldToScreenPoint(_rectTransform.position) - screenCenter;
 
         Debug.Log(output);
         return output;
     }


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JohannesMP · Mar 23, 2021 at 11:31 AM 0
Share

It doesn't also have a Transform component. The RectTransform itself is the Transform; RectTransform inherits from Transform: https://docs.unity3d.com/ScriptReference/RectTransform.html

That is why if you know your object is a UI element, you can access the RectTransform by just casting the the default transform field, without having to use a GetComponent call:

 RectTransform rt = transform as RectTransform;
  • ‹
  • 1
  • 2
  • 3

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

World Position Of Child Object Shifted After Parenting 1 Answer

Transform.rotation is setting local rotatoin 0 Answers

Convert local point in RectTransform to screen space 2 Answers

Raycast Layermask doesn't ignore no layered Parent? 1 Answer

Child cant apply variables from parent 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges