- Home /
Detect Device Orientation through Script
As the title suggest, I want to detect device orientation change at run time. So that I have to write script for that. Basically I am planning to develop 2d game that work on both orientation.
But how to detect that is basic question? Because I am pretty new to this engine.
I have other questions in mind. On android devices, I want to run my game in landscape mode in tablet and in portrait mode in mobile (Small Devices).
On iOS devices, I want to run my game in landscape mode in iPad and in portrait mode in iPhone.
How to perform this through script?
Answer by ejpaari · Mar 26, 2014 at 07:52 AM
You could change the layout based on Input.deviceOrientation. Something like
void Update() {
if (Input.deviceOrientation == DeviceOrientation.LandscapeLeft) {
UseLandscapeLeftLayout();
}
else if (Input.deviceOrientation == DeviceOrientation.Portrait) {
UsePortraitLayout();
}
}
In those UseLayout-functions you could for example change the position of GUI-elements or change what will be drawn OnGUI().
Basically you don't understand my point. I want to change my game layout as orientation changes. By default half screen get visible. I want to do something like Candy Crush has. So treat my question in that manner.
Thanks for your nice suggestion. At present I up vote your answer but you have to give two more question answer to mark this answer as correct. That I have asked for android and iOS.
So there is no "event" for when the device orientation changes?
Above answer give you, orientation change correctly.
Your answer
![](https://koobas.hobune.stream/wayback/20220613141208im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Shake Device on Orientation Change 0 Answers
Game support for landscape and portrait 0 Answers
Retrieve Continuous Touch Coordinate 0 Answers
Know Which Button is Clicked 0 Answers
How to set game scene according to iPad and iPhone for 2D game in Unity4.3 1 Answer