Search Unity

Aspect Ratio Issue On Build

Discussion in '2D' started by Evanmaz07, Aug 3, 2018.

  1. Evanmaz07

    Evanmaz07

    Joined:
    Sep 25, 2017
    Posts:
    9
    I've been working on this 2 d project and it requires a set 14:8 aspect ratio because all of the levels are 14 blocks by 8 blocks. I have it set to 14:8 in the game view so when I test it it looks fine. GravityMazePlayModePic.PNG
    But, when I build it it scales up to fit the window. This is a problem because when the player goes out of certain boundaries, it stops which looks weird when the screen is wider. GravityMazeBuildModePic.PNG I have tried messing around with the resolution and presentation window in player settings (e.g. making it full screen on default, resolution 1400 : 800, changing supported aspect ratios) but nothing worked. I know i could probably set the boundaries wider but it might still look weird on different computers. Also I would preferably not change all the levels to make them 16 : 10 or something. Is there any way to make it look somewhat like the 1st picture on build? Thanks!
     
  2. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    398
    I have somewhat of a cheat for this, I'm not sure where I got it, but it is something I use when I want fast results. No matter what it will override the camera and add black bars for screens that don't support the resolution. Just change targetaspect to 14.0 / 8.0 and you should be good to go! Then attach this script to your camera.

    Code (csharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class CameraAspect : MonoBehaviour {
    8.  
    9.    // Use this for initialization
    10.    void Start () {
    11.        // set the desired aspect ratio (the values in this example are
    12.        // hard-coded for 16:9, but you could make them into public
    13.        // variables instead so you can set them at design time)
    14.        float targetaspect = 16.0f / 9.0f;
    15.        
    16.        // determine the game window's current aspect ratio
    17.        float windowaspect = (float)Screen.width / (float)Screen.height;
    18.        
    19.        // current viewport height should be scaled by this amount
    20.        float scaleheight = windowaspect / targetaspect;
    21.        
    22.        // obtain camera component so we can modify its viewport
    23.        Camera camera = GetComponent<Camera>();
    24.        
    25.        // if scaled height is less than current height, add letterbox
    26.        if (scaleheight < 1.0f)
    27.        {  
    28.            Rect rect = camera.rect;
    29.            
    30.            rect.width = 1.0f;
    31.            rect.height = scaleheight;
    32.            rect.x = 0;
    33.            rect.y = (1.0f - scaleheight) / 2.0f;
    34.            
    35.            camera.rect = rect;
    36.        }
    37.        else // add pillarbox
    38.        {
    39.            float scalewidth = 1.0f / scaleheight;
    40.            
    41.            Rect rect = camera.rect;
    42.            
    43.            rect.width = scalewidth;
    44.            rect.height = 1.0f;
    45.            rect.x = (1.0f - scalewidth) / 2.0f;
    46.            rect.y = 0;
    47.            
    48.            camera.rect = rect;
    49.        }
    50.    }
    51.    
    52.    
    53. }
    54.  
    55.  
    Hope this helps!
     
  3. Evanmaz07

    Evanmaz07

    Joined:
    Sep 25, 2017
    Posts:
    9
    It works great. Thanks!
     
    unitynoob24 likes this.
  4. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    398
    No problem! Glad to help :)
     
  5. Evanmaz07

    Evanmaz07

    Joined:
    Sep 25, 2017
    Posts:
    9
  6. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    398
  7. LevitateWind

    LevitateWind

    Joined:
    Oct 10, 2018
    Posts:
    1

    You're a life saver. I was going crazy because I thought I wasn't going to be able to upload my game that I've worked really hard on. Thanks to you this quick fix was exactly what I needed. From bottom of my heart thank you, friend.
     
    unitynoob24 likes this.