Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by farazahmad759 · Sep 05, 2017 at 05:46 AM · layoutaspect-ratiocamera viewportscreen resolutionscreen size

Adjusting the Camera's Viewport Rect automatically upon aspect ratio change

I'm developing a game that has a basic GUI structure as below (for 16:9 aspect ratio. Layout Structure (when Aspect ratio is 16:9)

But when the aspect ratio changes (say becomes 16:10), the layout changes to the one like: Layout structure (when Aspect ratio is 16:10) Which means a horizontal blank space is created between the Camera viewport rect and the lower Canvas element. The lower canvas elements has fixed height (218 pixels) and it always stay at the bottom edge of the screen. What I want is to stretch to touch the upper edge of the Canvas element always (or in other words remove the blank space between these two parts).

I've already optimized the Canvas elements for Aspect ratio change, but Camera Viewport not being a canvas element is not adjusting.

I've already tried with Camera's Rect and PixelRect classes but no success.

For an aspect ratio 16:9, and screen size 1366 x 768. So subtracting 218 (the height of lower canvas element) I get 550 = the height of Canvas Viewport Rect. So according to my calculations the ViewPort Rect's height should be 550/768 = 0.716 which is fine and works. But when aspect ratio is 16:10 and screen size 1228 x 768. Now, doing the same above calculations, Height of canvas rect in pixels = 768 - 218 = 550 Viewport rect height = 550/768 = 0.716, but this time I get that blank horizontal space.

Please suggest me how do I fix this.

Thanks in advance.

screen-shot-2017-09-05-at-102111-am.png (42.6 kB)
screen-shot-2017-09-05-at-102810-am.png (55.9 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Glurth · Sep 05, 2017 at 06:46 AM

Trying using the following configuration for your camera, and canvas panels (respectively). It set each component as a fraction of the total screen height and width. It assumes your canvas is configured for "Screen Space Overlay". alt text


canvas-and-screen-config.png (12.7 kB)
Comment
Add comment · Show 3 · 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 farazahmad759 · Sep 05, 2017 at 09:39 AM 0
Share

Thanks for the answer but I can edit the size and position of Viewport from the inspector. What I want is to edit from the script, so that when screen size changes (due to device change), its size and position is automatically adjusted, just like I've implemented it for Canvas elements.

Basically I'm having difficulty in "calculating" the height of the Viewport. $$anonymous$$y calculations yield different result than the actual Viewport Rect.height value needed.

avatar image Glurth farazahmad759 · Sep 05, 2017 at 05:54 PM 0
Share

I'm not sure I understand. With the configuration above, it automatically adjusts the layout based on current aspect-ratio. No calculations are necessary, and no extra spaces are introduced.

avatar image Glurth Glurth · Sep 05, 2017 at 05:59 PM 0
Share

Ah, I see what I missed in your Q now...." The lower canvas elements has fixed height (218 pixels) " I missed this previously. If you fix this height to a number of pixels, and then change the output/device aspect-ratio; your camera-viewport area, will NOT have the same aspect ratio as the output/device. Is that what you want?

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

68 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

Related Questions

Camera Fit Issues on 10 inch tablet 0 Answers

How to make my UI items/canvas stretch to fill preset aspect ratio 0 Answers

Custom aspect ratio in build 0 Answers

Fill screen with Unity WebGL while maintaining display ratio 0 Answers

Unity3d How to support different screen size in Portrait orientation 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