Tanoda
AssetFolderPropertyDrawer.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System.IO;
10using System.Linq;
11using UnityEngine;
12using UnityEditor;
13using Leap.Unity.Query;
14
15namespace Leap.Unity {
16
17 [CustomPropertyDrawer(typeof(AssetFolder), useForChildren: true)]
18 public class AssetFolderPropertyDrawer : PropertyDrawer {
19
20 public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
21 Rect left, right;
22 position.SplitHorizontallyWithRight(out left, out right, position.height);
23 left.width -= 2;
24
25 Object folderAsset = null;
26 string folderPath = "";
27
28 SerializedProperty folderProp = property.FindPropertyRelative("_assetFolder");
29 if (folderProp.hasMultipleDifferentValues) {
30 EditorGUI.showMixedValue = true;
31 } else {
32 folderAsset = folderProp.objectReferenceValue;
33 if (folderAsset != null) {
34 folderPath = AssetDatabase.GetAssetPath(folderAsset);
35 }
36 }
37
38 EditorGUI.TextField(left, label, folderPath);
39
40 var content = EditorGUIUtility.IconContent("Folder Icon");
41
42 if (GUI.Button(right, content, GUIStyle.none)) {
43 string resultPath = PromptUserForPath(folderPath);
44 if (!string.IsNullOrEmpty(resultPath)) {
45 string relativePath = Utils.MakeRelativePath(Application.dataPath, resultPath);
46 var asset = AssetDatabase.LoadAssetAtPath<DefaultAsset>(relativePath);
47
48 string errorMessage;
49 if (!ValidatePath(resultPath, relativePath, out errorMessage)) {
50 EditorUtility.DisplayDialog("Invalid selection.", errorMessage, "OK");
51 } else {
52 folderProp.objectReferenceValue = asset;
53 }
54 }
55 }
56
57 EditorGUI.showMixedValue = false;
58
59 if (position.Contains(Event.current.mousePosition)) {
60 var draggedObject = DragAndDrop.objectReferences.FirstOrDefault();
61 string errorMessage;
62 if (draggedObject != null) {
63 switch (Event.current.type) {
64 case EventType.DragUpdated:
65 if (ValidateObject(draggedObject, out errorMessage)) {
66 DragAndDrop.visualMode = DragAndDropVisualMode.Link;
67 } else {
68 DragAndDrop.visualMode = DragAndDropVisualMode.Rejected;
69 }
70 break;
71 case EventType.DragPerform:
72 if (ValidateObject(draggedObject, out errorMessage)) {
73 DragAndDrop.AcceptDrag();
74 folderProp.objectReferenceValue = draggedObject;
75 }
76 break;
77 }
78 }
79 }
80 }
81
82 public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
83 return EditorGUIUtility.singleLineHeight;
84 }
85
86 protected virtual string PromptUserForPath(string currentPath) {
87 return EditorUtility.OpenFolderPanel("Select Folder", currentPath, "");
88 }
89
90 protected virtual bool ValidateObject(Object asset, out string errorMessage) {
91 string relativePath = AssetDatabase.GetAssetPath(asset);
92 string fullPath = Path.GetFullPath(relativePath);
93 return ValidatePath(fullPath, relativePath, out errorMessage);
94 }
95
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!";
99 return false;
100 }
101
102 var asset = AssetDatabase.LoadAssetAtPath<DefaultAsset>(relativePath);
103 if (asset != null) {
104 errorMessage = null;
105 return true;
106 } else {
107 errorMessage = "The specified folder is not an asset folder. Asset folders must be inside project's Assets directory.";
108 return false;
109 }
110 }
111 }
112}
A convenient serializable representation of an asset folder. Only useful for editor scripts since ass...
Definition: AssetFolder.cs:26
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)
UnityEngine.Object Object