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 !
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 !
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 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
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);