【发布时间】:2021-04-20 13:54:40
【问题描述】:
在 Unity 中,我正在尝试为 android 和 ios 平台构建手机游戏。 在发布游戏之前,我一直在尝试一些不同的屏幕分辨率以进行测试。 我已经使用代码解决了分辨率问题,
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class ControllingCameraAspectScript : MonoBehaviour
{
public float sceneWidth = 21f;
float targetaspect = 16.0f / 9.0f;
float windowaspect = (float)Screen.width / (float)Screen.height;
Camera camera;
public Vector2 targetAspect = new Vector2(16, 9);
void Start()
{
camera = GetComponent<Camera>();
UpdateCrop();
}
public void UpdateCrop()
{
// Determine ratios of screen/window & target, respectively.
float screenRatio = Screen.width / (float)Screen.height;
float targetRatio = targetAspect.x / targetAspect.y;
if (Mathf.Approximately(screenRatio, targetRatio))
{
// Screen or window is the target aspect ratio: use the whole area.
camera.rect = new Rect(0, 0, 1, 1);
}
else if (screenRatio > targetRatio)
{
// Screen or window is wider than the target: pillarbox.
float normalizedWidth = targetRatio / screenRatio;
float barThickness = (1f - normalizedWidth) / 2f;
camera.rect = new Rect(barThickness, 0, normalizedWidth, 1);
}
else
{
// Screen or window is narrower than the target: letterbox.
float normalizedHeight = screenRatio / targetRatio;
float barThickness = (1f - normalizedHeight) / 2f;
camera.rect = new Rect(0, barThickness, 1, normalizedHeight);
}
}
}
此代码在纵横比低于 2.0 时效果很好。 (16/9、16/10、5/4 等)。 当涉及到 3200x1440 等屏幕分辨率时,即使关卡似乎被精细切割以匹配视图,画布确实会从屏幕上掉下来,如下面的屏幕截图所示。
画布设置
有人知道这里的问题吗?任何建议将不胜感激。
【问题讨论】: