Skip to content

Instantly share code, notes, and snippets.

@FlaShG FlaShG/PathAttribute.cs
Last active Jul 4, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.