• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Aravind_Aby · Feb 07, 2018 at 04:03 PM · scalespritesbackgroundaspect-ratioscreen resolution

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!

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hexagonius · Feb 07, 2018 at 10:11 PM 0
Share

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.

avatar image Aravind_Aby hexagonius · Feb 08, 2018 at 04:15 AM 0
Share

@hexagonius But I am not using a UI image here. I am talking about a sprite.

avatar image ElijahShadbolt · Feb 08, 2018 at 02:40 AM 0
Share

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.

avatar image Aravind_Aby ElijahShadbolt · Feb 09, 2018 at 10:21 AM 1
Share

@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!!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Aravind_Aby · Feb 09, 2018 at 11:00 AM 0
Share

@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.

avatar image
0

Answer by ElijahShadbolt · Feb 13, 2018 at 02:06 AM

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);
         }
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

81 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Scale Everything In Game to Fit all screen Sizes 1 Answer

How can i make my game matching all Android Mobile Screen? 2 Answers

Scale gameobject to the width of screen 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges