Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by TwilitSoul · Sep 03, 2015 at 07:46 PM · scripting problemuipositioncanvaspanel

How to get accurate x/y for Rectangle Transform of Panel under canvas?

Edit: sorry if it's not clear. I'm trying to get accurate x/y coordinates for the Rectangle Transform of a Panel which is a child of a canvas object, and I want to do it form a script attached to the Panel. The end-goal is to get a UI item I'm positioning in the upper left corner of the panel.

Here's what I've tried, with the script attached to the Panel. (Obviously I didn't try them like this all at once, but one by one)

 RectTransform transform = gameObject.GetComponent<RectTransform>();
 Vector3 usefulPosition = Camera.main.WorldToViewportPoint(transform.anchoredPosition);
 usefulPosition = Camera.main.WorldToViewportPoint(transform.position);
 usefulPosition = Camera.main.WorldToViewportPoint(transform.localPosition);
 usefulPosition = Camera.main.WorldToScreenPoint(transform.anchoredPosition);
 usefulPosition = Camera.main.WorldToScreenPoint(transform.position);
 usefulPosition = Camera.main.WorldToScreenPoint(transform.localPosition);
 usefulPosition = Camera.main.ScreenToWorldPoint(transform.anchoredPosition);
 usefulPosition = Camera.main.ScreenToWorldPoint(transform.position);
 usefulPosition = Camera.main.ScreenToWorldPoint(transform.localPosition);
 usefulPosition = Camera.main.ScreenToViewportPoint(transform.anchoredPosition);
 usefulPosition = Camera.main.ScreenToViewportPoint(transform.position);
 usefulPosition = Camera.main.ScreenToViewportPoint(transform.localPosition);
 usefulPosition = Camera.main.ViewportToScreenPoint(transform.anchoredPosition);
 usefulPosition = Camera.main.ViewportToScreenPoint(transform.position);
 usefulPosition = Camera.main.ViewportToScreenPoint(transform.localPosition);
 usefulPosition = Camera.main.ViewportToWorldPoint(transform.anchoredPosition);
 usefulPosition = Camera.main.ViewportToWorldPoint(transform.position);
 usefulPosition = Camera.main.ViewportToWorldPoint(transform.localPosition);

However, none of these are enough on their own to get the item I'm positioning in the upper left corner of the panel.

Can someone please help me out? Am I missing some kind of multiply by transform type thing? I've done a lot of googling on this, but apparently I suck at Google.

Comment
Add comment · Show 1
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 Hellium · Sep 03, 2015 at 02:46 PM 0
Share

Explain what your are trying to achieve please

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Eudaimonium · Sep 03, 2015 at 07:52 PM

You can use Rectangle transform utility class.

 RectTransformUtility.RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform, rectTransform.position, Camera.main, out usefulPosition);

^Disclaimer, browser typing, I think there is ".position" property of RectTransform class, it should be it's position on the canvas. Note that the function doesn't return the value, but it rather puts it into Reference of a Vector3 for the final argument (hence out keyword). It's a bit stupid, IMO. Can't use it in-line for anything.

Note: Rename your RectTransform reference to something else, JUST in case, because "transform" by default already fetches the gameObject's attached Transform component.

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 Positive7 · Sep 03, 2015 at 07:55 PM

Try this It will move Child Objects to :

  GetComponent<RectTransform>().localPosition = new Vector3(0.0f,0.0f,0.0f);
     GetComponent<RectTransform>().anchorMin = new Vector2(0.0f,1.0f);
     GetComponent<RectTransform>().anchorMax = new Vector2(0.0f,1.0f);
     GetComponent<RectTransform>().pivot = new Vector2(0.0f,1.0f);

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

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Canvas, Scroll Rect, Panels, Panes? 0 Answers

Divide UI in sections 0 Answers

Unity UI - Fading Canvas/Panel 7 Answers

Animation not playing on game object activated by script 3 Answers

How to set the UI Text position to be relative to his "grandfather" prefab 0 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