FrameRateDrawer.cs 3.2 KB

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