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
0
Question by Zyrrith · Oct 20, 2012 at 09:05 PM · androidscreenorientationscreen.widthscreen.height

Android screen orientation: why am I getting wrong screen size values?

Hi there,

I've run into a problem that I can't seem to figure out. My goal is to execute some code after the screen orientation has changed; for example when the device has been rotated from landscape to portrait mode. The actual function call works fine, but when I try to receive the dimensions of the screen, while they were perfectly fine just before the function was executed! The problem occurs in an otherwise empty project and scene, on both a Samsung Galaxy Tab 2 and a Samsung Galaxy S2.

     private ScreenOrientation screenOrientation;
     private float screenWidth;
     private float screenHeight;
     private float wrongWidth;
     private float wrongHeight;
     
     public void Update()
     {
         //These values are right
         this.screenWidth = Screen.width;
         this.screenHeight = Screen.height;
         
         if (this.screenOrientation != Screen.orientation)
         {
             this.DoWhatever();
             this.screenOrientation = Screen.orientation;
         }
     }
     
     public void DoWhatever()
     {
         //These values are wrong
         this.wrongWidth = this.screenWidth;
         this.wrongHeight = this.screenHeight;
     }
     
     public void OnGUI()
     {
         //These are right
         string rightScreenSize = this.screenWidth.ToString() + "x" + this.screenHeight.ToString();
         GUI.Label(new Rect(Screen.width / 2, Screen.height / 2, 200, 50), "Right: " + rightScreenSize);
         
         //These are wrong
         string wrongScreenSize = this.wrongWidth.ToString() + "x" + this.wrongHeight.ToString();
         GUI.Label (new Rect(Screen.width / 2, Screen.height / 2 + 50, 200, 50), "Wrong: " + wrongScreenSize);
     }

Thanks for reading, I'd appreciate any help.

Comment
Add comment · Show 3
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 hvilela · Oct 20, 2012 at 10:18 PM 0
Share

By wrong you mean inverted? The value that should be in with is set to height and vice-versa?

avatar image Zyrrith · Oct 20, 2012 at 10:30 PM 0
Share

Sorry, I should have included that. In landscape mode, I get portrait mode values and vice versa. On a Galaxy S2 with 800x480 resolution, I get 480x762 (-38px status bar) in landscape mode, and 800x442 in portrait mode.

I just don't understand how the screenWidth and screenHeight values are perfectly fine in the Update function, and then as soon as the DoWhatever function is called and the values are copied into different variables, the values turn bad.

avatar image Zyrrith · Oct 20, 2012 at 11:26 PM 0
Share

I thought there might be a longer delay, caused by the OS, to allow for it's flipping animation to finish. I did a little test by counting the frames between the change of Screen.orientation and the change of actual Screen.width and Screen.height values. Varying results. Sometimes it takes up to 15 frames for them to change, but mostly around 8.

1 Reply

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

Answer by hvilela · Oct 20, 2012 at 10:38 PM

I'm guessing that is not related to the "DoWhatever" method call. I strong believe that if you move the two lines that are outside to inside the if you'll get the same values. What I guess is that Android is changing the value of Screen.orientation in one frame and the actual values for Screen.width and Screen.height in the next frame.

Please, try this:

 public void Update() {
     if (this.screenWidth != Screen.width || this.screenHeight = Screen.height) {
         this.screenWidth = Screen.width;
         this.screenHeight = Screen.height;
         this.DoWhatever();
     }
 }
Comment
Add comment · Show 3 · 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 Zyrrith · Oct 20, 2012 at 10:44 PM 0
Share

Thank you, that works! The explanation makes sense too :)

avatar image hvilela · Oct 20, 2012 at 10:46 PM 1
Share

$$anonymous$$ake sense, but it should't be like this. It's confusing.

avatar image Bunny83 · Oct 20, 2012 at 11:01 PM 0
Share

Totally agree. The static variables represents the current state of the screen (so it's a sort of a state machine). When the screen orientation changes that means it's now in a different state and all related values should reflect this state.

$$anonymous$$aybe this is already a "bug" from the OS and Unity just returns what the system is returning, but it's clearly not a convinient behaviour.

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

10 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

Related Questions

Screen.width and Screen.height sometimes flipped for Android 1 Answer

Screen.width & height not updated after screen rotation 1 Answer

Do screen.width and screen.height get updated depending on screen orientation on Android? 1 Answer

When are Screen.width and Screen.height updated after changing the Screen.orientation value? 0 Answers

Unity 3.5.7 Android Screen returns the wrong value? 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