StreamingSizeDrawer.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System.Reflection;
  4. namespace Unity.RenderStreaming.Editor
  5. {
  6. [CustomPropertyDrawer(typeof(StreamingSizeAttribute))]
  7. class StreamingSizeDrawer : PropertyDrawer
  8. {
  9. readonly GUIContent[] streamingSizeText =
  10. {
  11. EditorGUIUtility.TrTextContent("640 x 480"),
  12. EditorGUIUtility.TrTextContent("1280 x 720"),
  13. EditorGUIUtility.TrTextContent("1600 x 1200"),
  14. EditorGUIUtility.TrTextContent("1920 x 1200"),
  15. EditorGUIUtility.TrTextContent("2560 x 1440"),
  16. EditorGUIUtility.TrTextContent("Custom")
  17. };
  18. readonly Vector2Int[] streamingSizeValues =
  19. {
  20. new Vector2Int(640, 480),
  21. new Vector2Int(1280, 720), new Vector2Int(1600, 1200),
  22. new Vector2Int(1920, 1200), new Vector2Int(2560, 1440),
  23. };
  24. readonly GUIContent s_StreamingSizeLabel =
  25. EditorGUIUtility.TrTextContent("Streaming Size",
  26. "Streaming size should match display aspect ratio.");
  27. readonly GUIContent s_customValueLabel =
  28. EditorGUIUtility.TrTextContent("Custom Value",
  29. "Supporting resolutions are difference each platforms.");
  30. private static readonly string s_HelpBoxText =
  31. "Note that streaming might not operate properly " +
  32. "when set some resolutions. " +
  33. "Platforms or type of encoders are depended.";
  34. private bool IsCustomValue(Vector2Int value)
  35. {
  36. return !ArrayUtility.Contains(streamingSizeValues, value);
  37. }
  38. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  39. {
  40. EditorGUI.BeginProperty(position, label, property);
  41. Vector2Int value = property.vector2IntValue;
  42. var selectIndex = 0;
  43. while (selectIndex < streamingSizeValues.Length &&
  44. value != streamingSizeValues[selectIndex])
  45. {
  46. ++selectIndex;
  47. }
  48. var popupRect = position;
  49. popupRect.height = EditorGUIUtility.singleLineHeight;
  50. selectIndex = EditorGUI.Popup(popupRect, s_StreamingSizeLabel,
  51. selectIndex, streamingSizeText);
  52. Vector2Int newValue;
  53. var cutomValueRect = position;
  54. cutomValueRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  55. if (selectIndex < streamingSizeValues.Length)
  56. {
  57. newValue = streamingSizeValues[selectIndex];
  58. cutomValueRect.height = 0;
  59. }
  60. else
  61. {
  62. if(!IsCustomValue(value))
  63. {
  64. value = Vector2Int.zero;
  65. }
  66. cutomValueRect.height = EditorGUIUtility.singleLineHeight;
  67. newValue = EditorGUI.Vector2IntField(cutomValueRect, s_customValueLabel, value);
  68. }
  69. if(property.vector2IntValue != newValue)
  70. {
  71. if (Application.isPlaying)
  72. {
  73. var objectReferenceValue = property.serializedObject.targetObject;
  74. var type = objectReferenceValue.GetType();
  75. var attribute = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
  76. var methodName = "SetTextureSize";
  77. var method = type.GetMethod(methodName, attribute);
  78. method.Invoke(objectReferenceValue, new object[] { newValue });
  79. }
  80. else
  81. {
  82. property.vector2IntValue = newValue;
  83. }
  84. }
  85. var helpBoxRect = position;
  86. helpBoxRect.y = cutomValueRect.y + cutomValueRect.height + EditorGUIUtility.standardVerticalSpacing;
  87. helpBoxRect.height = EditorGUIUtility.singleLineHeight * 2;
  88. EditorGUI.HelpBox(helpBoxRect, s_HelpBoxText, MessageType.Info);
  89. EditorGUI.EndProperty();
  90. }
  91. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  92. {
  93. if (property == null)
  94. throw new System.ArgumentNullException(nameof(property));
  95. var height = 0f;
  96. // Popup.
  97. height += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  98. // Custom values
  99. Vector2Int value = property.vector2IntValue;
  100. if (IsCustomValue(value))
  101. {
  102. height += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  103. }
  104. //helpbox;
  105. height += EditorGUIUtility.singleLineHeight * 2 + EditorGUIUtility.standardVerticalSpacing;
  106. return height;
  107. }
  108. }
  109. }