SampleSceneManager.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // /******************************************************************************
  2. // * File: SampleSceneManager.cs
  3. // * Copyright (c) 2023 Qualcomm Technologies, Inc. and/or its subsidiaries. All rights reserved.
  4. // *
  5. // *
  6. // ******************************************************************************/
  7. using UnityEngine;
  8. using UnityEngine.Events;
  9. using UnityEngine.SceneManagement;
  10. using UnityEngine.XR.Interaction.Toolkit.Inputs.Simulation;
  11. namespace QCHT.Samples.Menu
  12. {
  13. public class SampleSceneManager : MonoBehaviour
  14. {
  15. [SerializeField]
  16. private SampleSettings startSample;
  17. private SampleSettings _currentSampleToLoad;
  18. private SampleSettings _currentSample;
  19. private Scene _currentScene;
  20. public UnityEvent OnSampleLoaded = new UnityEvent();
  21. public void Start()
  22. {
  23. Screen.sleepTimeout = SleepTimeout.NeverSleep;
  24. if (startSample)
  25. LoadSample(startSample);
  26. }
  27. public void OnEnable()
  28. {
  29. SceneManager.sceneLoaded += OnSceneLoaded;
  30. }
  31. public void OnDisable()
  32. {
  33. SceneManager.sceneLoaded -= OnSceneLoaded;
  34. }
  35. public void Quit()
  36. {
  37. #if UNITY_EDITOR
  38. UnityEditor.EditorApplication.isPlaying = false;
  39. #else
  40. Application.Quit();
  41. #endif
  42. }
  43. public void SwitchToScene(string name)
  44. {
  45. SceneManager.LoadScene(name);
  46. }
  47. #region Sample Loading
  48. /// <summary>
  49. /// Loads a sample scene and unload the current sample scene if exists.
  50. /// </summary>
  51. public void LoadSample(SampleSettings sample)
  52. {
  53. if (_currentSampleToLoad || sample.SceneName.Equals(_currentScene.name))
  54. return;
  55. // Unload current scene if exists
  56. if (_currentScene.IsValid() && _currentScene.isLoaded)
  57. {
  58. SceneManager.UnloadSceneAsync(_currentScene);
  59. _currentSample = null;
  60. }
  61. // Load the new sample scene by name
  62. _currentSampleToLoad = sample;
  63. SceneManager.LoadScene(sample.SceneName, LoadSceneMode.Additive);
  64. }
  65. private void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode)
  66. {
  67. #if UNITY_EDITOR
  68. if (sceneMode == LoadSceneMode.Single)
  69. RefreshXRDeviceSimulator();
  70. #endif
  71. if (!_currentSampleToLoad || scene.name != _currentSampleToLoad.SceneName)
  72. return;
  73. _currentScene = scene;
  74. _currentSample = _currentSampleToLoad;
  75. _currentSampleToLoad = null;
  76. OnSampleLoaded?.Invoke();
  77. }
  78. #if UNITY_EDITOR
  79. private static void RefreshXRDeviceSimulator()
  80. {
  81. var xrDeviceSimulator = FindObjectOfType<XRDeviceSimulator>();
  82. if (xrDeviceSimulator == null) return;
  83. xrDeviceSimulator.enabled = false;
  84. xrDeviceSimulator.enabled = true;
  85. }
  86. #endif
  87. #endregion
  88. }
  89. }