SignalingManagerEditor.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System;
  2. using System.Linq;
  3. using UnityEditor;
  4. using UnityEditor.UIElements;
  5. using UnityEngine;
  6. using UnityEngine.UIElements;
  7. namespace Unity.RenderStreaming.Editor
  8. {
  9. /// <summary>
  10. /// Render Streaming inspector.
  11. /// </summary>
  12. [CustomEditor(typeof(SignalingManager))]
  13. internal class SignalingManagerEditor : UnityEditor.Editor
  14. {
  15. const string DefaultSignalingSettingsSavePath =
  16. "Assets/SignalingSettings.asset";
  17. const string DefaultSignalingSettingsLoadPath =
  18. "Packages/com.unity.renderstreaming/Runtime/SignalingSettings.asset";
  19. VisualElement root;
  20. Button openProjectSettingsButton;
  21. PopupField<SignalingSettingsObject> signalingSettingsPopupField;
  22. PropertyField signalingSettingsField;
  23. public override VisualElement CreateInspectorGUI()
  24. {
  25. root = new VisualElement();
  26. bool useDefault = serializedObject.FindProperty("m_useDefault").boolValue;
  27. var useDefaultField = new PropertyField(serializedObject.FindProperty("m_useDefault"), "Use Default Settings in Project Settings");
  28. useDefaultField.RegisterValueChangeCallback(OnChangeUseDefault);
  29. openProjectSettingsButton = new Button { text = "Open Project Setings" };
  30. openProjectSettingsButton.clicked += OnClickedOpenProjectSettingsButton;
  31. signalingSettingsPopupField = CreatePopUpSignalingType(serializedObject.FindProperty("signalingSettingsObject"), "Signaling Settings Asset");
  32. signalingSettingsPopupField.RegisterValueChangedCallback(OnValueChangeSignalingSettingsObject);
  33. signalingSettingsField = new PropertyField(serializedObject.FindProperty("signalingSettings"), "Signaling Settings");
  34. signalingSettingsField.RegisterValueChangeCallback(OnValueChangeSignalingSettings);
  35. root.Add(useDefaultField);
  36. root.Add(openProjectSettingsButton);
  37. root.Add(signalingSettingsPopupField);
  38. root.Add(signalingSettingsField);
  39. if (useDefault)
  40. {
  41. signalingSettingsPopupField.style.display = DisplayStyle.None;
  42. signalingSettingsField.style.display = DisplayStyle.None;
  43. }
  44. else
  45. {
  46. openProjectSettingsButton.style.display = DisplayStyle.None;
  47. }
  48. root.Add(new ReorderableListField(serializedObject.FindProperty("handlers"), "Signaling Handler List"));
  49. root.Add(new PropertyField(serializedObject.FindProperty("runOnAwake"), "Run On Awake"));
  50. root.Add(new PropertyField(serializedObject.FindProperty("evaluateCommandlineArguments"), "Evaluate Commandline Arguments"));
  51. EditorApplication.projectChanged += OnProjectChanged;
  52. // Disable UI when running in Playmode
  53. EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
  54. if (EditorApplication.isPlaying)
  55. root.SetEnabled(false);
  56. return root;
  57. }
  58. PopupField<SignalingSettingsObject> CreatePopUpSignalingType(SerializedProperty property, string label)
  59. {
  60. var asset = property.objectReferenceValue as SignalingSettingsObject;
  61. var paths = GetAvailableSignalingSettingsPath();
  62. var field = new PopupField<SignalingSettingsObject>(label: label);
  63. field.formatSelectedValueCallback = v => AssetDatabase.GetAssetPath(v);
  64. field.formatListItemCallback = v => AssetDatabase.GetAssetPath(v);
  65. if (paths.Length == 0)
  66. return field;
  67. var availableObjects = paths.Select(path => AssetDatabase.LoadAssetAtPath<SignalingSettingsObject>(path)).ToArray();
  68. var defaultIndex = ArrayHelpers.IndexOf(availableObjects, asset);
  69. field.choices = availableObjects.ToList();
  70. field.index = defaultIndex < 0 ? 0 : defaultIndex;
  71. return field;
  72. }
  73. static string[] GetAvailableSignalingSettingsPath()
  74. {
  75. var guids = AssetDatabase.FindAssets("t:SignalingSettingsObject");
  76. return guids.Select(AssetDatabase.GUIDToAssetPath).Where(_ => _.StartsWith("Assets")).ToArray();
  77. }
  78. static bool IsValidSignalingSettingsObject(SignalingSettingsObject asset)
  79. {
  80. if (asset == null)
  81. return false;
  82. if (AssetDatabase.GetAssetPath(asset).IndexOf("Assets", StringComparison.Ordinal) != 0)
  83. return false;
  84. return true;
  85. }
  86. void CreateDefaultSignalingSettings()
  87. {
  88. // Create Default SignalingSettings in Assets folder when the useDefault flag is turned off first time.
  89. SignalingSettingsObject asset = AssetDatabase.LoadAssetAtPath<SignalingSettingsObject>(DefaultSignalingSettingsSavePath);
  90. if (asset == null)
  91. {
  92. if (!AssetDatabase.CopyAsset(DefaultSignalingSettingsLoadPath, DefaultSignalingSettingsSavePath))
  93. {
  94. Debug.LogError("CopyAssets is failed.");
  95. return;
  96. }
  97. asset = AssetDatabase.LoadAssetAtPath<SignalingSettingsObject>(DefaultSignalingSettingsSavePath);
  98. }
  99. var handler = serializedObject.targetObject as SignalingManager;
  100. handler.signalingSettingsObject = asset;
  101. handler.SetSignalingSettings(handler.signalingSettingsObject.settings);
  102. }
  103. private void OnPlayModeStateChanged(PlayModeStateChange e)
  104. {
  105. switch (e)
  106. {
  107. case PlayModeStateChange.EnteredPlayMode:
  108. root.SetEnabled(false);
  109. break;
  110. case PlayModeStateChange.ExitingPlayMode:
  111. root.SetEnabled(true);
  112. break;
  113. }
  114. }
  115. private void OnProjectChanged()
  116. {
  117. var paths = GetAvailableSignalingSettingsPath();
  118. // Force to use default settings if there are no available settings in project folder.
  119. if (paths.Length == 0)
  120. {
  121. serializedObject.FindProperty("m_useDefault").boolValue = true;
  122. serializedObject.ApplyModifiedProperties();
  123. return;
  124. }
  125. var asset = serializedObject.FindProperty("signalingSettingsObject").objectReferenceValue;
  126. var availableObjects = paths.Select(path => AssetDatabase.LoadAssetAtPath<SignalingSettingsObject>(path)).ToArray();
  127. var defaultIndex = ArrayHelpers.IndexOf(availableObjects, asset);
  128. if (defaultIndex < 0)
  129. {
  130. defaultIndex = 0;
  131. using var e = ChangeEvent<SignalingSettingsObject>.GetPooled(null, availableObjects[defaultIndex]);
  132. e.target = signalingSettingsPopupField;
  133. root.SendEvent(e);
  134. }
  135. signalingSettingsPopupField.choices = availableObjects.ToList();
  136. signalingSettingsPopupField.index = defaultIndex;
  137. }
  138. private void OnClickedOpenProjectSettingsButton()
  139. {
  140. SettingsService.OpenProjectSettings("Project/Render Streaming");
  141. }
  142. private void OnChangeUseDefault(SerializedPropertyChangeEvent e)
  143. {
  144. bool useDefault = e.changedProperty.boolValue;
  145. if (useDefault)
  146. {
  147. signalingSettingsPopupField.style.display = DisplayStyle.None;
  148. signalingSettingsField.style.display = DisplayStyle.None;
  149. openProjectSettingsButton.style.display = DisplayStyle.Flex;
  150. }
  151. else
  152. {
  153. signalingSettingsPopupField.style.display = DisplayStyle.Flex;
  154. signalingSettingsField.style.display = DisplayStyle.Flex;
  155. openProjectSettingsButton.style.display = DisplayStyle.None;
  156. var property = serializedObject.FindProperty("signalingSettingsObject");
  157. if (!IsValidSignalingSettingsObject(property.objectReferenceValue as SignalingSettingsObject))
  158. {
  159. CreateDefaultSignalingSettings();
  160. }
  161. }
  162. }
  163. private void OnValueChangeSignalingSettingsObject(ChangeEvent<SignalingSettingsObject> e)
  164. {
  165. var asset = e.newValue;
  166. if (asset == null)
  167. {
  168. Debug.LogError("Setting None is not allowed for this parameter. Reverted.");
  169. return;
  170. }
  171. if (AssetDatabase.GetAssetPath(asset).IndexOf("Assets", StringComparison.Ordinal) != 0)
  172. {
  173. Debug.LogError("Setting an asset not placed under Assets folder is not allowed for this parameter. Reverted.");
  174. return;
  175. }
  176. var property = serializedObject.FindProperty("signalingSettingsObject");
  177. property.objectReferenceValue = asset;
  178. serializedObject.ApplyModifiedProperties();
  179. var handler = serializedObject.targetObject as SignalingManager;
  180. handler.SetSignalingSettings(asset.settings);
  181. // Send event to repaint SignalingSettingsDrawer.
  182. using SerializedPropertyChangeEvent changeEvent = SerializedPropertyChangeEvent.GetPooled();
  183. changeEvent.changedProperty = property;
  184. changeEvent.target = signalingSettingsField.Children().First();
  185. root.SendEvent(changeEvent);
  186. }
  187. private void OnValueChangeSignalingSettings(SerializedPropertyChangeEvent e)
  188. {
  189. // Update SignalingSettings in ScriptableObject.
  190. var handler = serializedObject.targetObject as SignalingManager;
  191. if (handler.signalingSettingsObject != null)
  192. handler.signalingSettingsObject.settings = handler.GetSignalingSettings();
  193. }
  194. }
  195. }