- Home /
How to allow automatic screen rotation in iOS?
Can Unity automatically rotate screen when I for example turn device in my hands from "Portrait" position to "Portrait Upside Down" position? Is it possible in Unity at all?
Answer by JGeorge · Feb 22, 2011 at 04:34 PM
Here, use this code. It will throw some warnings in Unity because there are some newer methods, but they all still work. I just used this in a recent app.
if(/Vector3.Dot(Input.acceleration.normalized, new Vector3(0,-1,0)) > 0.8/Input.deviceOrientation == DeviceOrientation.Portrait)
{
iPhoneSettings.screenOrientation = iPhoneScreenOrientation.Portrait;
}
else if(/Vector3.Dot(Input.acceleration.normalized, new Vector3(1,0,0)) > 0.8/Input.deviceOrientation == DeviceOrientation.LandscapeRight)
{
iPhoneSettings.screenOrientation = iPhoneScreenOrientation.LandscapeRight;
}
else if(/Vector3.Dot(Input.acceleration.normalized, new Vector3(-1,0,0)) > 0.8/ Input.deviceOrientation == DeviceOrientation.LandscapeLeft)
{
iPhoneSettings.screenOrientation = iPhoneScreenOrientation.LandscapeLeft;
}
else if(/Vector3.Dot(Input.acceleration.normalized, new Vector3(0,1,0)) > 0.8/Input.deviceOrientation == DeviceOrientation.PortraitUpsideDown)
{
iPhoneSettings.screenOrientation = iPhoneScreenOrientation.PortraitUpsideDown;
}
The main difference is that it's now Screen.orientation
and ScreenOrientation.Portrait
etc. Also the formatting messed up the comments in your code, it took me a while to understand what was going on there...
I can't figure out the formatting. I'm getting "Operator '/' cannot be applied to operands of type 'double' and 'UnityEngine.DeviceOrientation'"
Answer by menneske · Jul 13, 2012 at 08:14 AM
Open player settings (Edit -> Project Settings -> Player) and open the "Resolution and Presentation" section.
Select "Auto Rotation" for the "Default Orientation" and select the orientations you would like to support.
+1 for updated answer. The question wsa 1 and half years old here. almost 2 years now.
Your answer
Follow this Question
Related Questions
Hey I would like to learn via script how to change automatically the screen orientation 1 Answer
How to have a scene rotate with the device orientation 0 Answers
Detect Orientation change at runtime? 0 Answers
How do you handle screen rotation in Unity 3.3? 3 Answers
iOS8 Screen Orientation Change 0 Answers