[Unity] Get the position of the UI object on the screen

Get the position of the UI object on the screen

Obtain the relative position of the ui in its own Canvas and
the screen coordinates of the full-screen ui of the canvas are its position relative to the canvas plus 1/2 of the size of the canvas;


Canvas canvas = this.transform.GetComponentInParent<Canvas>();
RectTransform canvasRect = canvas.transform.GetComponent<RectTransform>();
//自身相对于Canvas的位置
Vector3 loadpos = canvas.transform.InverseTransformPoint(this.transform.position);
//屏幕位置
Vector3 screenPoint = loadpos + new Vector3(canvasRect.sizeDelta.x, canvasRect.sizeDelta.y, 0) / 2;

Guess you like

Origin blog.csdn.net/weixin_41005274/article/details/129334574