Hi All,
is their anyway to drag and drop a file onto a text field to fill the field out with the name of the file? EG I have a file PlayerData.xml, how can I just drag onto a text field to show PlayerData.xml, or better the full path? Thanks
Yes, you can achieve that by using the different DragAndDropEvent
(see here for more details).
Here is a minimal example to do what you want:
Thanks.
Hi compiling however I can’t get it to accept. In the below
if (DragAndDrop.objectReferences.Length == 1 && DragAndDrop.objectReferences[0] is VisualTreeAsset )
{
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
Debug.Log("OnDragUpdatedEvent COPY");
return;
}
How do I get it to accept xml files? EG DragAndDrop.objectReferences[0] is VisualTreeAsset to DragAndDrop.objectReferences[0] is xml file? Can’t debug to work this out as debugging ( VSC ) won’t work with 2021 and I can’t find any documentation for DragAndDrop.objectReferences.
Thanks
If you start a drag and drop from a uxml file in the project window, the DragAndDrop.objectReferences
should be populated automatically. The uxml file itself gets imported as a VisualTreeAsset
.
For good measure, the sample is missing a call to DragAndDrop.AcceptDrag();
in the DragPerformEvent
.
In the sample above, dragging the uxml file on top of the TextField
should change the cursor to the DragAndDropVisualMode.Copy
.
Hi Sorry to keep on about this, I’m having no luck. OnDragUpdatedEvent callback is called all the time when I’ve grabbed the xml file and hold it over the box. However it keeps rejecting, I can see the length is 1 from my debug output so I presume the xml is not being see by DragAndDrop.objectReferences[0] as a visualtreeasset?
.
OnDragPerformed is never called. I know in Qt I need to click a box to tell a item ( textbox ) to be dropable. Do I need to do some sort of on drop?
EDIT Got it working, it doesn’t like objectreference bit and I filled the textbox by getting the path of the object. Thanks for pointing me in the right direction
void OnDragUpdatedEvent(DragUpdatedEvent evt)
{
if (DragAndDrop.objectReferences.Length == 1 )//&& DragAndDrop.objectReferences[0] is VisualTreeAsset )
{
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
return;
}
DragAndDrop.visualMode = DragAndDropVisualMode.Rejected;
}
void OnDragPerformed(DragPerformEvent evt)
{
string path = DragAndDrop.paths[ 0];
Debug.Log("Path " + path );
this.text_field.value = path;
}