How to get coördinates on a ui object?

Hey all,

I have a ui object its a box:

51620-box.png

If I click on the box I want to get the position that the mouse has on the object.
For example if I Click in the exact center it should return me (0.5,0.5). (The pivot system uses the same coordinates system)
I have set up the events etc… so thats is not the problem, but what method should i use to get the coördinates?

Is it GUI or UI?

In each case, get the current position of the mouse through Event.current.mousePosition.
I assume you have the position and the size of the box.

The algorithm.

MP = Mouse Position

X = Box.w / (MP.x - Box.x)
Y = Box.h / (MP.y - Box.y)

In C#

public Vector2	GetRelativeMousePosition()
{
	Rect	box; // Position and size of your box.
	Vector2	mp = Event.current.mousePosition;
	Vector2	relativeClickPosition = Vector2.zero;

	if (box.Contains(mp) == true)
	{
		relativeClickPosition.x = box.width / (mp.x - box.x);
		relativeClickPosition.y = box.height / (mp.y - box.y);
	}
	
	return relativeClickPosition;
}

You can use raycast2d on ui object by attaching boxcollider2d to it. Btw, pls specify what you gonna do by getting co ordinates ?