Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
Question by siddharth3322 · Jan 16, 2014 at 06:00 AM · unity 2dresolutionunity4.3

Targeting multiple display resolution in 2D games

I am trying to create 2D game in Unity but I want to consider wide variety of android devices for this. But I found that game area is cropped differently per resolution change.

Also I already run demo 2D Platformer game in different devices but I found same situation in this game also.

2D Platformer

So how to handle this situation in Unity 2D game development?

Comment
Add comment · Show 3
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 DynamicPrgm · Jan 23, 2014 at 02:58 AM 0
Share

The way I do it (for GUI, I'm not sure if 2D is the same) is by using percentages of the screen. If 800 is x percent of resolution that I'm using to develop, then use that percent to calculate for different aspect ratios. Then you can handle most aspect ratios! alt text

alt text

screen shot 2014-01-22 at 8.56.49 pm.png (24.4 kB)
screen shot 2014-01-22 at 8.57.02 pm.png (23.4 kB)
avatar image siddharth3322 · Jan 23, 2014 at 03:16 AM 0
Share

Thanks for your reply but you put it as answer not create so much long comment so that we discuss this thing more and I can give you a point.

avatar image siddharth3322 · Jan 31, 2014 at 07:36 AM 0
Share

Please friends I need some help in this.

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by siddharth3322 · Jan 16, 2014 at 02:23 PM

I got my thing worked by using following code snippets. I pasted it for helping other members.

 public class ControllingCameraAspectScript : MonoBehaviour
 {
 
     // Use this for initialization
     void Start()
     {
         // set the desired aspect ratio (the values in this example are
         // hard-coded for 16:9, but you could make them into public
         // variables instead so you can set them at design time)
         float targetaspect = 16.0f / 9.0f;
 
         // determine the game window's current aspect ratio
         float windowaspect = (float)Screen.width / (float)Screen.height;
 
         // current viewport height should be scaled by this amount
         float scaleheight = windowaspect / targetaspect;
 
         // obtain camera component so we can modify its viewport
         Camera camera = GetComponent<Camera>();
 
         // if scaled height is less than current height, add letterbox
         if (scaleheight < 1.0f)
         {
             Rect rect = camera.rect;
 
             rect.width = 1.0f;
             rect.height = scaleheight;
             rect.x = 0;
             rect.y = (1.0f - scaleheight) / 2.0f;
 
             camera.rect = rect;
         }
         else // add pillarbox
         {
             float scalewidth = 1.0f / scaleheight;
 
             Rect rect = camera.rect;
 
             rect.width = scalewidth;
             rect.height = 1.0f;
             rect.x = (1.0f - scalewidth) / 2.0f;
             rect.y = 0;
 
             camera.rect = rect;
         }
     }
 
 }
 
Comment
Add comment · Show 4 · 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 siddharth3322 · Jan 22, 2014 at 10:03 AM 0
Share

Still exist black bars in some resolution. So how to remove that black bars? This works great in pc but in mobile devices have no effect.

avatar image Tricephalus · Jun 18, 2014 at 03:34 PM 0
Share

This is a bit unrelated, but I'm still fresh to C# and I have a small doubt about that code you posted.

This line: float windowaspect = (float)Screen.width / (float)Screen.height;

wouldn't it be exactly the same as: float windowaspect = Screen.width / Screen.height;

I don't understand the purpose of those float within parentheses...

avatar image tanoshimi · Jun 18, 2014 at 03:39 PM -1
Share

Completely off-topic, @Tricephalus... Screen.width is an integer, Screen.height is an integer. Divide an integer by an integer and what do you get?

If you said "the wrong answer", score 10 points. That's what you (cast) them as floats first.

avatar image Tricephalus · Jun 18, 2014 at 04:33 PM 0
Share

Got it! Thank you

I don't know why, but I assumed that without casting them as floats, since you had cast the variable as being a float, the operation would return a float.

Live and learn :)

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.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

How could I deal with the resolution and camera size? 0 Answers

How to make Unity Game fit any resolution when resizing the window? 0 Answers

problem with building my game 1 Answer

Unity APP didn't load on the Chrome browser,Unite didn't load on the Chrome 0 Answers

2D GAME (non UI object) for every pc resolution 0 Answers


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