- Home /
Custom Editor: show asset folders in ObjectField
Hello, is there a way to use ObjectField (or a more apt field) to show the list of Assets' subdirectories and pick one? I'd like to avoid using the open folder dialog provided by Unity (I don't remember the method name now) because I could open any path in my computer and I don't think it has parameters to limit the scope of selectable directories.
Answer by Piranha771 · Apr 09, 2018 at 09:29 AM
To provide an answer for this question. It is possible. Here is a working example editor window:
using UnityEngine;
using UnityEditor;
class FolderTester: EditorWindow
{
private DefaultAsset targetFolder = null;
[MenuItem ("Window/Folder Selection Example")]
public static void ShowWindow ()
{
EditorWindow.GetWindow(typeof(FolderTester));
}
void OnGUI ()
{
targetFolder = (DefaultAsset)EditorGUILayout.ObjectField(
"Select Folder",
targetFolder,
typeof(DefaultAsset),
false);
if (targetFolder != null) {
EditorGUILayout.HelpBox(
"Valid folder! Name: " + targetFolder.name,
MessageType.Info,
true);
}
else
{
EditorGUILayout.HelpBox(
"Not valid!",
MessageType.Warning,
true);
}
}
}
Answer by Djayp · Nov 28, 2019 at 06:34 PM
You can limit the scope validating it after the folder has been chosen. Here is a complete PropertyDrawer I just made to keep folder references in the editor :
using System.IO;
using UnityEditor;
using UnityEngine;
[System.Serializable]
public class FolderReference
{
public string guid;
}
[CustomPropertyDrawer(typeof(FolderReference))]
public class FolderReferencePropertyDrawer : PropertyDrawer
{
private bool initialized;
private SerializedProperty guid;
private Object obj;
private void Init(SerializedProperty property)
{
initialized = true;
guid = property.FindPropertyRelative("guid");
obj = AssetDatabase.LoadAssetAtPath<Object>(AssetDatabase.GUIDToAssetPath(guid.stringValue));
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (!initialized) Init(property);
GUIContent guiContent = EditorGUIUtility.ObjectContent(obj, typeof(DefaultAsset));
Rect r = EditorGUI.PrefixLabel(position, label);
Rect textFieldRect = r;
textFieldRect.width -= 19f;
GUIStyle textFieldStyle = new GUIStyle("TextField")
{
imagePosition = obj ? ImagePosition.ImageLeft : ImagePosition.TextOnly
};
if (GUI.Button(textFieldRect, guiContent, textFieldStyle) && obj)
EditorGUIUtility.PingObject(obj);
if (textFieldRect.Contains(Event.current.mousePosition))
{
if (Event.current.type == EventType.DragUpdated)
{
Object reference = DragAndDrop.objectReferences[0];
string path = AssetDatabase.GetAssetPath(reference);
DragAndDrop.visualMode = Directory.Exists(path) ? DragAndDropVisualMode.Copy : DragAndDropVisualMode.Rejected;
Event.current.Use();
}
else if (Event.current.type == EventType.DragPerform)
{
Object reference = DragAndDrop.objectReferences[0];
string path = AssetDatabase.GetAssetPath(reference);
if (Directory.Exists(path))
{
obj = reference;
guid.stringValue = AssetDatabase.AssetPathToGUID(path);
}
Event.current.Use();
}
}
Rect objectFieldRect = r;
objectFieldRect.x = textFieldRect.xMax + 1f;
objectFieldRect.width = 19f;
if (GUI.Button(objectFieldRect, "", GUI.skin.GetStyle("IN ObjectField")))
{
string path = EditorUtility.OpenFolderPanel("Select a folder", "Assets", "");
if (path.Contains(Application.dataPath))
{
path = "Assets" + path.Substring(Application.dataPath.Length);
obj = AssetDatabase.LoadAssetAtPath(path, typeof(DefaultAsset));
guid.stringValue = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(obj));
}
else Debug.LogError("The path must be in the Assets folder");
}
}
}
Your answer
Follow this Question
Related Questions
Setting TextAsset[] from an Editor 0 Answers
Select asset for rename 2 Answers
editor folder path on Mac 0 Answers
Working with the editor Selection 0 Answers
How to get the top most GameObject selected in Editor 0 Answers