|
| 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 | +} |
0 commit comments