Monday, July 2, 2018

unity - Unity2D - Horizontal Scaling to Support Various Aspect Ratios?


I am trying to work out how to support various resolutions for my Android game. I have come up with a solution, but do not know how to implement it.


Automatically, I am told that Unity vertically scales the screen to match the screen, leaving more room at the sides. How can I change this behaviour? I am hoping to shrunk the game down till it fits horizontally, and filling it the background above and below with more decorative features. How can I achieve this?


I'm sorry if there is an obvious answer, I'm quite new working with the Unity UI system.


If it makes it any easier, my game is heavily based on UI, and is locked in landscape.


BIG EDIT: I completely rephrased this question because it was so broad and messy.




Answer



Okay, so I think I've got this worked out, so I'm making my own reply. I attached this script to the main camera.


using UnityEngine;

public class AspectRatioController : MonoBehaviour
{
public float targetWidth;

void Start()
{

Camera camera = GetComponent();
camera.orthographicSize = (targetWidth / camera.aspect) / 2;
}
}

In the Unity Editor, set the targetWidth as the width, in units, of your background. This will change the camera size until the background fits perfectly, leaving extra space at top and bottom depending on your aspect ratio.


As for the safe zone, I decided to make two scenes for every menu: one for wider screens, one for squarer screens. The wider screens have 3:2 backgrounds, with a 16:9 safe zone, while the squarer screens have 1:1 backgrounds with 4:3 safe zones.


I hope this can help other people.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...