Unity android屏幕分辨率

时间:2017-09-18 18:10:51

标签: unity3d unityscript

我在自由纵横比上制作了一个关于最新统一版本的简单2D游戏,但每当我将屏幕更改为肖像或景观时,事情就会陷入困境。现在我花了整整一天的时间来学习教程并完成每个教程或答案,但奇怪的是,其中一个代码没有帮助。以下是我的屏幕在不同分辨率上的显示方式

免费看点 enter image description here 肖像 enter image description here 景观

enter image description here

现在我正在使用此代码

   using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Screeratio : MonoBehaviour {

        // Use this for initialization
        void Start () {

            // set the desired aspect ratio (the values in this example are
            // hard-coded for 16:9, but you could make them into public
            // variables instead so you can set them at design time)
            float targetaspect = 16.0f / 9.0f;

            // determine the game window's current aspect ratio

            float windowaspect = (float)Screen.width / (float)Screen.height;

            // current viewport height should be scaled by this amount

            float scaleheight = windowaspect / targetaspect;

            // obtain camera component so we can modify its viewport

            Camera camera = GetComponent<Camera>();

            // if scaled height is less than current height, add letterbox

            if (scaleheight < 1.0f)
            {
                Rect rect = camera.rect;

                rect.width = 1.0f;

                rect.height = scaleheight;
                rect.x = 0;
                rect.y = (1.0f - scaleheight) / 2.0f;

                camera.rect = rect;

            }
            else // add pillarbox
            {
                float scalewidth = 1.0f / scaleheight;

                Rect rect = camera.rect;

                rect.width = scalewidth;

                rect.height = 1.0f;
                rect.x = (1.0f - scalewidth) / 2.0f;
                rect.y = 0;

                camera.rect = rect;

            }
        }

        // Update is called once per frame
        void Update () {

        }
    }

这似乎不起作用。我该怎么做才能让我的游戏在任何屏幕尺寸上顺畅运行。

1 个答案:

答案 0 :(得分:1)

你用帆布吗? 注意你的锚的位置?
如果你现在没有使用它,你可以用this video作为例子。

您的游戏只能在横向或任何方向播放? 您可以在编辑&gt;播放器&gt;解决方案和演示文稿选项卡中以纵向或横向修复视图。