将position
从视口空间转换为屏幕空间。
视口空间是标准化的,相对于相机的。相机的左下角是 (0,0);右上角是 (1,1)。z 轴位置是相机的世界单位。
屏幕空间以像素定义。屏幕的左下角是 (0,0);右上角是 (pixelWidth,pixelHeight)。z 轴位置是相机的世界单位。
using UnityEngine;
public class Example : MonoBehaviour { // Draw an image based on normalized view coordinates // rather than pixel positions. Texture2D bottomPanel;
void VPToScreenPtExample() { var origin = Camera.main.ViewportToScreenPoint(new Vector3(0.25f, 0.1f, 0)); var extent = Camera.main.ViewportToScreenPoint(new Vector3(0.5f, 0.2f, 0));
GUI.DrawTexture(new Rect(origin.x, origin.y, extent.x, extent.y), bottomPanel); } }