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 /
  • Help Room /
avatar image
0
Question by enderelectrics · Apr 22, 2020 at 02:47 PM · unity 2daspect-ratioaspect ratio

Resizable Window with Fixed Aspect Ratio?

Hey there! I've had this problem with every game I created up to this point,here's my problem:

I make 2D games. Things that are far away, like background, enemies, etc., don't load when the camera isn't close. I also really like Resizable Windows. But this means people can take the Resizable Window, drag it down, and instantly see the entire level, whether it's loaded or not. So how do I make it so that the window will always stay the same RESOLUTION, but not the same SIZE?

Again, this is a problem I've had with EVERY SINGLE GAME I ever created in Unity, and now I'm finally sick of it.

I saw someone post this:

V V V

private var lastWidth : int = 0; private var lastHeight : int = 0;

function Start () {

}

function Update () { var width = Screen.width; var height = Screen.height;

 if(lastWidth != width) // if the user is changing the width
 {
     // update the height
     var heightAccordingToWidth = width / 16.0 * 9.0;
     Screen.SetResolution(width, Mathf.Round(heightAccordingToWidth), false, 0);
 }
 else if(lastHeight != height) // if the user is changing the height
 {
     // update the width
     var widthAccordingToHeight = height / 9.0 * 16.0;
     Screen.SetResolution(Mathf.Round(widthAccordingToHeight), height, false, 0);
 }

 lastWidth = width;
 lastHeight = height;

}

But that's JavaScript. It doesn't work with C#. Any ideas?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Vivien_Lynn · Mar 13, 2021 at 02:22 PM

I am not sure if it is possible to resize the window while keeping a constant resolution. As far as I understand does the window size determent the resolution.

Maybe you can have some use for the script you can find here: https://forum.unity.com/threads/aspect-ratio-controller-enforce-window-aspect-ratio.599815/ With this script you can limit the min and max size of your game-window, to keep it in a range that does not break the game-experience.

Here is your java scrip in c#:

 using UnityEngine;
 
 public class AdjustWindow : MonoBehaviour
 {
     private int lastWidth = 0;
     private int lastHeight = 0;
 
     void Update()
     {
         var width = Screen.width; var height = Screen.height;
 
         if (lastWidth != width) // if the user is changing the width
         {
             // update the height
             var heightAccordingToWidth = width / 16.0 * 9.0;
             Screen.SetResolution(width, (int)Mathf.Round((float)heightAccordingToWidth), false, 0);
         }
         else if (lastHeight != height) // if the user is changing the height
         {
             // update the width
             var widthAccordingToHeight = height / 9.0 * 16.0;
             Screen.SetResolution((int)Mathf.Round((float)widthAccordingToHeight), height, false, 0);
         }
         lastWidth = width;
         lastHeight = height;
     }
 }
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
0

Answer by wmjoers · Jan 19 at 12:15 AM

Check out this sample projekt https://github.com/wmjoers/CameraScaler! The CameraScaler script lets you keep the aspect ratio or you can let it grow in any direction depending on your needs. Also note that you need to use a second camera to make sure you get a black letter box (it's in the sample scene).

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

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

Related Questions

Unity 2018 Custom Aspect Ratio Not Being Enforced When For Test Build 0 Answers

Unity built game resolution issue 1 Answer

How can I scale a scrollview item so it keeps its ratio? 0 Answers

How to Remove the black bar at my game window? 0 Answers

How to maintain width at lower aspect ratios? 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