OutputPathDrawer.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System.IO;
  2. using UnityEngine;
  3. namespace UnityEditor.Recorder
  4. {
  5. [CustomPropertyDrawer(typeof(OutputPath))]
  6. class OutputPathDrawer : TargetedPropertyDrawer<OutputPath>
  7. {
  8. SerializedProperty m_RootProperty;
  9. SerializedProperty m_LeafProperty;
  10. SerializedProperty m_ForceAssetFolder;
  11. protected override void Initialize(SerializedProperty property)
  12. {
  13. base.Initialize(property);
  14. if (m_RootProperty == null)
  15. m_RootProperty = property.FindPropertyRelative("m_Root");
  16. if (m_LeafProperty == null)
  17. m_LeafProperty = property.FindPropertyRelative("m_Leaf");
  18. if (m_ForceAssetFolder == null)
  19. m_ForceAssetFolder = property.FindPropertyRelative("m_ForceAssetFolder");
  20. }
  21. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  22. {
  23. Initialize(property);
  24. EditorGUI.BeginProperty(position, label, property);
  25. position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
  26. var indent = EditorGUI.indentLevel;
  27. EditorGUI.indentLevel = 0;
  28. const float rootWidth = 110.0f;
  29. const float btnWidth = 30.0f;
  30. var leafWidth = target.forceAssetsFolder ? position.width - rootWidth : position.width - rootWidth - btnWidth - 10;
  31. var rootRect = new Rect(position.x, position.y, rootWidth, position.height);
  32. var leafRect = new Rect(position.x + rootWidth + 5, position.y, leafWidth, position.height);
  33. var btnRect = new Rect(position.x + rootWidth + leafWidth + 10, position.y, btnWidth, position.height);
  34. if (target.forceAssetsFolder)
  35. {
  36. var root = (OutputPath.Root) m_RootProperty.intValue;
  37. GUI.Label(rootRect, root + " " + Path.DirectorySeparatorChar);
  38. }
  39. else
  40. {
  41. EditorGUI.PropertyField(rootRect, m_RootProperty, GUIContent.none);
  42. }
  43. EditorGUI.PropertyField(leafRect, m_LeafProperty, GUIContent.none);
  44. var fullPath = OutputPath.GetFullPath((OutputPath.Root)m_RootProperty.intValue, m_LeafProperty.stringValue);
  45. if (!target.forceAssetsFolder)
  46. {
  47. if (GUI.Button(btnRect, new GUIContent("...", fullPath)))
  48. {
  49. var newPath = EditorUtility.OpenFolderPanel("Select output location", fullPath, "");
  50. if (!string.IsNullOrEmpty(newPath))
  51. {
  52. var newValue = OutputPath.FromPath(newPath);
  53. m_RootProperty.intValue = (int) newValue.root;
  54. m_LeafProperty.stringValue = newValue.leaf;
  55. }
  56. }
  57. }
  58. EditorGUI.indentLevel = indent;
  59. EditorGUI.EndProperty();
  60. }
  61. }
  62. }