我用Unity开发了一个Android游戏。最后,我面临着下一个问题:
我的游戏的每个关卡都有严格的边框(就像一个方框)沿着屏幕的边框。我一直在开发这款游戏
方面9:16 (图。1)
..。当我构建它并在我的手机上运行(现在是9:19.5)时,我发现
关卡的最左边和最右边的部分在屏幕之外。
(图:2)
..。
我如何解决这个问题,使游戏适合每一个宽高比?
我尝试了不同的解决方案,但都不能让我满意。我太绝望了,我已经同意从9:16开始缩小,这样它就能适应屏幕的宽度,并在顶部和底部添加黑条
(图:3)
..。在我的游戏中,高度并不是那么重要,但宽度是至关重要的。
还是有更好的解决方案?



更新2:我的项目设计(有点灰盒子):

下面是它的外观
很好
在播放模式中有不同的方面:

下面是它的外观
差劲
在播放模式中有不同的方面:

发布于 2021-02-24 17:02:55
所以,我找到了一个实现目标的方法。我
增加了一个空的游戏对象
到现场然后
所有的层次结构都是子级的
,并在关卡上方和下方添加两个黑条(作为简单的立方体)。使用这种方法,我可以缩放父游戏对象,以便所有的关卡结构
按比例缩放
..。然后我要做的就是创建一个脚本来根据屏幕的宽度缩放父游戏对象。
发布于 2021-02-27 04:36:41
Unity的摄像头假设游戏将在横向进行。因此,无论纵横比如何,垂直渲染的游戏世界的数量在任何屏幕上都是相同的。如果你限制你的游戏只能在移动平台上玩,并且只能在肖像模式下玩,那么你会想要强制Unity做相反的事情。您希望摄影机始终渲染相同的宽度,并根据纵横比显示可变的高度。(有用的提示:使用您期望的最小纵横比进行开发,如4:5,而不是9:16。在游戏视图顶部的下拉菜单中配置要测试的所有纵横比或分辨率。在它们之间切换以进行测试,并允许您的UI和游戏视图向外扩展以获得更高的纵横比。否则,您的UI可能会以较小的纵横比一起运行。请看我在CanvasScaler上的帖子
这里
。)
无论如何,要更改默认的摄影机行为,您需要向摄影机添加一个脚本。这样的代码应该可以:
编辑:
我修改了组件以同时处理透视和正交相机。根据你的游戏,你似乎是,或者也许应该使用正交相机,这是我之前的回答没有处理的。此外,组件上没有更多的属性。只需连接到相机,然后正常使用相机的属性。此外,它现在处理视口,因此它与不占用整个屏幕的相机一起工作。
有一个已知的问题。使用正交摄影机时,如果尝试将视口调整为大于整个屏幕大小,则Unity内部的某些内容会重置投影矩阵并覆盖此组件的行为。若要修复,只需切换组件,或重新加载场景或编辑器,任何触发组件再次执行其操作的操作。
using UnityEngine;
[ExecuteAlways]
[RequireComponent(typeof(Camera))]
public class HorizontallyAlignedCamera : MonoBehaviour
{
private Camera _camera;
private float _aspectRatio;
private float _fieldOfView;
private bool _isOrthographic;
private float _orthographicSize;
private void Awake()
{
_camera = GetComponent();
}
private void OnEnable()
{
CachePropertiesAndRecalculate();
}
private void LateUpdate()
{
if(CameraPropertyHasChanged())
CachePropertiesAndRecalculate();
}
private void OnDisable()
{
_camera.ResetProjectionMatrix();
}
private bool CameraPropertyHasChanged()
{
bool hasChanged = (_aspectRatio != _camera.aspect
|| _fieldOfView != _camera.fieldOfView
|| _isOrthographic != _camera.orthographic
|| _orthographicSize != _camera.orthographicSize);
return hasChanged;
}
private void CacheCameraProperties()
{
_aspectRatio = _camera.aspect;
_fieldOfView = _camera.fieldOfView;
_isOrthographic = _camera.orthographic;
_orthographicSize = _camera.orthographicSize;
}
private void CachePropertiesAndRecalculate()
{
CacheCameraProperties();
if(_camera.orthographic)
RecalculateOrthographicMatrix();
else
RecalculatePerspectiveMatrix();
}
private void RecalculatePerspectiveMatrix()
{
float near = _camera.nearClipPlane;
float nearx2 = near * 2.0f;
float far = _camera.farClipPlane;
float halfFovRad = _camera.fieldOfView * 0.5f * Mathf.Deg2Rad;
// This is what aligns the camera horizontally.
float width = nearx2 * Mathf.Tan(halfFovRad);
float height = width / _camera.aspect;
// This is the default behavior.
//float height = nearx2 * Mathf.Tan(halfFovRad);
//float width = height * _camera.aspect;
float a = nearx2 / width;
float b = nearx2 / height;
float c = -(far + near) / (far - near);
float d = -(nearx2 * far) / (far - near);
Matrix4x4 newProjectionMatrix = new Matrix4x4(
new Vector4(a, 0.0f, 0.0f, 0.0f),
new Vector4(0.0f, b, 0.0f, 0.0f),
new Vector4(0.0f, 0.0f, c, -1.0f),
new Vector4(0.0f, 0.0f, d, 0.0f));
_camera.projectionMatrix = newProjectionMatrix;
}
private void RecalculateOrthographicMatrix()
{
// This is what aligns the camera horizontally.
float width = 2.0f * _camera.orthographicSize;
float height = width / _camera.aspect;
// This is the default behavior.
//float height = 2.0f * _camera.orthographicSize;
//float width = height * _camera.aspect;
float near = _camera.nearClipPlane;
float far = _camera.farClipPlane;
float a = 2.0f / width;
float b = 2.0f / height;
float c = -2.0f / (far - near);
float d = -(far + near) / (far - near);
Matrix4x4 newProjectionMatrix = new Matrix4x4(
new Vector4(a, 0.0f, 0.0f, 0.0f),
new Vector4(0.0f, b, 0.0f, 0.0f),
new Vector4(0.0f, 0.0f, c, 0.0f),
new Vector4(0.0f, 0.0f, d, 1.0f));
_camera.projectionMatrix = newProjectionMatrix;
}
}https://stackoverflow.com/questions/66320065
复制相似问题