缩放场景以获得不同的宽高比

时间:2014-04-15 11:57:18

标签: unity3d

在团结中我试图缩放场景以适应屏幕尺寸而不会失去它的纵横比。我尝试了一个方面实用程序的解决方案,但它不能正常工作,它显示黑色条带,因此UI看起来不太好。 我想针对Android和iPad两种设备(例如16:9,4:3的比例) 任何人都可以指导我如何在任何类型的设备上实现扩展吗?

3 个答案:

答案 0 :(得分:0)

您可以使用NGUI插件并在图像上附加UIStretchScript。

答案 1 :(得分:0)

试试这个...将脚本命名为" camera.cs" ....将其添加到相机中......并粘贴以下代码:

    using UnityEngine;
    using System.Collections;

    public class camera : 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 () {

}

}

答案 2 :(得分:0)

只有两种方法可以使用不同的视口宽高比来维持游戏的宽高比。首先,在给定的方向上伸展,这绝不是一个好的选择。其次,信箱(黑条)会影响可用性,特别是在手持屏幕上。我的建议是允许游戏视图根据屏幕的宽高比(我认为是Unity中的默认功能)进行缩放,并设计GUI以响应屏幕尺寸(即不用像素绘制GUI元素特定的坐标,但相对于屏幕尺寸的坐标。)