【问题标题】:Unity 2d Camera Scale to Screen SizeUnity 2d 相机缩放到屏幕尺寸
【发布时间】:2021-09-20 20:27:28
【问题描述】:

我正在寻找以统一 2d 的方式将正交相机缩放到屏幕尺寸。

当你编辑你的游戏时,你可以按照你想要的方式来定位相机,查看你的所有对象等等。

但是当你最大化它时,你的相机会缩小显示可能其他你不想显示的东西

例如图片

Edit Mode View

Maximised

谢谢你回答我的问题

最好的问候

大卫

【问题讨论】:

  • 查看this 线程。

标签: unity3d


【解决方案1】:

将以下组件附加到您的相机。确保在检查器中设置目标纵横比(例如 16/9)。

using UnityEngine;

[ExecuteAlways]
[RequireComponent(typeof(Camera))]
public class ViewportScaler : MonoBehaviour
{
    private Camera _camera;

    [Tooltip("Set the target aspect ratio.")]
    [SerializeField] private float _targetAspectRatio;

    private void Awake()
    {
        _camera = GetComponent<Camera>();

        if (Application.isPlaying)
            ScaleViewport();
    }

    private void Update()
    {
#if UNITY_EDITOR
        if (_camera)
            ScaleViewport();
#endif
    }

    private void ScaleViewport()
    {
        // determine the game window's current aspect ratio
        var windowaspect = Screen.width / (float) Screen.height;

        // current viewport height should be scaled by this amount
        var scaleheight = windowaspect / _targetAspectRatio;

        // if scaled height is less than current height, add letterbox
        if (scaleheight < 1)
        {
            var rect = _camera.rect;

            rect.width = 1;
            rect.height = scaleheight;
            rect.x = 0;
            rect.y = (1 - scaleheight) / 2;

            _camera.rect = rect;
        }
        else // add pillarbox
        {
            var scalewidth = 1 / scaleheight;

            var rect = _camera.rect;

            rect.width = scalewidth;
            rect.height = 1;
            rect.x = (1 - scalewidth) / 2;
            rect.y = 0;

            _camera.rect = rect;
        }
    }
    
}

【讨论】:

  • 非常有帮助,我的项目需要更多的灵活性,因此扩展了您的代码(由于 cmets 中的字符限制,我将其发布为答案)
【解决方案2】:

我想扩展 @sean-carey 的非常有用的答案。

我修改了他的代码以添加最小和最大纵横比,因此视口将在这两个方面之间全屏,并且当 max 时会出现柱框

using UnityEngine;

[ExecuteAlways]
[RequireComponent(typeof(Camera))]
public class ViewportController : MonoBehaviour
{
    private Camera _camera;

    [Tooltip("Set the target aspect ratio.")]
    [SerializeField] private float _minAspectRatio;
    [SerializeField] private float _maxAspectRatio;

    private void Awake()
    {
        _camera = GetComponent<Camera>();

        if (Application.isPlaying)
            ScaleViewport();
    }

    private void Update()
    {
#if UNITY_EDITOR
        if (_camera)
            ScaleViewport();
#endif
    }

    private void ScaleViewport()
    {
        // determine the game window's current aspect ratio
        var windowaspect = Screen.width / (float) Screen.height;

        // current viewport height should be scaled by this amount
        var letterboxRatio = windowaspect / _minAspectRatio;
        var pillarboxRatio = _maxAspectRatio / windowaspect;

        // if max height is less than current height, add letterbox
        if (letterboxRatio < 1)
        {
            SetRect(1, letterboxRatio, 0, (1-letterboxRatio)/2);
        }
        // if min height is more than current height, add pillarbox
        else if (pillarboxRatio < 1)
        {
            SetRect(pillarboxRatio, 1, (1-pillarboxRatio)/2, 0);
        }
        // full screen
        else
        {
            SetRect(1, 1, 0, 0);
        }
    }

    private void SetRect(float w, float h, float x, float y)
    {
        var rect = _camera.rect;

        rect.width = w;
        rect.height = h;
        rect.x = x;
        rect.y = y;

        _camera.rect = rect;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    • 2016-07-02
    • 2016-07-02
    相关资源
    最近更新 更多