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
1
Question by MaT227 · Apr 12, 2016 at 09:55 AM · 2duitransformpositionrect

Convert Input.mousePosition to RectTransform pivot position

Hi there,

I am trying to convert my Input.mousePosition into the normalized Pivot position of some of my RectTransform but I don't know how to do it. Any idea ?

Thanks a lot !

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

2 Replies

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

Answer by troien · Apr 12, 2016 at 02:31 PM

You can use RectTransformUtility.ScreenPointToLocalPointInRectangle to get the point inside your rectangle. I suppose you then could use your rectTransfrom.rect to calculate the normalized position using Rect.PointToNormalized. I say this out of my memory :p so not 100% sure. Please tell me whether this actually works or not, if it doesn't, i'll check and come up with a better answer :D

Comment
Add comment · Show 7 · 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 MaT227 · Apr 12, 2016 at 02:49 PM 0
Share

Thank you very much for your answer @troien, it gives me some clues but it doesn't seems to work this way :( If you have any other idea feel free to tell me, I'll also continue checking on my side.

avatar image troien MaT227 · Apr 12, 2016 at 03:03 PM 2
Share

Hmmmm... I tested it now, and it looks like it works for me. So perhaps you have a different setup then I do in my test, or misunderstood me, or I misunderstood you :p

But this is the code I currently have in my test. It prints out the normalized position. (It does clamp it between 0 and 1 though, cause thats how Rect.PointToNormalized works)

 public class PivotTest : $$anonymous$$onoBehaviour
 {
     private RectTransform rectTransform
     {
         get
         {
             return transform as RectTransform;
         }
     }
 
     void Update()
     {
         Vector2 localpoint;
         RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, Input.mousePosition, GetComponentInParent<Canvas>().worldCamera, out localpoint);
 
         Vector2 normalizedPoint = Rect.PointToNormalized(rectTransform.rect, localpoint);
 
         Debug.Log(normalizedPoint);
     }
 }
avatar image MaT227 · Apr 13, 2016 at 06:24 AM 0
Share

I've made another test and you're right we don't have the same setup. I am currently using a screen space overlay canvas and it doesn't work but if I switch to screen space camera it works like a charm.

Do you have any idea why it doesn't work in overlay mode ?

I also precise that when using an aspect ration fitter component it also moves the image a but this doesn't really matter.

avatar image troien MaT227 · Apr 13, 2016 at 09:39 AM 1
Share

Hmmm... I tested it with screen space overlay and it worked for me... So I started to test it now, and realized that it might be because you have the rendercamera of the canvas set.

Normally, you can only set the render/event camera when you select 'screen-space camera' or 'world space'. But it remains set when you then switch back to screen space overlay. I didn't expect that :p

ScreenPointToLocalPointInRectangle requires a camera as parameter, I use the render camera for that as it is in most cases the correct one, when in screen space overlay, this camera should be null, I expected worldCamera (thats how the render camera in the inspector is called in code) to be null when you switch to screen space overlay but it turns out it isn't :p

What I think that's happening is that you still have the render camera set. (switch to screen space camera, set camera to null and then switch back to screen space overlay to see if it is fixed... Alternatively, replace 'GetComponentInParent().worldCamera' with 'null' :p

avatar image MaT227 troien · Apr 13, 2016 at 09:52 AM 0
Share

Thank you very much for your help @troien, everything is working fine now ! :)

Show more comments
avatar image
0

Answer by Marc477 · Jan 19, 2021 at 02:03 AM

Hey, I've been trying to make this work in overlay mode too. I realized the original answer only works for a in Screen Space - Camera. Here is the solution that should work in any case:

 public Vector2 ScreenToRectPos(Vector2 screen_pos)
 { 
     if (canvas.renderMode != RenderMode.ScreenSpaceOverlay && canvas.worldCamera != null)
     {
         //Canvas is in Camera mode
         Vector2 anchorPos;
         RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, screen_pos, canvas.worldCamera, out anchorPos);
         return anchorPos;
     }
     else
     {
         //Canvas is in Overlay mode
         Vector2 anchorPos= screen_pos - new Vector2(rectTransform.position.x, rectTransform.position.y);
         anchorPos= new Vector2(anchorPos.x / rectTransform.lossyScale.x, anchorPos.y / rectTransform.lossyScale.y);
         return anchorPos;
     }
 }

You can then normalize it if needed:

Vector2 normalizedPoint = Rect.PointToNormalized(rectTransform.rect, anchor_pos);

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

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

Drawing a (debug) line between anchored UI element and mouse? 0 Answers

Shoot second cell in 2d 0 Answers

Set Position UI By Mouse Position 2 Answers

Setting my position to the position of a gameobject 0 Answers

Questions about UGUI Rect Transform Anchors. How to make UI objects scale, correctly, with screen? 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