Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 beppim · Nov 23, 2015 at 09:57 AM · uicanvasrecttransformscreenspace

Find UI Image position in Screen Space

In my user interface (based on UUI) I use a script to detect mobile touch input events, then compute the distance from the center of an image that represents a button. I don't know if this is the best way, but it allows me to have the advantages of touch (ie: keep pressed, no delay between first and second click, and so on). In addition I can have a touch-detect area that can be slightly larger than the button itself, and this allows me to have clean interface and no user input errors.

Said that, it used to work everything smoothy, with this simple code (very, very reduced):

 // ...
 pos = button.transform.position;
 // ...
 distance = Vector2.Distance(touch.position, pos);
 if (distance < touchSize)
 // ...

With canvas set in Overlay mode. I'm not aware of the reason why now it does not work anymore. It may be the Unity update, or some change in canvas, camera or alignment, anyway now pos is simply, and always (0, 0). It returns (0, 0) for different buttons in different positions. If I change the position of a parent, it returns the position of the parent. Why???

I tried these possible codes:

 pos = button.transform.position;
 // this used to work and now returns (0,0)
 //

 pos = button.transform.localPosition;
 // this returns a number that depends on alignment: for example it can contain negative numbers if the button is aligned to the center and positioned on the left. Should I calculate the screen position by myself?
 //

 pos = button.GetComponent<RectTransform>().position;
 // this again returns (0,0) !! same as transform.position
 //

 pos = button.GetComponent<RectTransform>().localPosition;
 // and this again returns a relative value !! same as transform.localPosition
 //

 pos = Camera.main.WorldToScreenPoint(button.transform.position);
  // this returns some number that is the same also if I move the button, as soon as button.transform.position returns zero.
 //

 pos = RectTransformUtility.WorldToScreenPoint(null, button.transform.position);
 // this returns always (0,0). As a matter of fact, this method seems to return the same that I get using the component RectTransform.
 //

 pos = Camera.main.WorldToScreenPoint(button.GetComponent<RectTransform>().localPosition);
 // this returns some values that are completely wrong at a first investigation, as if the camera wasn't aligned with the canvas (that is Overlay). I didn't check more, but this could be correct in portrait mode, but I use landscape...
 //

     pos = new Vector2(Screen.width / 2 + button.GetComponent<RectTransform>().localPosition.x, button.GetComponent<RectTransform>().localPosition.y);
 // This is how it currently works. I had to calculate manually the position on the screen, considering that I know that the button is centered horizontally and at the bottom. This is **by far** a bad option because as soon as I should change anything on the formatting of the scene, this would stop working.
 //

I'm really getting crazy on this. It seems that it should be so obvious and simple: I simply want to know the coordinates of the center of a UI Image in Screen Space. I don't have anymore ideas!! Does some one have any idea on how to do it?

Thank you.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Diablo404 · Nov 24, 2015 at 07:05 PM

Maybe you're looking for GetComponent().anchoredPosition

http://docs.unity3d.com/ScriptReference/RectTransform-anchoredPosition.html

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 beppim · Nov 28, 2015 at 02:51 PM 0
Share

I think that's the solution, thanks.

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

41 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 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

camera space performance/ aligncanvasrecttransformwithcamera 0 Answers

Problem copying RectTransform UI elements 0 Answers

Editing RectTransform scale 2 Answers

Strange Canvas behaviour at Screen Space - Camera mode? 0 Answers

Transform to RectTransform Conversion 1 Answer


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