unit: keep the background image fully visible in different screen solutions

I have already tried many scripts that look like what I need. However, none of them really worked as I needed.

I have composed an inventory scene.
I started by creating a 1920×1080 pixel image as a background.
I created a plane of scale x = 1.92, y = 1.08 and I put the background image.

Does it look like this:
enter the description of the image here

Then I applied the following script to the main camera to maintain the aspect ratio on different screen sizes (taken from here) and keep the background image fully visible:

using UnityEngine;

(RequireComponent(typeof(Camera)))
public class CameraCrop : MonoBehaviour {

    // Set this to your target aspect ratio, eg. (16, 9) or (4, 3).
    public Vector2 targetAspect = new Vector2(16, 9);
    Camera _camera;

    void Start () {
        _camera = GetComponent();
        UpdateCrop();
    }

    // Call this method if your window size or target aspect change.
    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);
        }
    }
}

However, this results in the following playability:

enter the description of the image here

Why does the script apply extra space at the top and bottom? This is a waste of space.

Thank you.

When a 2020 Democratic candidate who was caught lying about his background makes a speech, do you believe him?

Report abuse

Additional details