Skip to content

Instantly share code, notes, and snippets.

@FlaShG
Last active July 4, 2018 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FlaShG/09a6b44bedc2805058b3855a22365dc4 to your computer and use it in GitHub Desktop.
Save FlaShG/09a6b44bedc2805058b3855a22365dc4 to your computer and use it in GitHub Desktop.
Allows setting a string in the Unity Editor by dragging a file or folder from outside the editor.
using UnityEngine;
public class PathAttribute : PropertyAttribute
{
}
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(PathAttribute))]
public class PathAttributeDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
var currentEvent = Event.current;
var currentEventType = currentEvent.type;
if (currentEventType == EventType.DragExited) DragAndDrop.PrepareStartDrag();
if (position.Contains(currentEvent.mousePosition))
{
switch (currentEventType)
{
case EventType.DragUpdated:
DragAndDrop.visualMode = DragAndDropVisualMode.Link;
currentEvent.Use();
break;
case EventType.DragPerform:
DragAndDrop.AcceptDrag();
if (DragAndDrop.paths.Length > 0)
{
property.stringValue = DragAndDrop.paths[0];
currentEvent.Use();
}
break;
}
}
property.stringValue = EditorGUI.TextField(position, property.stringValue ?? "");
EditorGUI.EndProperty();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment