- Home /
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!
when you use it as an Image on a canvas, you can configure the canvasscaler to match the width. I think scale with screen size mode was it.
@hexagonius But I am not using a UI image here. I am talking about a sprite.
Couldn't you just edit the sprite image? Sprites in Unity can be scaled larger than the camera view anyway. You could use the Sprite Packer when importing the sprite, to make it just a crop of the full image.
@Cresspresso I am trying to fit my 2D game's background in iPad (4:3) and iPhone(16:9). When I try to fit the background with 4:3 aspect ratio in a 16:9 aspect ratio, the additional content of the background sprite from the bottom should go outside the camera. The width should remain the same and fit the screen perfectly ( there shouldn't be blank space left out on the left or right side). Also the top region shouldn't go outside the camera. Only the bottom region should go outside the camera. This is what I am trying to do!!
Answer by NKKS123 · Feb 09, 2018 at 10:39 AM
Click on this sprite in Project panel and then in inspector click "Sprite editor", there you can cut your Sprite.
@N$$anonymous$$$$anonymous$$S123 How will I use the same sprite on iPad if I cut the sprite? I will be publishing the game on appstore, which means the same sprite should work for screens with different aspect ratio(for example iPad and iPhone 6). Please read description also before answering.
Answer by ElijahShadbolt · Feb 13, 2018 at 02:06 AM
Assuming the game is 2D,
In the hierarchy, right-click your MainCamera and select [ 2D Object > Sprite ] to create a new GameObject with a SpriteRenderer component.
Assign your Sprite in the SpriteRenderer.
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);
}
}
}
Your answer
Follow this Question
Related Questions
Scale Everything In Game to Fit all screen Sizes 1 Answer
How Reference Resolution of Canvas Scaler affects to sprite size? 0 Answers
How to make my UI items/canvas stretch to fill preset aspect ratio 0 Answers
How to merge two background sprites so that there won't be a line between the merging sides? 0 Answers
Allow a game object to stick to another game object's part? 1 Answer