17 [CustomPropertyDrawer(typeof(
AssetFolder), useForChildren:
true)]
20 public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
22 position.SplitHorizontallyWithRight(out left, out right, position.height);
26 string folderPath =
"";
28 SerializedProperty folderProp =
property.FindPropertyRelative(
"_assetFolder");
29 if (folderProp.hasMultipleDifferentValues) {
30 EditorGUI.showMixedValue =
true;
32 folderAsset = folderProp.objectReferenceValue;
33 if (folderAsset !=
null) {
34 folderPath = AssetDatabase.GetAssetPath(folderAsset);
38 EditorGUI.TextField(left, label, folderPath);
40 var content = EditorGUIUtility.IconContent(
"Folder Icon");
42 if (GUI.Button(right, content, GUIStyle.none)) {
44 if (!
string.IsNullOrEmpty(resultPath)) {
45 string relativePath = Utils.MakeRelativePath(Application.dataPath, resultPath);
46 var asset = AssetDatabase.LoadAssetAtPath<DefaultAsset>(relativePath);
49 if (!
ValidatePath(resultPath, relativePath, out errorMessage)) {
50 EditorUtility.DisplayDialog(
"Invalid selection.", errorMessage,
"OK");
52 folderProp.objectReferenceValue = asset;
57 EditorGUI.showMixedValue =
false;
59 if (position.Contains(Event.current.mousePosition)) {
60 var draggedObject = DragAndDrop.objectReferences.FirstOrDefault();
62 if (draggedObject !=
null) {
63 switch (Event.current.type) {
64 case EventType.DragUpdated:
66 DragAndDrop.visualMode = DragAndDropVisualMode.Link;
68 DragAndDrop.visualMode = DragAndDropVisualMode.Rejected;
71 case EventType.DragPerform:
73 DragAndDrop.AcceptDrag();
74 folderProp.objectReferenceValue = draggedObject;
83 return EditorGUIUtility.singleLineHeight;
87 return EditorUtility.OpenFolderPanel(
"Select Folder", currentPath,
"");
91 string relativePath = AssetDatabase.GetAssetPath(asset);
92 string fullPath = Path.GetFullPath(relativePath);
93 return ValidatePath(fullPath, relativePath, out errorMessage);
96 protected virtual bool ValidatePath(
string fullPath,
string relativePath, out
string errorMessage) {
97 if (!Directory.Exists(fullPath)) {
98 errorMessage =
"The specified folder does not exist!";
102 var asset = AssetDatabase.LoadAssetAtPath<DefaultAsset>(relativePath);
107 errorMessage =
"The specified folder is not an asset folder. Asset folders must be inside project's Assets directory.";
A convenient serializable representation of an asset folder. Only useful for editor scripts since ass...
virtual bool ValidateObject(Object asset, out string errorMessage)
override float GetPropertyHeight(SerializedProperty property, GUIContent label)
virtual bool ValidatePath(string fullPath, string relativePath, out string errorMessage)
virtual string PromptUserForPath(string currentPath)
override void OnGUI(Rect position, SerializedProperty property, GUIContent label)