Version: 2020.2

Camera.ViewportToScreenPoint

切换到手册
public Vector3 ViewportToScreenPoint (Vector3 position);

描述

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); } }