I have been trying to figure this out for hours now and every answer I find seem to get thrown off with some setting but mainly depending on where the pivot is set to.
How do I find the center position of a RecTransform, in Screen Space (or relative to the canvas, either is useful), regardless of what scale, pivot anchor or canvas settings?
Sorry but you are wrong and just lucky that your implementation works as intended in your case. For Screen Space Overlay you just need to read transform.position to get its screen position.
Since this is the highest viewed source on Google, I’ll quickly denote this answer as semi-correct, for others stumbling upon it.
The rough summarization:
(ObjectTransform).TransformPoint
Does nothing else than convert the local space position to the objects world position.
There is no luck involved that the implementation works, as the RectTransform position - irrelevant if the canvas is set to Screenspace - Overlay/Camera or World space - will always return positions which can be transformed into the correlating Screen Space given the correct matrices.
Alas the implementation in this thread is just one of many ways to use the center bounds of an object, if the pivot is not in center.
The only “issues” that can occur, are skewed result positions, depending on the scaling of the object, but it’s such a bening issue that hardly anyone will even be able to eyeball it to too much.
Lastly, you can indeed get the position of an object via transform.position;
note that this will always be the position of the objects pivot, not it’s center.
With that said, I wish happy tidings on everyone’s development journey.
both UI Elements share a parent (like the canvas they are both on).
The object you are trying to get the center of has a pivot of (0.5f,0.5f).
RectTransform.localPosition is a Vector3 that shows the UI element’s offset from its parent. This property holds the screen space position of the object’s pivot point relative to its parent.