BitrateDrawer.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System.Reflection;
  4. namespace Unity.RenderStreaming.Editor
  5. {
  6. [CustomPropertyDrawer(typeof(BitrateAttribute))]
  7. class BitrateDrawer : PropertyDrawer
  8. {
  9. SerializedProperty propertyMinimum;
  10. SerializedProperty propertyMaximum;
  11. bool cache = false;
  12. bool changed = false;
  13. int minLimit;
  14. int maxLimit;
  15. static readonly GUIContent s_bitrateLabel =
  16. EditorGUIUtility.TrTextContent("Bitrate (kbits/sec)", "A range of bitrate of streaming.");
  17. static readonly GUIContent s_minBitrateLabel =
  18. EditorGUIUtility.TrTextContent("Min");
  19. static readonly GUIContent s_maxBitrateLabel =
  20. EditorGUIUtility.TrTextContent("Max");
  21. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  22. {
  23. if (!cache)
  24. {
  25. propertyMinimum = property.FindPropertyInChildren("min");
  26. propertyMaximum = property.FindPropertyInChildren("max");
  27. var attr = attribute as BitrateAttribute;
  28. minLimit = attr.minValue;
  29. maxLimit = attr.maxValue;
  30. property.Reset();
  31. cache = true;
  32. }
  33. var rect = position;
  34. rect.height = EditorGUIUtility.singleLineHeight;
  35. EditorGUI.BeginProperty(rect, label, property);
  36. float minValue = propertyMinimum.intValue;
  37. float maxValue = propertyMaximum.intValue;
  38. // slider
  39. EditorGUI.BeginChangeCheck();
  40. rect = EditorGUI.PrefixLabel(rect, s_bitrateLabel);
  41. EditorGUI.MinMaxSlider(rect, ref minValue, ref maxValue, minLimit, maxLimit);
  42. int min = (int)minValue;
  43. int max = (int)maxValue;
  44. if (EditorGUI.EndChangeCheck())
  45. {
  46. propertyMinimum.intValue = min;
  47. propertyMaximum.intValue = max;
  48. changed = true;
  49. }
  50. EditorGUI.EndProperty();
  51. // min value
  52. EditorGUI.BeginChangeCheck();
  53. rect.y += EditorGUIUtility.singleLineHeight;
  54. min = EditorGUI.IntField(rect, s_minBitrateLabel, min);
  55. if (EditorGUI.EndChangeCheck())
  56. {
  57. min = Mathf.Max(min, minLimit);
  58. min = Mathf.Min(min, max);
  59. propertyMinimum.intValue = min;
  60. changed = true;
  61. }
  62. // max value
  63. EditorGUI.BeginChangeCheck();
  64. rect.y += EditorGUIUtility.singleLineHeight;
  65. max = EditorGUI.IntField(rect, s_maxBitrateLabel, max);
  66. if (EditorGUI.EndChangeCheck())
  67. {
  68. max = Mathf.Min(max, maxLimit);
  69. max = Mathf.Max(min, max);
  70. propertyMaximum.intValue = max;
  71. changed = true;
  72. }
  73. if (changed)
  74. {
  75. if (Application.isPlaying)
  76. {
  77. var objectReferenceValue = property.serializedObject.targetObject;
  78. var type = objectReferenceValue.GetType();
  79. var attribute = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
  80. var methodName = "SetBitrate";
  81. var method = type.GetMethod(methodName, attribute);
  82. method.Invoke(objectReferenceValue, new object[] { (uint)min, (uint)max });
  83. }
  84. changed = false;
  85. }
  86. }
  87. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  88. {
  89. //var height = 0f;
  90. return EditorGUIUtility.singleLineHeight * 3;
  91. //return height;
  92. }
  93. }
  94. }