Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
4
Question by Nanako · May 18, 2015 at 08:59 PM · screenresize

How do i check if the screen/GameWindow has changed size?

is there any event or monobehaviour that is triggered when the game is resized, or even when the game window in the editor is resized? I'd like to make UI elements which can cope with a dynamic screen size

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

4 Replies

· Add your reply
  • Sort: 
avatar image
-3
Best Answer

Answer by redeemer · May 18, 2015 at 10:18 PM

I think a quick workaround would be something like this

 using UnityEngine;
 using System.Collections;
 
 public class ScreenTest : MonoBehaviour {
 
     Resolution res;
 
     // Use this for initialization
     void Start () {
     
         res = Screen.currentResolution;
 
     }
     
     // Update is called once per frame
     void Update () {
     
         if (res != Screen.currentResolution) {
         
             //Do stuff
 
             res = Screen.currentResolution;
 
         }
 
     }
 
 }
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 nitzanwilnai · Jun 07, 2017 at 08:34 PM 0
Share

You can't compare Screen.currentResolution, you need to compare the width and height separately.

avatar image J3-Gaming · Jan 24, 2018 at 03:47 PM 0
Share

Not sure why this is a best answer, code doesn't even compile you cannot compare Resolution variables out of the box and .Equals will also not work.

avatar image
17

Answer by J3-Gaming · Jan 24, 2018 at 03:57 PM

 private Vector2 resolution;
 
 private void Awake()
 {
     resolution = new Vector2(Screen.width, Screen.height);
 }
 
 private void Update ()
 {
     if (resolution.x != Screen.width || resolution.y != Screen.height)
     {
         // do your stuff
 
         resolution.x = Screen.width;
         resolution.y = Screen.height;
     }

 }



I am developing an iOS/Android app, and I don't need this code running while on a phone, so consider this:

 #if UNITY_EDITOR
     private Vector2 resolution;
     #endif //UNITY_EDITOR
     
     private void Awake()
     {
         #if UNITY_EDITOR
         resolution = new Vector2(Screen.width, Screen.height);
         #endif //UNITY_EDITOR
     }
     
     private void Update ()
     {
         #if UNITY_EDITOR
         if (resolution.x != Screen.width || resolution.y != Screen.height)
         {
             // do stuff
     
             resolution.x = Screen.width;
             resolution.y = Screen.height;
         }
         #endif //UNITY_EDITOR
     }






Very late @Nanako hoping to help the next person on google.

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 Kneasle · Aug 22, 2018 at 01:50 PM 0
Share

You helped me (as the next person from google). Well done!

avatar image YoloJero · Feb 13, 2019 at 10:49 AM 2
Share
  1. You are comparing float values directly there! So either use $$anonymous$$athf.Approximately or Vector2Int

  2. why do you convert/store the two values to a Vector2 if than you anyway use the single values again for comparing? you could as well simply use

     private int resolutionX;
     private int resolutionY;
    
    
  3. If using #if UNITY_EDITOR you should do it on the entire class and not leaving empty Unity messages back like Awake, Start and Update. Reson: If they exist they are called even if they technically do nothing -> causing unnecessary overhead.

So overall it should probably rather be

 #if UNITY_EDITOR

     private int resolutionX;
     private int resolutionY;
  
     private void Awake()
     {
          resolutionX = Screen.width
          resolutionY = Screen.height;
      }
  
      private void Update ()
      {
          if (resolution.x == Screen.width && resolution.y == Screen.height) return;
      
          // do stuff
  
          resolutionX = Screen.width;
          resolutionY = Screen.height;
      }
  
 #endif //UNITY_EDITOR
avatar image
1

Answer by FortisVenaliter · May 18, 2015 at 09:49 PM

Well, with the new UGUI in Unity 5, you can use anchoring which mostly eliminates that problem entirely.

But if that doesn't work for you... I'm not really sure about an event off the top of my head, but you could check the Screen.width and Screen.height variables for changes manually.

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 Nanako · May 18, 2015 at 10:06 PM 0
Share

anchoring is nice, but i'm doing a layout with variable rows and columns, are there automated tools to help with that?

avatar image FortisVenaliter · May 18, 2015 at 10:23 PM 0
Share

Like this?

avatar image
1

Answer by unclejey34 · Feb 26, 2020 at 02:07 PM

you can use this event

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 kimbaudi · Apr 03, 2020 at 07:47 PM 0
Share

I've been playing around with OnRectTransformDimensionsChange to detect screen size/orientation changes in both Unity Editor and on my mobile device. What I noticed is that OnRectTransformDimensionsChange does NOT get called when I run my program in Unity Editor and switch between "1280x720 Portrait" and "1280x720 Landscape", which is what I expected to happen. As it turns out, it is because my Canvas had Pixel Perfect disabled. So to ensure that OnRectTransformDimensionsChange detects all screen size/orientation changes, you need to enable Pixel Perfect on your Canvas.

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

29 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

Related Questions

Resolution Dialog missing for Linux? 1 Answer

GUI Resize Mobile 3 Answers

Script for resize aspect ratio for every device (andorid 2D) ?? 0 Answers

how to get Gui in OnGUI function to resize with the window? 1 Answer

Testing orthographic camera at various resolutions -- detect window resize? 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