screen space overlay 下直接取UI坐标和长,宽,计算得到四角坐标。
screen space camera 下需要转换长宽到相机空间单位:如下:
var cam = GameObject.Find("Camera").GetComponent<Camera>(); var cam_h = 2*cam.orthographicSize; var cam_w = cam_h * Screen.width /Screen.height; var rtrans = gameObject.GetComponent<RectTransform>(); Vector3[] worldcorners = new Vector3[4]; rtrans.GetWorldCorners(worldcorners); float[] wcs = new float[4]; float fw = rtrans.rect.width / Screen.width * cam_w; float fh = rtrans.rect.height / Screen.height * cam_h; var c1 = transform.position.x - fw / 2; var c2 = transform.position.x + fw / 2; var c3 = transform.position.y - fh / 2; var c4 = transform.position.y + fh / 2;
时间: 2024-11-06 09:28:14