ExamplesHub.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. using UnityEngine;
  10. using UnityEngine.SceneManagement;
  11. namespace NRKernal.NRExamples
  12. {
  13. /// <summary> The examples hub. </summary>
  14. public class ExamplesHub : SingletonBehaviour<ExamplesHub>
  15. {
  16. /// <summary> The scenes. </summary>
  17. private string[] m_Scenes = new string[] {
  18. "HelloMR",
  19. "ImageTracking",
  20. "Input-ControllerInfo",
  21. "Input-Interaction",
  22. "RGBCamera",
  23. "RGBCamera-Capture",
  24. "RGBCamera-Record"
  25. };
  26. /// <summary> The current index. </summary>
  27. private int m_CurrentIndex = 0;
  28. /// <summary> Gets or sets the current index. </summary>
  29. /// <value> The current index. </value>
  30. public int CurrentIndex
  31. {
  32. get
  33. {
  34. return m_CurrentIndex;
  35. }
  36. private set
  37. {
  38. m_CurrentIndex = value;
  39. if (m_CurrentIndex < 0 || m_CurrentIndex >= m_Scenes.Length)
  40. {
  41. m_CurrentIndex = 0;
  42. }
  43. }
  44. }
  45. /// <summary> True if is lock, false if not. </summary>
  46. private bool m_IsLock = false;
  47. /// <summary> Updates this object. </summary>
  48. private void Update()
  49. {
  50. #if UNITY_EDITOR
  51. if (Input.GetKeyDown(KeyCode.RightArrow))
  52. {
  53. LoadNextScene();
  54. }
  55. if (Input.GetKeyDown(KeyCode.LeftArrow))
  56. {
  57. LoadLastScene();
  58. }
  59. #endif
  60. if (NRInput.GetTouch().x > 0.8f)
  61. {
  62. LoadNextScene();
  63. }
  64. if (NRInput.GetTouch().x < -0.8f)
  65. {
  66. LoadLastScene();
  67. }
  68. }
  69. /// <summary> Loads next scene. </summary>
  70. public void LoadNextScene()
  71. {
  72. if (m_IsLock)
  73. {
  74. return;
  75. }
  76. m_IsLock = true;
  77. CurrentIndex++;
  78. if (CanSceneLoaded(m_Scenes[CurrentIndex]))
  79. {
  80. SceneManager.LoadScene(m_Scenes[CurrentIndex]);
  81. }
  82. Invoke("Unlock", 1f);
  83. }
  84. /// <summary> Loads last scene. </summary>
  85. public void LoadLastScene()
  86. {
  87. if (m_IsLock)
  88. {
  89. return;
  90. }
  91. m_IsLock = true;
  92. CurrentIndex--;
  93. if (CanSceneLoaded(m_Scenes[CurrentIndex]))
  94. {
  95. SceneManager.LoadScene(m_Scenes[CurrentIndex]);
  96. }
  97. Invoke("Unlock", 1f);
  98. }
  99. /// <summary> Unlocks this object. </summary>
  100. private void Unlock()
  101. {
  102. m_IsLock = false;
  103. }
  104. /// <summary> Determine if we can scene loaded. </summary>
  105. /// <param name="name"> The name.</param>
  106. /// <returns> True if we can scene loaded, false if not. </returns>
  107. private bool CanSceneLoaded(string name)
  108. {
  109. return (SceneUtility.GetBuildIndexByScenePath(name) != -1) &&
  110. !SceneManager.GetActiveScene().name.Equals(name);
  111. }
  112. }
  113. }