- Home /
Scale Everything In Game to Fit all screen Sizes
I have checked online both on for the answers to this question but have only found vague responses, i would be so grateful if someone could help me out with this.
I am creating a small game, and am near completion, i used the Unity build settings to build the game but then came across a problem. the game was designed in the 16:10 aspect ratio as it suits my monitor, but anytime i build the game on a smaller resolution parts of the game get cut off from the screen. I intended to release this game for android and obviously need something that allows the game to fit on any size screen or tablet.
the GUI scales fine due to the option to make the canvas 'scale with screen size' im looking for the same for everything else in the game.
additional info:
I have an orthographic camera
I have been told in other forums that i need Camera.main.aspect = ScreenWidth / ScreenHeight;
but in my script this doesnt do anything.
I have included my camera script as well:
public class 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);
}
}
Answer by RayJr · Jun 25, 2015 at 07:27 PM
You can force a screen ratio in your game. If someone is using a different ratio, they will see black bars on the top and bottom or sides to meet the ratio you defined..
not really sure thats the way i want to go, it may seem a little unprofessional, especially for the amount of different devices run on android, alot of people will be playing it with black bars, but appreicate you answering as if its something new i have now learnt :)
I hear ya. The most common way to handle this is to test in various resolutions as you build.
Though, you're not forcing the resolution, just the ratio. the black bars may be quite small in many cases.
Good Luck!
Your answer
Follow this Question
Related Questions
Camera Fit Issues on 10 inch tablet 0 Answers
Aquarium like camera 1 Answer
How to make my UI items/canvas stretch to fill preset aspect ratio 0 Answers