AspectRatioPropertyDrawer.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace UnityEditor.Recorder
  6. {
  7. [CustomPropertyDrawer(typeof(AspectRatio))]
  8. class AspectRatioPropertyDrawer : PropertyDrawer
  9. {
  10. SerializedProperty m_CustomAspectX;
  11. SerializedProperty m_CustomAspectY;
  12. SerializedProperty m_ImageAspect;
  13. bool m_Initialized;
  14. const string k_Format = "0.0###";
  15. static GUIContent[] s_DisplayNames;
  16. static readonly Dictionary<ImageAspect, string> s_AspectToName = new Dictionary<ImageAspect, string>
  17. {
  18. { ImageAspect.x19_10, "19:10 (" + AspectRatio.s_AspectToValue[ImageAspect.x19_10].ToString(k_Format) + ")"},
  19. { ImageAspect.x16_9, "16:9 (" + AspectRatio.s_AspectToValue[ImageAspect.x16_9].ToString(k_Format) + ")"},
  20. { ImageAspect.x16_10, "16:10 (" + AspectRatio.s_AspectToValue[ImageAspect.x16_10].ToString(k_Format) + ")"},
  21. { ImageAspect.x5_4, "5:4 (" + AspectRatio.s_AspectToValue[ImageAspect.x5_4].ToString(k_Format) + ")"},
  22. { ImageAspect.x4_3, "4:3 (" + AspectRatio.s_AspectToValue[ImageAspect.x4_3].ToString(k_Format) + ")"},
  23. { ImageAspect.x3_2, "3:2 (" + AspectRatio.s_AspectToValue[ImageAspect.x3_2].ToString(k_Format) + ")"},
  24. { ImageAspect.x1_1, "1:1 (" + AspectRatio.s_AspectToValue[ImageAspect.x1_1].ToString(k_Format) + ")"},
  25. { ImageAspect.Custom, "Custom"}
  26. };
  27. void Initialize(SerializedProperty property)
  28. {
  29. if (m_Initialized )
  30. return;
  31. m_Initialized = true;
  32. m_CustomAspectX = property.FindPropertyRelative("m_CustomAspectX");
  33. m_CustomAspectY = property.FindPropertyRelative("m_CustomAspectY");
  34. m_ImageAspect = property.FindPropertyRelative("m_ImageAspect");
  35. if (s_DisplayNames == null)
  36. {
  37. s_DisplayNames = ((ImageAspect[]) Enum.GetValues(typeof(ImageAspect))).Select(e => new GUIContent(s_AspectToName[e])).ToArray();
  38. }
  39. }
  40. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  41. {
  42. Initialize(property);
  43. EditorGUI.BeginProperty(position, label, property);
  44. m_ImageAspect.intValue = EditorGUI.Popup(position, label, m_ImageAspect.intValue, s_DisplayNames);
  45. EditorGUI.EndProperty();
  46. var aspect = (ImageAspect) m_ImageAspect.intValue;
  47. if (aspect == ImageAspect.Custom)
  48. {
  49. CustomAspectField();
  50. }
  51. }
  52. void CustomAspectField()
  53. {
  54. var r = EditorGUILayout.GetControlRect();
  55. var rContent = r;
  56. rContent.xMin = r.x + EditorGUIUtility.labelWidth;
  57. EditorGUI.BeginChangeCheck();
  58. int indentLevel = EditorGUI.indentLevel;
  59. var labelWidth = EditorGUIUtility.labelWidth;
  60. EditorGUI.indentLevel = 0;
  61. EditorGUIUtility.labelWidth = 0;
  62. const float columnWidth = 12.0f;
  63. var w = Mathf.Max(30.0f, (rContent.width - columnWidth) / 3.0f);
  64. var rCurrent = rContent;
  65. rCurrent.width = w;
  66. var x = EditorGUI.FloatField(rCurrent, m_CustomAspectX.floatValue);
  67. rCurrent.x += w;
  68. rCurrent.width = columnWidth;
  69. EditorGUI.LabelField(rCurrent, ":");
  70. rCurrent.x += columnWidth;
  71. rCurrent.width = w;
  72. var y = EditorGUI.FloatField(rCurrent, m_CustomAspectY.floatValue );
  73. if (EditorGUI.EndChangeCheck())
  74. {
  75. m_CustomAspectX.floatValue = Mathf.Max(x, 1.0f);
  76. m_CustomAspectY.floatValue = Mathf.Max(y, 1.0f);
  77. }
  78. var aspect = m_CustomAspectX.floatValue / m_CustomAspectY.floatValue;
  79. rCurrent.xMin = rCurrent.xMax + 3.0f;
  80. rCurrent.xMax = r.xMax;
  81. EditorGUI.LabelField(rCurrent, "(" + aspect.ToString(k_Format) + ")");
  82. EditorGUI.indentLevel = indentLevel;
  83. EditorGUIUtility.labelWidth = labelWidth;
  84. }
  85. }
  86. }