How to fit a background sprite with 4:3 aspect ratio in a 16:9 screen by simply making the extra content of the sprite from the bottom go outside the camera?

Hello guys, I am making a 2D platformer game in Unity, in which I will be using a background sprite of the resolution 1280 * 960. I want to fit the same sprite in a screen with an aspect ratio of 16:9 ( 1280 * 720). So in order to do that, I need to cut the additional content of that sprite from the bottom. The width should remain the same and fit the screen perfectly ( there shouldn’t be any blank spaces left out on the left or right side). Also the top region shouldn’t go outside the camera (and there shouldn’t be any blank space left). Only the bottom region should go outside the camera. Could someone please help me do this? Thanks in advance!

Click on this sprite in Project panel and then in inspector click “Sprite editor”, there you can cut your Sprite.

Assuming the game is 2D,

  1. In the hierarchy, right-click your MainCamera and select [ 2D Object > Sprite ] to create a new GameObject with a SpriteRenderer component.
  2. Assign your Sprite in the SpriteRenderer.
  3. Add the SpriteBackground script below. [ Add Component > New Script > name it “SpriteBackground” > Create and Add ]

SpriteBackground.cs

using UnityEngine;

[RequireComponent(typeof(SpriteRenderer))]
public class SpriteBackground : MonoBehaviour
{
	// When the game starts,
	void Awake()
	{
		// get references
		SpriteRenderer sr = GetComponent<SpriteRenderer>();
		Camera cam = Camera.main;

		// check sprite is not null
		if (sr.sprite == null)
			Debug.LogError("Sprite is null.");
		else
		{
			// if screen is portrait, there will be a gap at the bottom.
			if (cam.aspect < 1f)
				Debug.LogError("Portrait screen means SpriteBackground has a gap at the bottom!");

			// get sprite size
			Vector2 spriteSize = sr.sprite.bounds.size;

			// get orthographic camera height/width in world units.
			float height = cam.orthographicSize;
			float width = height * cam.aspect;

			// calculate the required scale
			float size = width * 2f / spriteSize.x;
			transform.localScale = new Vector3(size, size, 1f);

			// calculate the position so that the top of
			// the sprite touches the top of the camera view.
			const float zPos = 100f; // distance from the camera
			transform.localPosition = new Vector3(0f, height - (size / 2 * spriteSize.y), zPos);
		}
	}
}