screen auto rotation forth and back
Hi,
I am developing a story application with games & story in it. In that one of the game developed as portrait and others are in landscape orientation. So when i select that particular game screen needs to auto rotated to portrait even we hold the device as landscape and when exit fro that game it needs to be autorotated to landscape even the device held as portrait. I did scripting for this, but its not automatically rotating, if i rotate the device to those desired orientation, its working.
//To Portrait from Landscape
void Start () {
if (Input.deviceOrientation == DeviceOrientation.LandscapeRight)
{
Screen.orientation = ScreenOrientation.AutoRotation;
Screen.autorotateToPortrait = true;
Screen.orientation = ScreenOrientation.Portrait;
}
else if(Input.deviceOrientation == DeviceOrientation.LandscapeLeft)
{
Screen.orientation = ScreenOrientation.AutoRotation;
Screen.autorotateToPortraitUpsideDown = true;
Screen.orientation = ScreenOrientation.PortraitUpsideDown;
}
else
{
Screen.orientation = ScreenOrientation.AutoRotation;
Screen.autorotateToPortrait = true;
Screen.orientation = ScreenOrientation.Portrait;
}
Screen.autorotateToLandscapeLeft = false;
Screen.autorotateToLandscapeRight = false;
}
//To Landscape from Portrait
void Start () {
if (Input.deviceOrientation == DeviceOrientation.Portrait)
{
Screen.orientation = ScreenOrientation.AutoRotation;
Screen.autorotateToLandscapeRight = true;
Screen.orientation = ScreenOrientation.LandscapeRight;
}
else if(Input.deviceOrientation == DeviceOrientation.PortraitUpsideDown)
{
Screen.orientation = ScreenOrientation.AutoRotation;
Screen.autorotateToLandscapeLeft = true;
Screen.orientation = ScreenOrientation.LandscapeLeft;
}
else
{
Screen.orientation = ScreenOrientation.AutoRotation;
Screen.autorotateToLandscapeRight = true;
Screen.orientation = ScreenOrientation.LandscapeRight;
}
Screen.autorotateToPortrait = false;
Screen.autorotateToPortraitUpsideDown = false;
}
Please let me know is any other way or any scripting changes i have to made to make things possible.
Regards, Ben
Answer by AhadKhan98 · Nov 11, 2015 at 08:55 AM
If you want it to lock it to a only portrait mode then add a gameobject, attach a script and write the following code in the start function.
Screen.orientation = ScreenOrientation.Portrait;
Screen.autorotateToLandscapeLeft = false;
Screen.autorotateToLandscapeRight = false;
Screen.autorotateToPortrait = true;
Screen.autorotateToPortraitUpsideDown = true;
Screen.orientation = ScreenOrientation.AutoRotation;
And if you want to set a specific scene to only landscape then use this code on the other scene.
Screen.orientation = ScreenOrientation.LandscapeLeft;
Screen.autorotateToLandscapeLeft = true;
Screen.autorotateToLandscapeRight = true;
Screen.autorotateToPortrait = false;
Screen.autorotateToPortraitUpsideDown = false;
Screen.orientation = ScreenOrientation.AutoRotation;
Just create two separate scripts for example, ScreenAdjustmentPotrait and ScreenAdjustmentLandscape and use them accordingly.
Thanks for the reply. I will check and share the details. Is there any way to set these things in unity editor widow, means when i open the scene with portrait, game window will be at Tall (768 x 1024) and when i play landscape game scene from there it should automatically switch to wide (1024 x 768)
I don't think you can set unity editor to switch automatically.
Hi, The script mentioned above is not working for the first time when we play portrait scene from landscape scene, we need to rotate the device to make it portrait. And when we switch back to landscape from portrait, its still in portrait mode again have to rotate the device to make it landscape. But for the second time when we play portrait scene from landscape its working, but back to landscape from portrait is not. Is any other solution for this. Is we need to specify for iOS & android.
Hi, did you manage to fix this? I have the same exact problem (screen resolution not changing when I set screen.orientation, but it works I run the android app 2nd time)