Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
3
Question by matyicsapo · Jul 25, 2010 at 07:28 PM · cameraresolutionscreenviewportmatrix

same visible area regardless of aspect ratio

Hi :D

How to make it so that regardless of aspect ratio the camera would show the same portion of the world?

If the whole screen could be stretched, how? If I could add a black border to the sides, how?

(Checked out how others do it so I fired up S.W.A.T. 4 which uses the Unreal Engine and checked the game with both 4:3 and 5:4 and the same could be seen and I didn't notice any stretching so there might really be better ways than those mentioned above.)

What else could one do, how?

If it's sg. with matrices then please consider that one might be a newbie with those (too).

Thanks in advance :)

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

8 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Magnus Wolffelt · Jul 25, 2010 at 10:31 PM

Stretching could probably be achieved by modifying the Camera.aspect property, but would you really want that? It probably makes for a poor visual experience.

And black padding, isn't that also quite poor visuals?

I think most games simply use a fixed FOV (field of view) and don't care much about not showing exactly the same portion of the world.

When you set FOV on a Unity camera it represents the vertical FOV: http://unity3d.com/support/documentation/ScriptReference/Camera-fieldOfView.html

If however you want to keep the horizontal FOV fixed, try something like this:

myCam.fieldOfView = myDesiredHorizontalFov / ((float)myCam.pixelWidth / myCam.pixelHeight);

Note that this needs to run whenever the viewports change. I have not tested this code for correctness. :)

Comment
Add comment · Show 2 · 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 FabioZ84 · Nov 03, 2013 at 06:06 PM 1
Share

Thanks. The idea is perfect for my problem by changing just a few things

float normalAspect = 16/9f;

myCam.fieldOfView = myDesiredHorizontalFov * normalAspect / ((float)myCam.pixelWidth / myCam.pixelHeight);

avatar image NewPath · Jul 01, 2015 at 10:31 AM 0
Share

This works much better than repositioning the camera. Thanks!

avatar image
3

Answer by Eagle32 · Jul 26, 2010 at 01:22 AM

If you want to letter/pillar box then something like the below script is probably what you want. Note that it is not a complete generalised solution, it is only a demonstration. It assumes the aspect ratio you want for the viewport is 16:9.

using UnityEngine; using System.Collections;

[RequireComponent(typeof(Camera))] public class LetterBoxing : MonoBehaviour {

 const float NORMAL_ASPECT = 4/3f;
 const float COMPUTER_WIDE_ASPECT = 16/10f;
 const float EPSILON = 0.01f;

 void Start () 
 {
     float aspectRatio = Screen.width / ((float)Screen.height);

     if (Mathf.Abs(aspectRatio - NORMAL_ASPECT) < EPSILON)
     {
         camera.rect = new Rect(0f, 0.125f, 1f, 0.75f); // 16:9 viewport in a 4:3 screen res
     }
     else if (Mathf.Abs(aspectRatio - COMPUTER_WIDE_ASPECT) < EPSILON)
     {
         camera.rect = new Rect(0f, 0.05f, 1f, 0.9f); // 16:9 viewport in a 16:10 screen res
     }

     //  everything else is assumed to be 16:9.
 }

}

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
avatar image
3

Answer by -hiTo- · Apr 02, 2014 at 11:44 AM

I found this helpful, but I needed a general solution, so I extended @PeterGriffin3's solution by a bit. I thought I should share it;

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(Camera))]
 public class Letterboxing : MonoBehaviour 
 {
     const float KEEP_ASPECT = 16/9f;
     
     void Start()
     {
         float aspectRatio = Screen.width / ((float)Screen.height);
         float percentage = 1 - (aspectRatio / KEEP_ASPECT);
 
         camera.rect = new Rect(0f, (percentage / 2), 1f, (1 - percentage));
     }
 }

Now of course you could change the KEEP_ASPECT to anything you like.

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 wolfomat · Apr 21, 2021 at 03:32 PM 0
Share

hey there. this did not work, at least not in editor preview. i did attach this script do "MainCamera" and called instead of camera.rect -> Camera.Main.rect...

any ideas?

avatar image
1

Answer by PeterGriffin3 · Oct 25, 2012 at 10:32 AM

Hey the script works just fine for me! But I need to support the ratio of 5:4 as well and I just didnt get really how you calculated the rectangles for the different ratios! It would be very kind when someone can help me out!

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
avatar image
1

Answer by chrisleathers · Sep 27, 2013 at 08:44 PM

This is probably a little basic and I am definitely a very new programmer. but I have the problem of having to output a project to multiple devices with different aspect ratios, and this is what I am doing so far:

PS - my cameras are all positioned in world space. I figure out what Z position looks good for each aspect ratio, and then set the z for the camera, based on the aspect ratio I am running or building to.

//---------------

var ratio0403 : boolean = true; // this setting also works for 5:4 in my case

var ratio0302 : boolean = false;

var ratio1610 : boolean = false;

var ratio1609 : boolean = false;

var CamX : GameObject; // drag camera or parent of camera to this slot

// probably I could run this in the start function as long as I set it up before building

function Update () {

// camera X set positions

CamX.transform.position.z = -6.4;

if (ratio0403 == true) { CamX.transform.position.z = -6.4; }

if (ratio0302 == true) { CamX.transform.position.z = -5.65; }

if (ratio1610 == true) { CamX.transform.position.z = -5.3; }

if (ratio1609 == true) { CamX.transform.position.z = -4.75; }

}

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
  • 1
  • 2
  • ›

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

8 People are following this question.

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

Related Questions

2D Game. Screen, Camera and coordinates. 0 Answers

4:3 aspect ratio 0 Answers

Orthographic cameras and screen resolutions 1 Answer

Best way to downsample camera viewport? 1 Answer

Why Is My Orthographic Camera Not Rendering at the Full Resolution? 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