Mouse co-ordinates on UI RawImage?

Is there a nice straightforward way to get the mouse co-ordinates relative to a particular UI component?

Specifically I’m trying to manipulate the Texture2D attached to a RawImage but if there’s a general-purpose approach for all UI Components that would be great, I think.

Okay, from this video I’ve seen how I can extend various input event handlers

Which gets me access to PointerEventData which has a .position that appears to be relative to the Canvas.
http://docs.unity3d.com/ScriptReference/EventSystems.PointerEventData-position.html

So now I just need that info relative to the RawImage instead of the whole Canvas.

Is there anything equivalent to TransformPoint and InverseTransformPoint but instead of between local and world space, it converts between the canvas and the local UI element?

Okay, further playing around, I’ve decided that PointerEventData.position isn’t in CanvasSpace like I thought, because
Debug.Log(rectrect.InverseTransformPoint(eventData.position));
this seems to get me results I want. Zero is located at the pivot point, and it seems to be scaled to match the dimensions of the rectangle of the rectTransform. I think I can work with this.

1 Like