ScaleResolutionDrawer.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System.Reflection;
  4. namespace Unity.RenderStreaming.Editor
  5. {
  6. [CustomPropertyDrawer(typeof(ScaleResolutionAttribute))]
  7. class ScaleResolutionDrawer : PropertyDrawer
  8. {
  9. readonly GUIContent[] scaleFactorText =
  10. {
  11. EditorGUIUtility.TrTextContent("No Scale"),
  12. EditorGUIUtility.TrTextContent("1 \u2215 2"),
  13. EditorGUIUtility.TrTextContent("1 \u2215 4"),
  14. EditorGUIUtility.TrTextContent("1 \u2215 8"),
  15. EditorGUIUtility.TrTextContent("1 \u2215 16"),
  16. };
  17. readonly float[] scaleFactorValues =
  18. {
  19. 1,
  20. 2,
  21. 4,
  22. 8,
  23. 16,
  24. };
  25. readonly GUIContent s_ScaleResolutionLabel =
  26. EditorGUIUtility.TrTextContent("Scale Resolution Down",
  27. "Downscaling video resolution to reduce bandwidth.");
  28. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  29. {
  30. EditorGUI.BeginProperty(position, label, property);
  31. float value = property.floatValue;
  32. var selectIndex = 1;
  33. while (selectIndex < scaleFactorValues.Length && !Mathf.Approximately(value, scaleFactorValues[selectIndex]))
  34. {
  35. ++selectIndex;
  36. }
  37. // default value
  38. if (selectIndex == scaleFactorValues.Length)
  39. selectIndex = 0;
  40. var popupRect = position;
  41. popupRect.height = EditorGUIUtility.singleLineHeight;
  42. selectIndex = EditorGUI.Popup(popupRect, s_ScaleResolutionLabel,
  43. selectIndex, scaleFactorText);
  44. float newValue;
  45. var cutomValueRect = position;
  46. cutomValueRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  47. cutomValueRect.height = 0;
  48. newValue = scaleFactorValues[selectIndex];
  49. if (!Mathf.Approximately(value, newValue))
  50. {
  51. property.floatValue = newValue;
  52. if(Application.isPlaying)
  53. {
  54. var objectReferenceValue = property.serializedObject.targetObject;
  55. var type = objectReferenceValue.GetType();
  56. var attribute = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
  57. var methodName = "SetScaleResolutionDown";
  58. var method = type.GetMethod(methodName, attribute);
  59. method.Invoke(objectReferenceValue, new object[] { newValue });
  60. }
  61. }
  62. EditorGUI.EndProperty();
  63. }
  64. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  65. {
  66. if (property == null)
  67. throw new System.ArgumentNullException(nameof(property));
  68. var height = 0f;
  69. // Popup.
  70. height += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  71. return height;
  72. }
  73. }
  74. }