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 SuperMoog · Sep 20, 2015 at 06:44 PM · cameraviewportaspect-ratio

Changing camera aspect ratio with non-fullscreen viewport

Hey,

In my Unity application I have a screen which contains a right hand sidebar and a footer as GUI components. In the upper left of the screen I have my main camera with it's viewport rect adjusted to fit that area. This viewport contains a scene in which the user creates content, hence the sidebar and footer which contain controls (such as place asset). I hope that makes sense.

The content is to be displayed on devices with varying aspect ratios so I allow the user to switch aspect ratio in order to preview what the content would look like on the given device. I control the aspect ratio with a behavior that I derived from: http://wiki.unity3d.com/index.php?title=AspectRatioEnforcer

When the script is used full screen it works fine. By fine I mean that whatever the width and height the screen (free-aspect view) the content appears correctly - as in it is letterboxed/pillarboxed and the exact same area of content is displayed in all cases.

In order to restrict the viewport area in my user interface to the upper left I attempted to add padding variables. The result does render the viewport with the correct position and size but for some reason at certain dimensions the content displayed in the viewport is shifted and this is not acceptable for my use case.

So basically my padding modifications have altered the actual content that appears in the viewport rather than simply resizing the container. What I intended to occur was for the letterboxing/pillarboxing to accommodate the padding and display as per when full screen.

Here is my script:

 public class CameraAspectRatioController : MonoBehaviour
 {
     // Unity visible properties
     public Camera Camera;
     public float PaddingTop;
     public float PaddingLeft;
     public float PaddingBottom;
     public float PaddingRight;
     //

     private float[] aspectRatio = new[]
         {
             // 16:9 - iPhone 5, 6, 6+
             (16.0f/9.0f),

             // 3:2 - iPhone
             (3.0f/2.0f),

             // 4:3 - iPad
             (4.0f/3.0f)
         };

     private int aspectRatioIndex = 0;

     private void Awake()
     {
         if (Camera == null)
             Camera = UnityEngine.Camera.main;

         RenderSizeChangeListener.OnRenderSizeChanged += () => ShiftAspectRatio();
     }

     private void Start()
     {
         ShiftAspectRatio();
     }

     private void Update()
     {
         if (Input.GetKeyUp(KeyCode.UpArrow))
             ShiftAspectRatio(-1);

         if (Input.GetKeyUp(KeyCode.DownArrow))
             ShiftAspectRatio(1);            
     }

     private void ShiftAspectRatio(int diff = 0)
     {
         this.aspectRatioIndex += diff;
         if (this.aspectRatioIndex < 0)
             this.aspectRatioIndex = this.aspectRatio.Length - 1;
         else if (this.aspectRatioIndex > this.aspectRatio.Length - 1)
             this.aspectRatioIndex = 0;

         var targetAspect = aspectRatio[this.aspectRatioIndex];
         SetAspectRatio(targetAspect);
     }

     private void SetAspectRatio(float targetAspect)
     {
         var currentAspect = Width / Height;

         // If the current aspect ratio is already approximately equal to the desired aspect ratio,
         // use a full-screen Rect (with padding) (in case it was set to something else previously)
         if (Mathf.Approximately(currentAspect, targetAspect))
         {
             Camera.rect = new Rect(UnitLeft, UnitBottom, UnitWidth, UnitHeight);
             return;
         }

         // Pillarbox
         if (currentAspect > targetAspect)
         {
             var inset = 1.0f - targetAspect/currentAspect;
             Camera.rect = new Rect(UnitLeft + (inset/2), UnitBottom, UnitWidth-inset, UnitHeight);
         }
         // Letterbox
         else 
         {
             var inset = 1.0f - currentAspect/targetAspect;
             Camera.rect = new Rect(UnitLeft, UnitBottom + (inset/2), UnitWidth, UnitHeight-inset);
         }
     }

     private float Width
     {
         get { return Screen.width - PaddingLeft - PaddingRight; }
     }

     private float Height
     {
         get { return Screen.height - PaddingTop - PaddingBottom; }
     }

     private float UnitWidth
     {
         get { return Width / Screen.width; }
     }

     private float UnitHeight
     {
         get { return Height / Screen.height; }
     }

     private float UnitLeft
     {
         get { return PaddingLeft / Screen.width; }
     }

     private float UnitRight
     {
         get { return (Screen.width - PaddingRight) / Screen.width; }
     }

     private float UnitTop
     {
         get { return (Screen.height - PaddingTop) / Screen.height; }
     }

     private float UnitBottom
     {
         get { return PaddingBottom / Screen.height; }
     }
 }

If anyone has any ideas that would be gratefully appreciated. I hope my question makes sense.

Thanks in advance...

Cheers, Mark

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Ortho camera viewport setup? 3 Answers

How does the aspect ratio work? 0 Answers

How to get a camera to render to render texture at a custom aspect ratio. 1 Answer

How to force the game to be stretched to fit the screen? 1 Answer

Why is the camera aspect ratio broken for the game tab? 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