如何使游戏适合任何屏幕尺寸

时间:2015-06-25 16:43:00

标签: camera screen screen-resolution aspect-ratio

我已经在网上查询了这个问题的答案,但只发现了模糊的回答,如果有人可以帮我解决这个问题,我将非常感激。

我正在创建一个小游戏,并且接近完成,我使用Unity构建设置来构建游戏,但后来遇到了一个问题。该游戏的设计采用16:10宽高比,因为它适合我的显示器,但任何时候我在较小分辨率的游戏部分构建游戏都会从屏幕上切断。我打算发布这款适用于Android的游戏,显然需要能让游戏适合任何尺寸的屏幕或平板电脑。

GUI可以很好地缩放,因为选项可以使画布按屏幕大小缩放'我正在为游戏中的其他一切寻找相同的东西。

其他信息:

我有一台正交相机

我在其他论坛上被告知我需要Camera.main.aspect = ScreenWidth / ScreenHeight;

但在我的剧本中,这并没有做任何事情。

我也包括了我的相机脚本:

公共类CameraScript:MonoBehaviour {

public Transform PlayerTarget;

public float YaXis;
public float XaXis;
public float moveDown;
public float moveUp;

public Vector2 
    margin, 
    Smoothing;

private Vector3 
    _min, 
    _max,
    PlayerPosition;


public bool IsFollowing;

public BoxCollider2D CamBounds;

public void Start()
{


    //float worldScreenHeight = Camera.main.orthographicSize * 2f;
    //float worldScreenWidth = worldScreenHeight / Screen.height * Screen.width;

    //Camera.main.aspect = worldScreenWidth / worldScreenHeight;

    _min = CamBounds.bounds.min;
    _max = CamBounds.bounds.max;
    IsFollowing = true;

}

public void Update()
{
    var x = transform.position.x;
    var y = transform.position.y;

    if (IsFollowing) 
    {
        if (Mathf.Abs(x - PlayerTarget.position.x)> margin.x)
            x = Mathf.Lerp (x, PlayerTarget.position.x, Smoothing.x * Time.deltaTime);

        if (Mathf.Abs(y - PlayerTarget.position.y)> margin.y)
            y = Mathf.Lerp (y, PlayerTarget.position.y, Smoothing.y * Time.deltaTime);
    }



    var CameraHalfWidth = GetComponent<Camera>().orthographicSize * ((float)Screen.width / Screen.height);

    x = Mathf.Clamp (x, _min.x + CameraHalfWidth, _max.x - CameraHalfWidth);
    y = Mathf.Clamp (y, _min.y + GetComponent<Camera>().orthographicSize, _max.y - GetComponent<Camera>().orthographicSize);


    if (Input.GetKey(KeyCode.DownArrow)){
        transform.position = new Vector3 ((x - XaXis) , (y + YaXis) - moveDown, transform.position.z);
    }else if (Input.GetKey(KeyCode.UpArrow)){
        transform.position = new Vector3 ((x - XaXis) , (y + YaXis) + moveUp, transform.position.z);
    }else
    transform.position = new Vector3 ((x - XaXis) , (y + YaXis), transform.position.z);

}

}

2 个答案:

答案 0 :(得分:1)

也许你可以试试这个。

public class AspectRatioScript : MonoBehaviour {

float wantedAspectRatio = 16f / 10f;

void Awake()
{
    GetComponent<Camera>().rect = new Rect(0f, (1.0f - Camera.main.aspect / wantedAspectRatio) / 2, 1f, Camera.main.aspect / wantedAspectRatio);
}}

您将它连接到主摄像头,它会将您的摄像头宽高比调整为16:10。我刚试过它。 希望它会对你有所帮助。

答案 1 :(得分:0)

基于你的问题16:10,我假设你的游戏是横向屏幕吗? 尝试此代码并将其附加到场景中的每个游戏对象(包括主摄像头和背景)

public class gameresize : MonoBehaviour {
float currentCameraAspect;
Camera kamera;
float minAspect,percentAspect;
// 5 : 3 Aspect Ratio
float standartAspect = 1.665557f;
float objectScaleSize;
float newX, newY;
Vector3 objectSize;

// Use this for initialization
void Start () {
    kamera = GameObject.FindObjectOfType(typeof(Camera)) as Camera;
    //resize game object depending on device aspect ratio;
    ResizeObject ();
}

void Update () {

}

void ResizeObject()
{
    currentCameraAspect = kamera.aspect;


    if (currentCameraAspect < standartAspect) {
        //left it blank

    } else if (currentCameraAspect > standartAspect) {

        minAspect = currentCameraAspect - standartAspect;
        percentAspect = (100 * minAspect) / currentCameraAspect;


        objectSize = transform.localScale;
        newX = ((percentAspect * objectSize.x) / 100) + objectSize.x;
        newY = ((percentAspect * objectSize.y) /100) + objectSize.y;

        transform.localScale = new Vector3 (newX , newY ,1f);

    } else {
        //left it blank
    }

  }
}  

希望它可以解决它