NRSessionBehaviour.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using UnityEngine;
  12. /// <summary>
  13. /// Oprate AR system state and handles the session lifecycle for application layer. </summary>
  14. [HelpURL("https://developer.nreal.ai/develop/discover/introduction-nrsdk")]
  15. [ScriptOrder(NativeConstants.NRSESSIONBEHAVIOUR_ORDER)]
  16. public class NRSessionBehaviour : SingletonBehaviour<NRSessionBehaviour>
  17. {
  18. /// <summary> LogVevel of NRSDK should be while release. </summary>
  19. [Tooltip("LogLevel of NRSDK.")]
  20. [SerializeField]
  21. LogLevel LogLevel = LogLevel.Info;
  22. /// <summary> The SessionConfig of nrsession. </summary>
  23. [Tooltip("A scriptable object specifying the NRSDK session configuration.")]
  24. [SerializeField]
  25. public NRSessionConfig SessionConfig;
  26. /// <summary>
  27. /// Base Awake method that sets the Singleton's unique instance. Called by Unity when
  28. /// initializing a MonoBehaviour. Scripts that extend Singleton should be sure to call
  29. /// base.Awake() to ensure the static Instance reference is properly created. </summary>
  30. new void Awake()
  31. {
  32. #if NRDEBUG
  33. NRDebugger.logLevel = LogLevel.All;
  34. #elif !UNITY_EDITOR
  35. NRDebugger.logLevel = Debug.isDebugBuild ? LogLevel.Debug : LogLevel;
  36. #else
  37. NRDebugger.logLevel = LogLevel;
  38. #endif
  39. Debug.LogFormat("[SessionBehaviour] Awake: logLevel={0}", NRDebugger.logLevel);
  40. base.Awake();
  41. if (isDirty) return;
  42. NRDebugger.Info("[SessionBehaviour] Awake: CreateSession");
  43. NRSessionManager.Instance.CreateSession(this);
  44. }
  45. /// <summary> Starts this object. </summary>
  46. void Start()
  47. {
  48. if (isDirty) return;
  49. NRDebugger.Info("[SessionBehaviour] Start: StartSession");
  50. NRSessionManager.Instance.StartSession();
  51. }
  52. /// <summary> Executes the 'application pause' action. </summary>
  53. /// <param name="pause"> True to pause.</param>
  54. private void OnApplicationPause(bool pause)
  55. {
  56. if (isDirty) return;
  57. NRDebugger.Info("[SessionBehaviour] OnApplicationPause: {0}", pause);
  58. if (pause)
  59. {
  60. NRSessionManager.Instance.DisableSession();
  61. }
  62. else
  63. {
  64. NRSessionManager.Instance.ResumeSession();
  65. }
  66. }
  67. /// <summary> Executes the 'enable' action. </summary>
  68. void OnEnable()
  69. {
  70. if (isDirty) return;
  71. NRDebugger.Info("[SessionBehaviour] OnEnable: ResumeSession");
  72. NRSessionManager.Instance.ResumeSession();
  73. }
  74. /// <summary> Executes the 'disable' action. </summary>
  75. void OnDisable()
  76. {
  77. if (isDirty) return;
  78. NRDebugger.Info("[SessionBehaviour] OnDisable: DisableSession");
  79. NRSessionManager.Instance.DisableSession();
  80. }
  81. /// <summary>
  82. /// Base OnDestroy method that destroys the Singleton's unique instance. Called by Unity when
  83. /// destroying a MonoBehaviour. Scripts that extend Singleton should be sure to call
  84. /// base.OnDestroy() to ensure the underlying static Instance reference is properly cleaned up. </summary>
  85. new void OnDestroy()
  86. {
  87. if (isDirty) return;
  88. base.OnDestroy();
  89. NRDebugger.Info("[SessionBehaviour] OnDestroy: DestroySession");
  90. NRSessionManager.Instance.DestroySession();
  91. }
  92. }
  93. }