RenderStreaming.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Linq;
  3. using UnityEngine;
  4. using Object = UnityEngine.Object;
  5. #if UNITY_EDITOR
  6. using UnityEditor;
  7. #endif
  8. namespace Unity.RenderStreaming
  9. {
  10. #if UNITY_EDITOR
  11. [InitializeOnLoad]
  12. #endif
  13. public static class RenderStreaming
  14. {
  15. internal const string EditorBuildSettingsConfigKey = "com.unity.renderstreaming.settings";
  16. internal const string DefaultRenderStreamingSettingsPath =
  17. "Packages/com.unity.renderstreaming/Runtime/RenderStreamingSettings.asset";
  18. private static RenderStreamingSettings s_settings;
  19. private static GameObject s_automaticStreamingObject;
  20. private static bool m_running;
  21. internal static RenderStreamingSettings Settings
  22. {
  23. get => s_settings;
  24. set
  25. {
  26. if (value == null)
  27. throw new ArgumentNullException(nameof(value));
  28. if (s_settings == value)
  29. return;
  30. // In the editor, we keep track of the settings asset through EditorBuildSettings.
  31. #if UNITY_EDITOR
  32. if (!string.IsNullOrEmpty(AssetDatabase.GetAssetPath(value)))
  33. {
  34. EditorBuildSettings.AddConfigObject(EditorBuildSettingsConfigKey, value, true);
  35. }
  36. #endif
  37. if (m_running && s_settings.signalingSettings != value.signalingSettings)
  38. {
  39. Debug.LogWarning("Signaling settings doesn't change on already started signaling instance.");
  40. }
  41. s_settings = value;
  42. ApplySettings();
  43. }
  44. }
  45. public static bool AutomaticStreaming
  46. {
  47. get => s_settings.automaticStreaming;
  48. set
  49. {
  50. if (s_settings.automaticStreaming == value)
  51. {
  52. return;
  53. }
  54. s_settings.automaticStreaming = value;
  55. ApplySettings();
  56. }
  57. }
  58. public static T GetSignalingSettings<T>() where T : SignalingSettings
  59. {
  60. return s_settings.signalingSettings as T;
  61. }
  62. static RenderStreaming()
  63. {
  64. #if UNITY_EDITOR
  65. InitializeInEditor();
  66. #else
  67. m_running = true;
  68. #endif
  69. }
  70. #if UNITY_EDITOR
  71. private static void InitializeInEditor()
  72. {
  73. if (EditorBuildSettings.TryGetConfigObject(EditorBuildSettingsConfigKey, out RenderStreamingSettings settingsAsset))
  74. {
  75. s_settings = settingsAsset;
  76. }
  77. else
  78. {
  79. s_settings = AssetDatabase.LoadAssetAtPath<RenderStreamingSettings>(DefaultRenderStreamingSettingsPath);
  80. }
  81. EditorApplication.playModeStateChanged += change =>
  82. {
  83. m_running = change == PlayModeStateChange.EnteredPlayMode;
  84. };
  85. EditorApplication.projectChanged += () =>
  86. {
  87. if (!EditorBuildSettings.TryGetConfigObject(EditorBuildSettingsConfigKey, out RenderStreamingSettings _))
  88. {
  89. Settings = AssetDatabase.LoadAssetAtPath<RenderStreamingSettings>(DefaultRenderStreamingSettingsPath);
  90. }
  91. };
  92. }
  93. #endif
  94. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
  95. private static void LoadSettings()
  96. {
  97. if (s_settings == null)
  98. {
  99. s_settings = Resources.FindObjectsOfTypeAll<RenderStreamingSettings>().FirstOrDefault() ??
  100. ScriptableObject.CreateInstance<RenderStreamingSettings>();
  101. }
  102. }
  103. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
  104. private static void RunInitialize()
  105. {
  106. if (AutomaticStreaming)
  107. {
  108. CreateAutomaticStreaming();
  109. }
  110. }
  111. internal static void ApplySettings()
  112. {
  113. #if UNITY_EDITOR
  114. EditorUtility.SetDirty(s_settings);
  115. #endif
  116. if (!m_running)
  117. {
  118. return;
  119. }
  120. if (s_settings.automaticStreaming && s_automaticStreamingObject == null)
  121. {
  122. CreateAutomaticStreaming();
  123. }
  124. if (!s_settings.automaticStreaming)
  125. {
  126. CleanUpAutomaticStreaming();
  127. }
  128. }
  129. private static void CreateAutomaticStreaming()
  130. {
  131. if (s_automaticStreamingObject != null)
  132. {
  133. Object.DestroyImmediate(s_automaticStreamingObject);
  134. }
  135. s_automaticStreamingObject = new GameObject("AutomaticStreaming");
  136. s_automaticStreamingObject.AddComponent<AutomaticStreaming>();
  137. Object.DontDestroyOnLoad(s_automaticStreamingObject);
  138. }
  139. private static void CleanUpAutomaticStreaming()
  140. {
  141. Object.DestroyImmediate(s_automaticStreamingObject);
  142. s_automaticStreamingObject = null;
  143. }
  144. }
  145. }