NREditorSceneManager.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.NREditor
  10. {
  11. using UnityEngine;
  12. using UnityEditor;
  13. using System;
  14. /// <summary> Manager for nr editor scenes. </summary>
  15. public class NREditorSceneManager
  16. {
  17. /// <summary> True if scene initialized. </summary>
  18. private bool m_SceneInitialized;
  19. /// <summary> True to unload unused assets. </summary>
  20. private bool m_UnloadUnusedAssets;
  21. /// <summary> True to apply appearance. </summary>
  22. private bool m_ApplyAppearance;
  23. /// <summary> True to apply properties. </summary>
  24. private bool m_ApplyProperties;
  25. /// <summary> The instance. </summary>
  26. private static NREditorSceneManager m_Instance;
  27. /// <summary> The update callback. </summary>
  28. private EditorApplication.CallbackFunction m_UpdateCallback;
  29. /// <summary> Gets the instance. </summary>
  30. /// <value> The instance. </value>
  31. public static NREditorSceneManager Instance
  32. {
  33. get
  34. {
  35. if (NREditorSceneManager.m_Instance == null)
  36. {
  37. Type typeFromHandle = typeof(NREditorSceneManager);
  38. lock (typeFromHandle)
  39. {
  40. if (NREditorSceneManager.m_Instance == null)
  41. {
  42. NREditorSceneManager.m_Instance = new NREditorSceneManager();
  43. }
  44. }
  45. }
  46. return NREditorSceneManager.m_Instance;
  47. }
  48. }
  49. /// <summary> Gets a value indicating whether the scene initialized. </summary>
  50. /// <value> True if scene initialized, false if not. </value>
  51. public bool SceneInitialized { get { return m_SceneInitialized; } }
  52. /// <summary>
  53. /// Constructor that prevents a default instance of this class from being created. </summary>
  54. private NREditorSceneManager()
  55. {
  56. //return;
  57. m_UpdateCallback = new EditorApplication.CallbackFunction(EditorUpdate);
  58. if (EditorApplication.update == null || !EditorApplication.update.Equals(m_UpdateCallback))
  59. {
  60. EditorApplication.update = (EditorApplication.CallbackFunction)System.Delegate.Combine(EditorApplication.update, m_UpdateCallback);
  61. }
  62. m_SceneInitialized = false;
  63. }
  64. /// <summary> Initializes the scene. </summary>
  65. public void InitScene()
  66. {
  67. m_SceneInitialized = true;
  68. }
  69. /// <summary> Editor update. </summary>
  70. public void EditorUpdate()
  71. {
  72. NRTrackableBehaviour[] trackables = GameObject.FindObjectsOfType<NRTrackableBehaviour>();
  73. if (m_ApplyAppearance)
  74. {
  75. UpdateTrackableAppearance(trackables);
  76. m_ApplyAppearance = false;
  77. }
  78. if (m_ApplyProperties)
  79. {
  80. UpdateTrackableProperties(trackables);
  81. m_ApplyProperties = false;
  82. }
  83. if (m_UnloadUnusedAssets)
  84. {
  85. Resources.UnloadUnusedAssets();
  86. m_UnloadUnusedAssets = false;
  87. }
  88. }
  89. /// <summary> Updates the trackable appearance described by trackables. </summary>
  90. /// <param name="trackables"> The trackables.</param>
  91. private void UpdateTrackableAppearance(NRTrackableBehaviour[] trackables)
  92. {
  93. if (!Application.isPlaying)
  94. {
  95. for (int i = 0; i < trackables.Length; i++)
  96. {
  97. NRTrackableBehaviour trackableBehaviour = trackables[i];
  98. NRTrackableAccessor trackableAccessor = NRAccessorFactory.Create(trackableBehaviour);
  99. if (trackableAccessor != null)
  100. {
  101. trackableAccessor.ApplyDataAppearance();
  102. }
  103. }
  104. }
  105. }
  106. /// <summary> Updates the trackable properties described by trackables. </summary>
  107. /// <param name="trackables"> The trackables.</param>
  108. private void UpdateTrackableProperties(NRTrackableBehaviour[] trackables)
  109. {
  110. for (int i = 0; i < trackables.Length; i++)
  111. {
  112. NRTrackableBehaviour trackableBehaviour = trackables[i];
  113. NRTrackableAccessor trackableAccessor = NRAccessorFactory.Create(trackableBehaviour);
  114. if (trackableAccessor != null)
  115. {
  116. trackableAccessor.ApplyDataProperties();
  117. }
  118. }
  119. }
  120. /// <summary> Unload unused assets. </summary>
  121. public void UnloadUnusedAssets()
  122. {
  123. m_UnloadUnusedAssets = true;
  124. }
  125. }
  126. }