0
\$\begingroup\$

From the Screen.orientation description I get

If the value is set to ScreenOrientation.AutoRotation then the screen will select [...] automatically as the device orientation changes.

This seems to imply that if the screen does auto-rotate, then Screen.orientation has been set to ScreenOrientation.AutoRotation.

If that's so, how can I know in which direction it has been auto-rotated?

Disabling the auto-rotation doesn't count (obviously), and I need to distinguish between LandscapeLeft and LandscapeRight, so checking height and width doesn't work either.

This should be cross-platform, without including any kind of non-C# plugin.

(the answer, given this documentation, seems to be "you can't", but maybe I'm missing something)

\$\endgroup\$
1
\$\begingroup\$

It should be easy to distinguish between portrait and landscape by checking Screen.height > Screen.width.

From a display perspective you are unable to distinguish between Portrait/PortraitUpsideDown and LandscapeLeft/LandscapeRight.

However it is possible to disable any of these by setting any of the following to false:

  • autorotateToLandscapeLeft
  • autorotateToLandscapeRight
  • autorotateToPortrait
  • autorotateToPortraitUpsideDown

Some coverage of these display issues is here for Android and here for iOS.

However you can determine the device orientation via Input.deviceOrientation

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Unfortunately I need the opposite: I need to distinguish betweek LandscapeLeft/LandscapeRight, and I need not to disable it. So I guess this could be summarised as "you can't"? \$\endgroup\$ – o0'. Sep 22 '14 at 11:21
  • \$\begingroup\$ @Lohoris Please see edit. \$\endgroup\$ – Kelly Thomas Sep 22 '14 at 11:32
  • \$\begingroup\$ Oh, Input.deviceOrientation might be what I need, I'll test it, thanks! \$\endgroup\$ – o0'. Sep 22 '14 at 11:34
  • \$\begingroup\$ I've solved it with WebCamTexture.videoRotationAngle. I'm not posting this as an answer, since I don't know why I didn't use Input.deviceOrientation. \$\endgroup\$ – o0'. Nov 13 '14 at 11:53
0
\$\begingroup\$

I can think of some method, that will return ScreenOrientation from Input.deviceOrientation. But because sometimes deviceOrientation can't be distinguished (e.g. diagonal or parallel to ground) that method will return null in that case:

private ScreenOrientation? DeviceOrientationToScreenOrientation(DeviceOrientation deviceOrientation)
        {
            switch (deviceOrientation)
            {
                case DeviceOrientation.Portrait:
                    if (Screen.autorotateToPortrait)
                    {
                        return ScreenOrientation.Portrait;
                    }
                    break;
                case DeviceOrientation.PortraitUpsideDown:
                    if (Screen.autorotateToPortraitUpsideDown)
                    {
                        return ScreenOrientation.PortraitUpsideDown;
                    }
                    break;
                case DeviceOrientation.LandscapeLeft:
                    if (Screen.autorotateToLandscapeLeft)
                    {
                        return ScreenOrientation.LandscapeLeft;
                    }
                    break;
                case DeviceOrientation.LandscapeRight:
                    if (Screen.autorotateToLandscapeRight)
                    {
                        return ScreenOrientation.LandscapeRight;
                    }
                    break;
            }
            return null;
        }

So we need to wait, until Input.deviceOrientation will return something, we can distinguish. In my game I need to freeze screen from flipping between landscapeLeft & landscapeRight when in game, but allow when in menu. So I start checking for Input.deviceOrientation each half-second after hiding menu. That's how it can be implemented using UniRx:

private void OnEnable()
{
Observable.Interval(TimeSpan.FromMilliseconds(500))
                .Select(_ => DeviceOrientationToScreenOrientation(Input.deviceOrientation))
                .First(orientation => orientation.HasValue)
                .SubscribeUntilDisable(this, orientation =>
                {
                    Screen.orientation = orientation.Value;
                });
}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.