Created
December 10, 2024 00:51
-
-
Save AldeRoberge/524b8c486ebfd6fa53ac75dc8e8bb760 to your computer and use it in GitHub Desktop.
Allows to give permission to the user (or disallow) screen rotation on a per scene basis (add the script to a game object in the scene)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Sirenix.OdinInspector; | |
using System; | |
using UnityEngine; | |
namespace Runtime.Scripts.Device | |
{ | |
// Allows to give permission to the user (or disallow) screen rotation on a per scene basis (add the script to a game object in the scene) | |
public class DeviceOrientation : MonoBehaviour | |
{ | |
[SerializeField] private bool _isLocked; | |
// Remove the following line if you don't have Odin | |
[ShowIf(@"@!" + nameof(_isLocked)), SerializeField] | |
private DeviceScreenOrientationPermissions _permissions; | |
private float _elapsedTime; | |
private ScreenOrientation _currentScreenOrientation = ScreenOrientation.Unknown; | |
public static event Action<OrientationChangeContext> OrientationChanged = delegate { }; | |
private bool _isEnabled = false; | |
private void Start() | |
{ | |
Debug.Log("[DeviceOrientation] DeviceOrientation Start"); | |
if (!_isEnabled) | |
return; | |
if (_isLocked) | |
{ | |
Debug.Log($"[DeviceOrientation] Locking to current screen orientation and setting auto rotation permission for scene {gameObject.scene.name}"); | |
LockToCurrentScreenOrientation(); | |
return; | |
} | |
Debug.Log($"[DeviceOrientation] Setting auto rotation permission for scene {gameObject.scene.name}"); | |
SetAutoRotationPermission(); | |
InvokeRepeating(nameof(EvaluateDeviceOrientation), 0.1f, 1f); | |
} | |
private void LockToCurrentScreenOrientation() | |
{ | |
switch (Screen.orientation) | |
{ | |
case ScreenOrientation.Portrait: | |
Screen.orientation = ScreenOrientation.Portrait; | |
break; | |
case ScreenOrientation.PortraitUpsideDown: | |
Screen.orientation = ScreenOrientation.PortraitUpsideDown; | |
break; | |
case ScreenOrientation.LandscapeLeft: | |
Screen.orientation = ScreenOrientation.LandscapeRight; | |
break; | |
case ScreenOrientation.LandscapeRight: | |
Screen.orientation = ScreenOrientation.LandscapeLeft; | |
break; | |
} | |
Screen.autorotateToLandscapeLeft = false; | |
Screen.autorotateToLandscapeRight = false; | |
Screen.autorotateToPortrait = false; | |
Screen.autorotateToPortraitUpsideDown = false; | |
} | |
private void SetAutoRotationPermission() | |
{ | |
Screen.autorotateToLandscapeLeft = _permissions.HasFlag(DeviceScreenOrientationPermissions.LandscapeLeft); | |
Screen.autorotateToLandscapeRight = _permissions.HasFlag(DeviceScreenOrientationPermissions.LandscapeRight); | |
Screen.autorotateToPortrait = _permissions.HasFlag(DeviceScreenOrientationPermissions.Portrait); | |
Screen.autorotateToPortraitUpsideDown = _permissions.HasFlag(DeviceScreenOrientationPermissions.PortraitUpsideDown); | |
Screen.orientation = ScreenOrientation.AutoRotation; | |
} | |
/// <summary> | |
/// Listens for changes in the device orientation. | |
/// Calls the <see cref="OrientationChanged"/> event when the orientation changes. | |
/// </summary> | |
private void EvaluateDeviceOrientation() | |
{ | |
// No changes | |
if (_currentScreenOrientation == Screen.orientation) return; | |
_currentScreenOrientation = Screen.orientation; | |
switch (Screen.orientation) | |
{ | |
case ScreenOrientation.Portrait: | |
case ScreenOrientation.PortraitUpsideDown: | |
OrientationChanged(new OrientationChangeContext | |
{ | |
IsPortrait = true | |
}); | |
Debug.Log("[DeviceOrientation] Screen orientation changed to portrait"); | |
break; | |
case ScreenOrientation.LandscapeLeft: | |
case ScreenOrientation.LandscapeRight: | |
OrientationChanged(new OrientationChangeContext | |
{ | |
IsLandscape = true | |
}); | |
Debug.Log("[DeviceOrientation] Screen orientation changed to landscape"); | |
break; | |
} | |
} | |
[Serializable] | |
[Flags] | |
public enum DeviceScreenOrientationPermissions | |
{ | |
Portrait = 1, | |
PortraitUpsideDown = 2, | |
LandscapeLeft = 4, | |
LandscapeRight = 8 | |
} | |
public record OrientationChangeContext | |
{ | |
public bool IsLandscape; | |
public bool IsPortrait; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment