123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEditor;
- using UnityEditor.UIElements;
- using UnityEngine.UIElements;
- namespace Unity.RenderStreaming.Editor
- {
- [CustomPropertyDrawer(typeof(SignalingSettingsAttribute))]
- class SignalingSettingsDrawer : PropertyDrawer
- {
- private VisualElement editorGUI;
- private PopupField<string> popupFieldSignalingType;
- private ISignalingSettingEditor editor;
- private Dictionary<Type, SignalingSettings> table = new Dictionary<Type, SignalingSettings>();
- public override VisualElement CreatePropertyGUI(SerializedProperty property)
- {
- editor = CreateEditor(property);
- var root = new VisualElement();
- root.RegisterCallback<SerializedPropertyChangeEvent, SerializedProperty>(OnSignalingSettingsObjectChange, property);
- var box = new Box();
- root.Add(box);
- editorGUI = editor.CreateInspectorGUI(property);
- popupFieldSignalingType = CreatePopUpSignalingType(property, "Signaling Type");
- popupFieldSignalingType.RegisterValueChangedCallback(e => OnPopupFieldValueChange(e, property));
- box.Add(popupFieldSignalingType);
- box.Add(editorGUI);
- return root;
- }
- ISignalingSettingEditor CreateEditor(SerializedProperty property)
- {
- var settings = fieldInfo.GetValue(property.serializedObject.targetObject) as SignalingSettings;
- var type = CustomSignalingSettingsEditor.FindInspectorTypeByInspectedType(settings.GetType());
- return Activator.CreateInstance(type) as ISignalingSettingEditor;
- }
- PopupField<string> CreatePopUpSignalingType(SerializedProperty property, string label)
- {
- var settings = fieldInfo.GetValue(property.serializedObject.targetObject) as SignalingSettings;
- var defaultValue = CustomSignalingSettingsEditor.FindLabelByInspectedType(settings.GetType());
- var choices = CustomSignalingSettingsEditor.Labels().ToList();
- return new PopupField<string>(label: label, choices: choices, defaultValue: defaultValue);
- }
- static void ReplaceVisualElement(VisualElement oldValue, VisualElement newValue)
- {
- var root = oldValue.parent;
- var index = root.IndexOf(oldValue);
- root.Remove(oldValue);
- root.Insert(index, newValue);
- }
- void OnSignalingSettingsObjectChange(SerializedPropertyChangeEvent e, SerializedProperty property)
- {
- var settings = fieldInfo.GetValue(property.serializedObject.targetObject) as SignalingSettings;
- var label = CustomSignalingSettingsEditor.FindLabelByInspectedType(settings.GetType());
- if (popupFieldSignalingType.value == label)
- return;
- popupFieldSignalingType.value = label;
- RecreateEditorGUI(label, property);
- }
- void OnPopupFieldValueChange(ChangeEvent<string> e, SerializedProperty property)
- {
- if(!(fieldInfo.GetValue(property.serializedObject.targetObject) is SignalingSettings settings))
- return;
- // cache current settings.
- var type = settings.GetType();
- table[type] = settings;
- var label = e.newValue;
- RecreateEditorGUI(label, property);
- }
- void RecreateEditorGUI(string label, SerializedProperty property)
- {
- var inspectedType = CustomSignalingSettingsEditor.FindInspectedTypeByLabel(label);
- if (!table.ContainsKey(inspectedType))
- {
- var newSettings = Activator.CreateInstance(inspectedType) as SignalingSettings;
- table.Add(inspectedType, newSettings);
- }
- property.managedReferenceValue = table[inspectedType];
- property.serializedObject.ApplyModifiedProperties();
- var inspectorType = CustomSignalingSettingsEditor.FindInspectorTypeByInspectedType(inspectedType);
- editor = Activator.CreateInstance(inspectorType) as ISignalingSettingEditor;
- var newValue = editor.CreateInspectorGUI(property);
- // Unbind old element to serializedObject.
- editorGUI.Unbind();
- ReplaceVisualElement(editorGUI, newValue);
- editorGUI = newValue;
- // bind new element to serializedObject.
- editorGUI.Bind(property.serializedObject);
- }
- }
- /// <summary>
- ///
- /// </summary>
- public interface ISignalingSettingEditor
- {
- VisualElement CreateInspectorGUI(SerializedProperty property);
- }
- [CustomSignalingSettingsEditor(typeof(HttpSignalingSettings), "HTTP Polling")]
- internal class HttpSignalingSettingsEditor : ISignalingSettingEditor
- {
- public VisualElement CreateInspectorGUI(SerializedProperty property)
- {
- VisualElement root = new VisualElement();
- root.Add(new PropertyField(property.FindPropertyRelative("m_url"), "URL"));
- root.Add(new PropertyField(property.FindPropertyRelative("m_interval"), "Polling Interval (msec)"));
- root.Add(new PropertyField(property.FindPropertyRelative("m_iceServers"), "ICE Servers"));
- return root;
- }
- }
- [CustomSignalingSettingsEditor(typeof(WebSocketSignalingSettings), "WebSocket")]
- internal class WebSocketSignalingSettingsEditor : ISignalingSettingEditor
- {
- public VisualElement CreateInspectorGUI(SerializedProperty property)
- {
- VisualElement root = new VisualElement();
- root.Add(new PropertyField(property.FindPropertyRelative("m_url"), "URL"));
- root.Add(new PropertyField(property.FindPropertyRelative("m_iceServers"), "ICE Servers"));
- return root;
- }
- }
- [CustomSignalingSettingsEditor(typeof(FurioosSignalingSettings), "Furioos")]
- internal class FurioosSignalingSettingsEditor : ISignalingSettingEditor
- {
- public VisualElement CreateInspectorGUI(SerializedProperty property)
- {
- VisualElement root = new VisualElement();
- root.Add(new PropertyField(property.FindPropertyRelative("m_url"), "URL"));
- root.Add(new PropertyField(property.FindPropertyRelative("m_iceServers"), "ICE Servers"));
- return root;
- }
- }
- }
|