在使用中,为了是面片保持宽高比和原始位置,全屏化选择
// 获取摄像机的宽高比
float camera_aspect = virtureCamera.aspect;
// 面片的宽高比
float texture_aspect = (float)m_width / m_height;
// 根据图像的比率对图片进行缩放操作
backgroundPlane.localScale = new Vector3(m_width / 10f, 1, m_height / 10f);
// 获取背景图片的像素宽高
Vector2 point1 = virtureCamera.WorldToScreenPoint(backgroundPlane.GetComponent<Renderer>().bounds.min);
Vector3 point2 = virtureCamera.WorldToScreenPoint(backgroundPlane.GetComponent<Renderer>().bounds.max);
if (camera_aspect > texture_aspect)
{
float plane_X= point2.x - point1.x;
float screen_plane = Screen.width / plane_X;
if (screen_plane < 0)
screen_plane *= -1;
backgroundPlane.localScale *= screen_plane;
}
else
{
float plane_Y = point2.y - point1.y;
float screen_plane = Screen.height / plane_Y;
if (screen_plane < 0)
screen_plane *= -1;
backgroundPlane.localScale *= screen_plane;
}
本文介绍了一种在Unity中实现面片全屏显示的方法,通过调整面片的宽高比来保持其原始比例,同时确保它能正确填充整个屏幕。文章详细解释了如何根据摄像机和面片的宽高比进行缩放,并根据不同的宽高比情况调整面片的位置。
1000

被折叠的 条评论
为什么被折叠?



