- Home /
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 !
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
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.
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);
}
}
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.
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
Thank you very much for your help @troien, everything is working fine now ! :)
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);
Your answer
Follow this Question
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