ActivateFromOrientation.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*===============================================================================
  2. Copyright (C) 2022 Immersal - Part of Hexagon. All Rights Reserved.
  3. This file is part of the Immersal SDK.
  4. The Immersal SDK cannot be copied, distributed, or made available to
  5. third-parties for commercial purposes without written permission of Immersal Ltd.
  6. Contact sdk@immersal.com for licensing requests.
  7. ===============================================================================*/
  8. using UnityEngine;
  9. namespace Immersal.Samples.Util
  10. {
  11. public class ActivateFromOrientation : MonoBehaviour
  12. {
  13. [SerializeField]
  14. private CanvasGroup m_portraitGroup = null;
  15. [SerializeField]
  16. private CanvasGroup m_landscapeLeftGroup = null;
  17. [SerializeField]
  18. private CanvasGroup m_landscapeRightGroup = null;
  19. private DeviceOrientation m_previousOrientation;
  20. #if !UNITY_EDITOR
  21. private void Start()
  22. {
  23. UpdateOrientation();
  24. }
  25. private void Update()
  26. {
  27. UpdateOrientation();
  28. }
  29. #endif
  30. private void UpdateOrientation()
  31. {
  32. if (m_portraitGroup == null || m_landscapeLeftGroup == null || m_landscapeRightGroup == null)
  33. return;
  34. DeviceOrientation orientation = Input.deviceOrientation;
  35. if (orientation == DeviceOrientation.Unknown)
  36. return;
  37. if (orientation != m_previousOrientation)
  38. {
  39. m_previousOrientation = orientation;
  40. switch (orientation)
  41. {
  42. case DeviceOrientation.LandscapeLeft:
  43. m_landscapeLeftGroup.gameObject.SetActive(true);
  44. m_landscapeRightGroup.gameObject.SetActive(false);
  45. m_portraitGroup.gameObject.SetActive(false);
  46. break;
  47. case DeviceOrientation.LandscapeRight:
  48. m_landscapeLeftGroup.gameObject.SetActive(false);
  49. m_landscapeRightGroup.gameObject.SetActive(true);
  50. m_portraitGroup.gameObject.SetActive(false);
  51. break;
  52. default:
  53. m_landscapeLeftGroup.gameObject.SetActive(false);
  54. m_landscapeRightGroup.gameObject.SetActive(false);
  55. m_portraitGroup.gameObject.SetActive(true);
  56. break;
  57. }
  58. }
  59. }
  60. }
  61. }