- Home /
Drag and drop files into unity3d window?
If I have a Unity app running in a browser or just in a window natively on a machine is it possible ot get it to accept files I drag and drop into it? Like I grab a jpeg and drag it over the unity window and just drop it, could unity grab that and read it in (possibly also load it up froma file directory).
Just trying to see if I can make something like a photogallery using unity...
Answer by Bunny83 · Feb 10, 2018 at 06:17 AM
I've just created a drag and drop solution for windows standalone builds. See the project on github. It doesn't require a native code plugin. All is done from managed code and only uses several win API functions through P/Invoke. It installs a GetMessage hook for the main thread which is able to intercept the WM_DROPFILES message that the application receives upon dropping files onto your application.
Some important notes:
Make sure the instance of the "UnityDragAndDropHook" does stay in memory. Always call UninstallHook before you dismiss the instance.
Do not create several instances and do not install the hook several times.
The returned file paths are just returned as they have been passed by the OS. Note that "link files"(*.lnk) are not resolved.
Keep in mind that a path doesn't need to represent a file but could also be a folder.
This is awesome! I’ll try your solution ASAP! Thanks a lot! :)
This is great! Any idea of how to do something like this for mac? I haven't found any solution for mac standalone :/
No, sorry. I never had a mac (and never will ^^). I'm not familiar with mac OS and it's APIs. I grew up with DOS and windows ^^.
This solution works wonderfully! Thank you a ton Bunny83 for your time!
I have doubts in using it. It would me more fine if the readme.text had more detailed description.
So now, i have attached the FileDragAndDrop.cs to a game object in the editor. But when i hit play,and drag and drop a file into the game window, nothing happens. What am i missing??
Uhm first of all it's not meant to work inside the editor. It may work but it depends on several things. $$anonymous$$aybe you have a too old Unity version. $$anonymous$$aybe you have a strange editor window layout so your game window is not the window receiving the drop event. $$anonymous$$aybe you have a too old Windows version. Have you tried a standalone build? Of course you don't see the Debug.Log message in your build, but you should see it in the output_log.txt. However you may just show the dropped file in an UI text on screen for debugging
Actually it worked in the built!! :) But not in the editor. I don't know, it may be because of the reasons mentioned by you. Thanks a lot for your support :)
Answer by Graham-Dunnett · Nov 27, 2011 at 11:03 PM
Am pretty sure that Unity applications do not respond to the WM_DROPFILES windows message, so, no, I don't think you can do that.
@JabberWorx: The UnityEditor is written as a native application with full control over all system messages. As a user of Unity you only can use what the scripting environment allows / offers you. As Graham said the message pump of windows belongs to the application itself. Even when you use DragAcceptFiles you can't receive the W$$anonymous$$_DROPFFILES message in your scripts.
Well it would be possible to subclass the main window from a native plugin but it's more a hack than a solution. For webbuilds there is absolutely no way.
edit
I've just posted a feature request for drag&drop support here. Feel free to vote ;)
Edit. I seem to be getting it working but only in the build not the edit. Still it's a start :) thanks for your hard work.
Hi
This might be a stupid question as I'm pretty new to C sharp but I'm having difficulty getting your script to work. I've downloaded the scripts. Created a new project. Created and Empty Object. Dragged the FileDragAndDrop.cs onto it. Then hit play and dragged a file onto the window. I don't then get anything in the console. I was expecting the debug info that was the FileDragAndDrop file. Am I doing something wrong here?
Sorry if this is a dumb question. I've been looking for a solution to this for a long time as I make a video player called Whirligig and being able to drag and drop a video file onto the window to play it would be awesome.
Any help you or anyone else can give would be amazing.
All the best
Phil
As far as i remember it originally worked in the editor but i restricted it to the build game due to the window classname. The window that the unity player (a unity build game) creates has the window class "UnityWndClass" while any window of the Unity editor (the main window or any floating / undocked window) has the window class "UnityContainerWndClass". In the editor it was a bit tricky to actually select the correct window ("window" in the sense of OS window). Also the Unity editor already reacts to file drag and drop events so that could result in some strange interference. It as meant only for a build solution.
Though in the editor it should simply pick the first window accociated with the current threadID. This could be the main window but it could be any other floating window you might have open at the moment ^^.
File drag & drop to the Unity window of Windows and OS X standalone builds would be extremely useful to me. I'm happy to hear I'm not the only one.
The last comment on this question was a few years ago.. does anyone know if there has been any progress on this, and if it is possible to achieve this in Unity 5?
I search something like that as well but have not found anything for Windows, $$anonymous$$acOSX or Linux yet...
The best thing I found is for WebGL only: https://www.assetstore.unity3d.com/en/#!/content/25846
I think you may be able to program a plugin into unity to add this however I cannot suggest how that would work, I guess grab a book on the Windows OS and try and figure it out? It would be hard.
Your answer
Follow this Question
Related Questions
Click and drag an object 3 Answers
PropertyDrawer DragAndDrop AnimationCurve 1 Answer
drag and drop in specific area Unity 3 Answers
Sorted assets when drag-and-drop into array? 0 Answers
Mouse drag cloning Game Objects 1 Answer