unity获取屏幕中心位置Vector3
要获取屏幕中心位置的Vector3,可以使用Camera.main的ViewportToWorldPoint方法将屏幕中心点的屏幕坐标转换为世界坐标。
以下是获取屏幕中心位置的代码示例:
using UnityEngine;
public class GetScreenCenter : MonoBehaviour
{
private void Start()
{
// 获取屏幕中心点的屏幕坐标
Vector3 screenCenter = new Vector3(Screen.width / 2, Screen.height / 2, 0);
// 将屏幕中心点的屏幕坐标转换为世界坐标
Vector3 worldCenter = Camera.main.ViewportToWorldPoint(screenCenter);
Debug.Log("屏幕中心位置:" + worldCenter);
}
}
将上述脚本挂载到场景中的任意一个游戏对象上,运行游戏后,控制台会输出屏幕中心位置的世界坐标。
原文地址: https://www.cveoy.top/t/topic/jf3e 著作权归作者所有。请勿转载和采集!