Search code examples
unity-game-enginescreen-resolution

Unity3D canvas scaler for different aspect ratios


In Unity, I'm trying to build a mobile game for both android and ios platforms. I have been trying some different screen resolutions for testing purposes before I release the game. I have addressed the resolution issues using the code,

[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class ControllingCameraAspectScript : MonoBehaviour
{
    public float sceneWidth = 21f;
    float targetaspect = 16.0f / 9.0f;
    float windowaspect = (float)Screen.width / (float)Screen.height;
    Camera camera;
    public Vector2 targetAspect = new Vector2(16, 9);
    
    void Start()
    {
        camera = GetComponent<Camera>();
        UpdateCrop();
    }

    public void UpdateCrop()
    {
        // Determine ratios of screen/window & target, respectively.
        float screenRatio = Screen.width / (float)Screen.height;
        float targetRatio = targetAspect.x / targetAspect.y;

        if (Mathf.Approximately(screenRatio, targetRatio))
        {
            // Screen or window is the target aspect ratio: use the whole area.
            camera.rect = new Rect(0, 0, 1, 1);
        }
        else if (screenRatio > targetRatio)
        {
            // Screen or window is wider than the target: pillarbox.
            float normalizedWidth = targetRatio / screenRatio;
            float barThickness = (1f - normalizedWidth) / 2f;
            camera.rect = new Rect(barThickness, 0, normalizedWidth, 1);
        }
        else
        {
            // Screen or window is narrower than the target: letterbox.
            float normalizedHeight = screenRatio / targetRatio;
            float barThickness = (1f - normalizedHeight) / 2f;
            camera.rect = new Rect(0, barThickness, 1, normalizedHeight);
        }
    }
}

This code works very well with the aspect ratios below 2.0. (16/9, 16/10, 5/4 etc). When it comes to the screen resolutions such as 3200x1440 even the level seems to be finely cut off to match the view, canvas does drop out from the screen as per the screenshots below.

Aspect ratio : 16/9 (same result for 1280x768) enter image description here

screen resolution 3200x1440 (Aspect ratios above 2.0) enter image description here

Canvas settings

enter image description here

Does anybody have an idea about the issue here? Any suggestions would be highly appreciated.


Solution

  • I have found a good solution for this particular issue.

    Letterboxer makes it easy to automatically add letterboxing, pillar boxing, or pixel-perfect view scaling to that game camera's view. It also updates while in edit mode to give real-time previews of letterboxing in the game view.

    Usage -

    1. Add the Letterboxer component to your Camera GameObject. If a Camera component doesn't exist on the GameObject, one will be added.

    2. Change the Target Width and Target Height options to suit your needs. These will be used to determine the aspect ratio for the Maintain Aspect Ratio mode or the base size for the Best Pixel Perfect Fit mode.

    3. Set the Type option, both of which will be described in more detail below.

    Download - https://github.com/RyanNielson/Letterboxer