using UnityEngine;
public class GetScreenCoordinatesTest : MonoBehaviour
{
public GameObject obj;
// Start is called before the first frame update
void Start()
{
Rect rect = GetScreenCoordinates(transform.GetComponent<RectTransform>());
obj.transform.position = new Vector3(rect.x, rect.y,obj.transform.position.z);
}
public Rect GetScreenCoordinates(RectTransform uiElement)
{
var worldCorners = new Vector3[4];
uiElement.GetWorldCorners(worldCorners);
var result = new Rect(
worldCorners[0].x + (worldCorners[2].x - worldCorners[0].x)/2,
worldCorners[0].y + (worldCorners[2].y - worldCorners[0].y)/2,
worldCorners[2].x - worldCorners[0].x,
worldCorners[2].y - worldCorners[0].y);
Debug.Log(result);
return result;
}
}