Search Unity

Does drag and drop a folder in GUI exist?

Discussion in 'Immediate Mode GUI (IMGUI)' started by FeastSC2, Jun 1, 2017.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Is it possible to create a field where I can drag and drop a folder into?
    I'd like my programmatically created assets to be stored in that specified folder.

    I tried "_animFolder = (object)EditorGUILayout.ObjectField(_targetAnimClip, typeof(object), true);" to no avail.
     
  2. g4ma

    g4ma

    Joined:
    Dec 18, 2018
    Posts:
    32
    Hello! This is a super old topic but this might help others like me wondering about it as well.
    Here is what I've done, slightly nasty but functional.

    I've skipped some boilerplate code that you will find in https://docs.unity3d.com/ScriptReference/Editor.html

    Usage:

    Code (CSharp):
    1. public class MySettings : ScriptableObject
    2. {
    3.     [SerializeField]
    4.     string PathProperty;
    5. }
    6.  
    7. [CustomEditor(typeof(MySettings))]
    8. public class MySettingsEditor : Editor
    9. {
    10.     Object _obj = null;
    11.  
    12.     // ...
    13.  
    14.     public override void OnInspectorGUI()
    15.     {
    16.         // ...
    17.         _obj = GetPathPropertyFromAssetObject(_obj, serializedObject.FindProperty("PathProperty"));
    18.     }
    19. }
    How it's done:
    Code (CSharp):
    1. static Object GetPathPropertyFromAssetObject(Object obj, SerializedProperty property)
    2. {
    3.     Object folderAssetObj = null;
    4.     string existingFolderPath = property.stringValue;
    5.     if (existingFolderPath != null)
    6.     {
    7.         folderAssetObj = AssetDatabase.LoadAssetAtPath<Object>(existingFolderPath);
    8.     }
    9.     Object newFolderAssetObj = EditorGUILayout.ObjectField(property.displayName, folderAssetObj, typeof(Object), false);
    10.     if (newFolderAssetObj == folderAssetObj)
    11.     {
    12.         return folderAssetObj;
    13.     }
    14.     if (newFolderAssetObj != null)
    15.     {
    16.         string newPath = AssetDatabase.GetAssetPath(newFolderAssetObj);
    17.         if (newPath != null && System.IO.Directory.Exists(newPath))
    18.         {
    19.             property.stringValue = newPath;
    20.             return newFolderAssetObj;
    21.         }
    22.     }
    23.     return null;
    24. }