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.
g4ma
November 17, 2020, 9:01am
2
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 Unity - Scripting API: Editor
Usage:
public class MySettings : ScriptableObject
{
[SerializeField]
string PathProperty;
}
[CustomEditor(typeof(MySettings))]
public class MySettingsEditor : Editor
{
Object _obj = null;
// ...
public override void OnInspectorGUI()
{
// ...
_obj = GetPathPropertyFromAssetObject(_obj, serializedObject.FindProperty("PathProperty"));
}
}
How it’s done:
static Object GetPathPropertyFromAssetObject(Object obj, SerializedProperty property)
{
Object folderAssetObj = null;
string existingFolderPath = property.stringValue;
if (existingFolderPath != null)
{
folderAssetObj = AssetDatabase.LoadAssetAtPath<Object>(existingFolderPath);
}
Object newFolderAssetObj = EditorGUILayout.ObjectField(property.displayName, folderAssetObj, typeof(Object), false);
if (newFolderAssetObj == folderAssetObj)
{
return folderAssetObj;
}
if (newFolderAssetObj != null)
{
string newPath = AssetDatabase.GetAssetPath(newFolderAssetObj);
if (newPath != null && System.IO.Directory.Exists(newPath))
{
property.stringValue = newPath;
return newFolderAssetObj;
}
}
return null;
}