- Home /
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.
By wrong you mean inverted? The value that should be in with is set to height and vice-versa?
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.
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.
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();
}
}
Thank you, that works! The explanation makes sense too :)
$$anonymous$$ake sense, but it should't be like this. It's confusing.
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
Follow this Question
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