Skip to content

Commit

Permalink
feat: Define SignalingSettings asset to make portable signaling confi…
Browse files Browse the repository at this point in the history
…gurations (Unity-Technologies#854)

* Fix SignalingManager inspector

* fix bugs

* fix

* fix

* fix

* fix

* fix
  • Loading branch information
karasusan authored Feb 3, 2023
1 parent 973f8ac commit 48ab2b6
Show file tree
Hide file tree
Showing 20 changed files with 789 additions and 89 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
Expand All @@ -12,17 +11,22 @@ namespace Unity.RenderStreaming.Editor
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);
box.Add(CreatePopUpSignalingType(property, "Signaling Type"));
popupFieldSignalingType = CreatePopUpSignalingType(property, "Signaling Type");
popupFieldSignalingType.RegisterValueChangedCallback(e => OnPopupFieldValueChange(e, property));
box.Add(popupFieldSignalingType);
box.Add(editorGUI);
return root;
}
Expand All @@ -34,17 +38,34 @@ ISignalingSettingEditor CreateEditor(SerializedProperty property)
return Activator.CreateInstance(type) as ISignalingSettingEditor;
}

VisualElement CreatePopUpSignalingType(SerializedProperty property, string label)
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();
var element = new PopupField<string>(label: label, choices: choices, defaultValue: defaultValue);
element.RegisterValueChangedCallback(e => OnChangedValue(e, property));
return element;
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 OnChangedValue(ChangeEvent<string> e, SerializedProperty property)
void OnPopupFieldValueChange(ChangeEvent<string> e, SerializedProperty property)
{
if(!(fieldInfo.GetValue(property.serializedObject.targetObject) is SignalingSettings settings))
return;
Expand All @@ -54,6 +75,11 @@ void OnChangedValue(ChangeEvent<string> e, SerializedProperty property)
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))
{
Expand All @@ -65,22 +91,18 @@ void OnChangedValue(ChangeEvent<string> e, SerializedProperty property)
property.serializedObject.ApplyModifiedProperties();

var inspectorType = CustomSignalingSettingsEditor.FindInspectorTypeByInspectedType(inspectedType);
var editor = Activator.CreateInstance(inspectorType) as ISignalingSettingEditor;
editor = Activator.CreateInstance(inspectorType) as ISignalingSettingEditor;
var newValue = editor.CreateInspectorGUI(property);
ReplaceElement(editorGUI, newValue);

// Unbind old element to serializedObject.
editorGUI.Unbind();

ReplaceVisualElement(editorGUI, newValue);
editorGUI = newValue;

// bind new serializedObject.
// bind new element to serializedObject.
editorGUI.Bind(property.serializedObject);
}

static void ReplaceElement(VisualElement oldValue, VisualElement newValue)
{
var root = oldValue.parent;
var index = root.IndexOf(oldValue);
root.Remove(oldValue);
root.Insert(index, newValue);
}
}

/// <summary>
Expand All @@ -102,11 +124,6 @@ public VisualElement CreateInspectorGUI(SerializedProperty property)
root.Add(new PropertyField(property.FindPropertyRelative("m_iceServers"), "ICE Servers"));
return root;
}

public void SetSignalingSettings(SignalingSettings settings)
{
throw new System.NotImplementedException();
}
}

[CustomSignalingSettingsEditor(typeof(WebSocketSignalingSettings), "WebSocket")]
Expand All @@ -119,11 +136,6 @@ public VisualElement CreateInspectorGUI(SerializedProperty property)
root.Add(new PropertyField(property.FindPropertyRelative("m_iceServers"), "ICE Servers"));
return root;
}

public void SetSignalingSettings(SignalingSettings settings)
{
throw new System.NotImplementedException();
}
}

[CustomSignalingSettingsEditor(typeof(FurioosSignalingSettings), "Furioos")]
Expand All @@ -136,10 +148,5 @@ public VisualElement CreateInspectorGUI(SerializedProperty property)
root.Add(new PropertyField(property.FindPropertyRelative("m_iceServers"), "ICE Servers"));
return root;
}

public void SetSignalingSettings(SignalingSettings settings)
{
throw new System.NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public override void OnActivate(string searchContext, VisualElement rootElement)
var selectorContainer = rootVisualElement.Q<VisualElement>("renderStreamingSettingsSelector");

var defaultIndex = ArrayHelpers.IndexOf(availableRenderStreamingSettingsAssets, AssetDatabase.GetAssetPath(settings));
var choices = availableRenderStreamingSettingsAssets.Select(x => x.Replace("/", " \u2215 ")).ToList();
var choices = availableRenderStreamingSettingsAssets.ToList();
var selectPopup = new PopupField<string>(label: label, choices: choices, defaultIndex: defaultIndex)
{
name = "renderStreamingSettingsSelectPopup"
Expand Down Expand Up @@ -92,6 +92,11 @@ public override void OnActivate(string searchContext, VisualElement rootElement)
selectorContainer.Add(createAssetHelpBox);

ShowRenderStreamingSettingsProperty();

// Disable UI when running in Playmode
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
if(EditorApplication.isPlaying)
rootVisualElement.SetEnabled(false);
}

public override void OnInspectorUpdate()
Expand Down Expand Up @@ -148,6 +153,19 @@ private static void CreateNewSettingsAsset(string relativePath)
RenderStreaming.Settings = settings;
}

private void OnPlayModeStateChanged(PlayModeStateChange e)
{
switch (e)
{
case PlayModeStateChange.EnteredPlayMode:
rootVisualElement.SetEnabled(false);
break;
case PlayModeStateChange.ExitingPlayMode:
rootVisualElement.SetEnabled(true);
break;
}
}

private void ShowRenderStreamingSettingsProperty()
{
var settingsPropertyContainer = rootVisualElement.Q("settingsPropertyContainer");
Expand Down
Loading

0 comments on commit 48ab2b6

Please sign in to comment.