Skip to content

Commit 0cfb5fa

Browse files
authored
fix: Add FrameRatePropertyDrawer (#743)
1 parent f8c0d89 commit 0cfb5fa

File tree

3 files changed

+102
-17
lines changed

3 files changed

+102
-17
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
using System.Reflection;
4+
5+
namespace Unity.RenderStreaming.Editor
6+
{
7+
[CustomPropertyDrawer(typeof(FrameRateAttribute))]
8+
class FrameRateDrawer : PropertyDrawer
9+
{
10+
readonly GUIContent[] frameRateText =
11+
{
12+
EditorGUIUtility.TrTextContent("Default"),
13+
EditorGUIUtility.TrTextContent("10"),
14+
EditorGUIUtility.TrTextContent("15"),
15+
EditorGUIUtility.TrTextContent("20"),
16+
EditorGUIUtility.TrTextContent("30"),
17+
EditorGUIUtility.TrTextContent("60"),
18+
};
19+
20+
readonly float?[] frameRateValues =
21+
{
22+
null,
23+
10,
24+
15,
25+
20,
26+
30,
27+
60
28+
};
29+
30+
readonly GUIContent s_FramerateLabel =
31+
EditorGUIUtility.TrTextContent("Framerate",
32+
"Video encoding framerate.");
33+
34+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
35+
{
36+
EditorGUI.BeginProperty(position, label, property);
37+
38+
float value = property.floatValue;
39+
var selectIndex = 1;
40+
while (selectIndex < frameRateValues.Length && !Mathf.Approximately(value, frameRateValues[selectIndex].Value))
41+
{
42+
++selectIndex;
43+
}
44+
45+
// default value
46+
if (selectIndex == frameRateValues.Length)
47+
selectIndex = 0;
48+
49+
var popupRect = position;
50+
popupRect.height = EditorGUIUtility.singleLineHeight;
51+
52+
selectIndex = EditorGUI.Popup(popupRect, s_FramerateLabel,
53+
selectIndex, frameRateText);
54+
55+
float newValue;
56+
var cutomValueRect = position;
57+
cutomValueRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
58+
cutomValueRect.height = 0;
59+
60+
if (0 < selectIndex && selectIndex < frameRateValues.Length)
61+
{
62+
newValue = frameRateValues[selectIndex].Value;
63+
}
64+
else
65+
{
66+
newValue = 0;
67+
}
68+
if (!Mathf.Approximately(value, newValue))
69+
{
70+
property.floatValue = newValue;
71+
72+
if(Application.isPlaying)
73+
{
74+
var objectReferenceValue = property.serializedObject.targetObject;
75+
var type = objectReferenceValue.GetType();
76+
var attribute = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
77+
var methodName = "SetFrameRate";
78+
var method = type.GetMethod(methodName, attribute);
79+
method.Invoke(objectReferenceValue, new object[] { newValue });
80+
}
81+
}
82+
EditorGUI.EndProperty();
83+
}
84+
85+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
86+
{
87+
if (property == null)
88+
throw new System.ArgumentNullException(nameof(property));
89+
90+
var height = 0f;
91+
92+
// Popup.
93+
height += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
94+
return height;
95+
}
96+
}
97+
}

com.unity.renderstreaming/Editor/PropertyDrawers/FrameRateDrawer.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.unity.renderstreaming/Runtime/Scripts/VideoStreamSender.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,14 @@
66

77
namespace Unity.RenderStreaming
88
{
9-
/// <summary>
10-
///
11-
/// </summary>
129
internal sealed class StreamingSizeAttribute : PropertyAttribute { }
1310

14-
/// <summary>
15-
///
16-
/// </summary>
17-
internal sealed class FramerateAttribute : PropertyAttribute { }
11+
internal sealed class FrameRateAttribute : PropertyAttribute { }
1812

19-
/// <summary>
20-
///
21-
/// </summary>
2213
internal sealed class BitrateAttribute : PropertyAttribute { }
2314

24-
/// <summary>
25-
///
26-
/// </summary>
2715
internal sealed class RenderTextureAntiAliasingAttribute : PropertyAttribute { }
2816

29-
/// <summary>
30-
///
31-
/// </summary>
3217
internal sealed class RenderTextureDepthBufferAttribute : PropertyAttribute { }
3318

3419
internal static class RTCRtpSenderExtension
@@ -96,7 +81,7 @@ public class VideoStreamSender : StreamSenderBase
9681
[SerializeField, StreamingSize]
9782
public Vector2Int streamingSize = new Vector2Int(1280, 720);
9883

99-
[SerializeField]
84+
[SerializeField, FrameRate]
10085
private float m_frameRate = s_defaultFrameRate;
10186

10287
[SerializeField]

0 commit comments

Comments
 (0)