SignalingSettingsDrawer.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEditor.UIElements;
  6. using UnityEngine.UIElements;
  7. namespace Unity.RenderStreaming.Editor
  8. {
  9. [CustomPropertyDrawer(typeof(SignalingSettingsAttribute))]
  10. class SignalingSettingsDrawer : PropertyDrawer
  11. {
  12. private VisualElement editorGUI;
  13. private PopupField<string> popupFieldSignalingType;
  14. private ISignalingSettingEditor editor;
  15. private Dictionary<Type, SignalingSettings> table = new Dictionary<Type, SignalingSettings>();
  16. public override VisualElement CreatePropertyGUI(SerializedProperty property)
  17. {
  18. editor = CreateEditor(property);
  19. var root = new VisualElement();
  20. root.RegisterCallback<SerializedPropertyChangeEvent, SerializedProperty>(OnSignalingSettingsObjectChange, property);
  21. var box = new Box();
  22. root.Add(box);
  23. editorGUI = editor.CreateInspectorGUI(property);
  24. popupFieldSignalingType = CreatePopUpSignalingType(property, "Signaling Type");
  25. popupFieldSignalingType.RegisterValueChangedCallback(e => OnPopupFieldValueChange(e, property));
  26. box.Add(popupFieldSignalingType);
  27. box.Add(editorGUI);
  28. return root;
  29. }
  30. ISignalingSettingEditor CreateEditor(SerializedProperty property)
  31. {
  32. var settings = fieldInfo.GetValue(property.serializedObject.targetObject) as SignalingSettings;
  33. var type = CustomSignalingSettingsEditor.FindInspectorTypeByInspectedType(settings.GetType());
  34. return Activator.CreateInstance(type) as ISignalingSettingEditor;
  35. }
  36. PopupField<string> CreatePopUpSignalingType(SerializedProperty property, string label)
  37. {
  38. var settings = fieldInfo.GetValue(property.serializedObject.targetObject) as SignalingSettings;
  39. var defaultValue = CustomSignalingSettingsEditor.FindLabelByInspectedType(settings.GetType());
  40. var choices = CustomSignalingSettingsEditor.Labels().ToList();
  41. return new PopupField<string>(label: label, choices: choices, defaultValue: defaultValue);
  42. }
  43. static void ReplaceVisualElement(VisualElement oldValue, VisualElement newValue)
  44. {
  45. var root = oldValue.parent;
  46. var index = root.IndexOf(oldValue);
  47. root.Remove(oldValue);
  48. root.Insert(index, newValue);
  49. }
  50. void OnSignalingSettingsObjectChange(SerializedPropertyChangeEvent e, SerializedProperty property)
  51. {
  52. var settings = fieldInfo.GetValue(property.serializedObject.targetObject) as SignalingSettings;
  53. var label = CustomSignalingSettingsEditor.FindLabelByInspectedType(settings.GetType());
  54. if (popupFieldSignalingType.value == label)
  55. return;
  56. popupFieldSignalingType.value = label;
  57. RecreateEditorGUI(label, property);
  58. }
  59. void OnPopupFieldValueChange(ChangeEvent<string> e, SerializedProperty property)
  60. {
  61. if(!(fieldInfo.GetValue(property.serializedObject.targetObject) is SignalingSettings settings))
  62. return;
  63. // cache current settings.
  64. var type = settings.GetType();
  65. table[type] = settings;
  66. var label = e.newValue;
  67. RecreateEditorGUI(label, property);
  68. }
  69. void RecreateEditorGUI(string label, SerializedProperty property)
  70. {
  71. var inspectedType = CustomSignalingSettingsEditor.FindInspectedTypeByLabel(label);
  72. if (!table.ContainsKey(inspectedType))
  73. {
  74. var newSettings = Activator.CreateInstance(inspectedType) as SignalingSettings;
  75. table.Add(inspectedType, newSettings);
  76. }
  77. property.managedReferenceValue = table[inspectedType];
  78. property.serializedObject.ApplyModifiedProperties();
  79. var inspectorType = CustomSignalingSettingsEditor.FindInspectorTypeByInspectedType(inspectedType);
  80. editor = Activator.CreateInstance(inspectorType) as ISignalingSettingEditor;
  81. var newValue = editor.CreateInspectorGUI(property);
  82. // Unbind old element to serializedObject.
  83. editorGUI.Unbind();
  84. ReplaceVisualElement(editorGUI, newValue);
  85. editorGUI = newValue;
  86. // bind new element to serializedObject.
  87. editorGUI.Bind(property.serializedObject);
  88. }
  89. }
  90. /// <summary>
  91. ///
  92. /// </summary>
  93. public interface ISignalingSettingEditor
  94. {
  95. VisualElement CreateInspectorGUI(SerializedProperty property);
  96. }
  97. [CustomSignalingSettingsEditor(typeof(HttpSignalingSettings), "HTTP Polling")]
  98. internal class HttpSignalingSettingsEditor : ISignalingSettingEditor
  99. {
  100. public VisualElement CreateInspectorGUI(SerializedProperty property)
  101. {
  102. VisualElement root = new VisualElement();
  103. root.Add(new PropertyField(property.FindPropertyRelative("m_url"), "URL"));
  104. root.Add(new PropertyField(property.FindPropertyRelative("m_interval"), "Polling Interval (msec)"));
  105. root.Add(new PropertyField(property.FindPropertyRelative("m_iceServers"), "ICE Servers"));
  106. return root;
  107. }
  108. }
  109. [CustomSignalingSettingsEditor(typeof(WebSocketSignalingSettings), "WebSocket")]
  110. internal class WebSocketSignalingSettingsEditor : ISignalingSettingEditor
  111. {
  112. public VisualElement CreateInspectorGUI(SerializedProperty property)
  113. {
  114. VisualElement root = new VisualElement();
  115. root.Add(new PropertyField(property.FindPropertyRelative("m_url"), "URL"));
  116. root.Add(new PropertyField(property.FindPropertyRelative("m_iceServers"), "ICE Servers"));
  117. return root;
  118. }
  119. }
  120. [CustomSignalingSettingsEditor(typeof(FurioosSignalingSettings), "Furioos")]
  121. internal class FurioosSignalingSettingsEditor : ISignalingSettingEditor
  122. {
  123. public VisualElement CreateInspectorGUI(SerializedProperty property)
  124. {
  125. VisualElement root = new VisualElement();
  126. root.Add(new PropertyField(property.FindPropertyRelative("m_url"), "URL"));
  127. root.Add(new PropertyField(property.FindPropertyRelative("m_iceServers"), "ICE Servers"));
  128. return root;
  129. }
  130. }
  131. }