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.