• 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
Question by nbst84 · Aug 02, 2014 at 12:02 PM · windowsstandalonewindowresizeaspect ratio

Force aspect ratio of window (standalone Windows)

Hi,

I am designing an application that is supposed to run exclusively in a 1:1 window. No fullscreen, no different aspect ratio.

I have been searching high and low for the following but cannot seem to find it out:

1/ How do I prevent ALT+Enter from switching to fullscreen?

2/ How do I force the aspect ratio of the window, when the window is resized by the user?

My player options are set so that fullscreen is disabled by default, the window resolution is 800x800 and the window is resizable. I cannot see any built-in option for any of the problems above.

I am guessing (and hoping) it is possible to script a solution (in the window resize event, force the ratio?) but I cannot figure it out.

Is that possible?

Thanks!

Comment

People who like this

0 Show 0
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

Answer by fafase · Aug 02, 2014 at 12:05 PM

You could try something like this:

     void Start()
 {
     Screen.SetResolution (800, 800, false);
 }
 void Update()
 {
     if (Screen.fullScreen || Camera.main.aspect != 1) 
     {
         Screen.SetResolution (800, 800, false);
     }
 }

This is just a start as it will flicker when switching to full mode, but that could get you starting.

Comment

People who like this

0 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 nbst84 · Aug 02, 2014 at 01:00 PM 0
Share

Hey, thanks for the reply.

Using your approach I have come up with this:

     int lastWidth = Screen.width;
     
     void Update () {
         if (Camera.main.aspect != 1) {
             if (Screen.width != lastWidth) {
                 // user is resizing width
                 Screen.SetResolution(Screen.width, Screen.width, false);
                 lastWidth = Screen.width;
             } else {
                 // user is resizing height
                 Screen.SetResolution(Screen.height, Screen.height, false);
             }
         }
     }

Which is close to what I want - the window can be resized but resizing it will keep it square. The window moves in the process, which is not very nice looking. Also I am not sure why, but using this code, hitting ALT+ENTER almost crashes the Windows window manager (some of my other Windows apps crashed). I guess that could be prevented by killing the ALT+ENTER event somehow. I would keep this as a solution if:

a) I was able to kill ALT+ENTER

b) I was able to prevent the window from recentering on screen each time the resolution is set (e.g. if resizing from E or S, it should anchor at NW corner, and vice versa)

avatar image fafase · Aug 02, 2014 at 06:55 PM 0
Share

Using alt enter makes a few flickerings, I would think it culd be due to the fact that it is happening in Update but the redrawing is done with the refresh rate. So if both are not synchronized, you could end up with multiple calls to SetResolution.

Using an approach that somehow wait and prevent to call many times, it seems to avoid the weird flickering. On the other hand, you get a full screen for a short time:

     int lastWidth = Screen.width;
 bool isReseting = false; 
 void LateUpdate () {
     if (Camera.main.aspect != 1 && !isReseting) {
         if (Screen.width != lastWidth) {
             // user is resizing width
             StartCoroutine(SetResolution ());
             lastWidth = Screen.width;
         } else {
             // user is resizing height
             StartCoroutine(SetResolution ());
         }
     }
 }
 IEnumerator SetResolution(){
     isReseting = true;
     Screen.fullScreen = ! Screen.fullScreen;
     Screen.SetResolution(Screen.width, Screen.width, false);
     yield return new WaitForSeconds(0.5F);
     isReseting = false;
 }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How to make standalone player resizable? 2 Answers

RenderTexture contents lost on Standalone Window resize 1 Answer

Unity Standalone CommandLineArgs 0 Answers

Force the minimum resolution of the resizable window? 0 Answers

Standalone player in windows open fullscreen, but player settings says windowed 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