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
2
Question by Wisearn · Oct 28, 2016 at 04:08 PM · scalewindowviewportwidthcrop

Scale viewport/game by width instead of height?

Hello unitycommunity!

I've noticed that by default, when I make a build of my game and I change the screen WIDTH, the window will crop away parts of the game.

But when I change the screen HEIGHT of my game, all the game content zooms in and becomes bigger.

I need the exact opposite effect, so that changing width will make the game change in size, and changing screen height will crop or show more of the game.

Here's a picture: effect

game-viewport-scaling.png (63.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
1

Answer by oStaiko · Oct 28, 2016 at 08:00 PM

So, for an aspect ratio, you are using free aspect. This allows players to size the screen however they want. With cameras in free aspect, this is the default of how it sizes things up, and after a bit of research there's no easy way to do this. If you want to switch it as you've said above, then you'll have to script it in yourself. You can do that With a combination of screen resolution, and camera projection matrix. This gets tough and requires some non-default camera options, which you can read more on at the Screen API and the Projection Matrix API.

 function Start ()
 {
     wScaleFactor=cam.projectionMatrix.m00; //Sets base scale
     hScaleFactor=cam.projectionMatrix.m11; //Otherwise it'll look flat
 }

 function Update ()
 {
     cam.ResetProjectionMatrix();
 
     var m = cam.projectionMatrix;
 
     m.m00 = wScaleFactor;    //width scale remains constant
     m.m11 = (Screen.width/Screen.height) * hScaleFactor;   //height scale changes to match screens height
 
     cam.projectionMatrix = m;
 }

Now this script works fine on it's own, however in my tests it was full of unexplainable bugs. Use it as a basis to get you started, and along with the APIs I've included you should do just fine. The bugs for me were getting a black screen when width was less than height, and some odd jumps between scales. This probably has to do with the matrix being a large trig function but I'll leave that to you to figure out...


Another (unprobable) lead I found for that is to actually rotate the camera 90 degrees. Then when you drag the height, you're actually dragging the width which makes it how you want. I suppose if you rotate all of your UI and your monitor 90 degrees it'll be perfect, right?

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.

Follow this Question

Answers Answers and Comments

59 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

Related Questions

How to realize Image.fillAmount with Sprites in game? 0 Answers

Make long distance objects visible on camera 0 Answers

Camera picture stretching when width changed in script? 1 Answer

Is it better to enlarge an image by width/height or by scale? 0 Answers

Is it possible to quit the game before the window appears? 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