Allows setting a string in the Unity Editor by dragging a file or folder from outside the editor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class PathAttribute : PropertyAttribute | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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