Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by chillypacman · Nov 27, 2011 at 07:48 AM · drag-and-drop

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...

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
6

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.

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sphax84 · Feb 10, 2018 at 07:57 AM 0
Share

This is awesome! I’ll try your solution ASAP! Thanks a lot! :)

avatar image alelepd · May 05, 2018 at 01:28 AM 0
Share

This is great! Any idea of how to do something like this for mac? I haven't found any solution for mac standalone :/

avatar image Bunny83 alelepd · May 05, 2018 at 02:48 AM 0
Share

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 ^^.

avatar image Aiden83 · May 05, 2018 at 12:28 PM 0
Share

This solution works wonderfully! Thank you a ton Bunny83 for your time!

avatar image $$anonymous$$ · Jun 07, 2018 at 11:58 PM 0
Share

Excellent solution, thanks!

avatar image GuruJeya14 · Jul 03, 2018 at 07:28 AM 0
Share

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??

avatar image Bunny83 GuruJeya14 · Jul 03, 2018 at 05:19 PM 0
Share

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

avatar image GuruJeya14 Bunny83 · Jul 09, 2018 at 11:19 AM 0
Share

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 :)

Show more comments
avatar image
2

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.

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image chillypacman · Nov 28, 2011 at 12:12 AM 0
Share

how does the unity editor work then?

avatar image Bunny83 · Nov 28, 2011 at 12:33 AM 0
Share

@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 ;)

avatar image phileday Bunny83 · Mar 27, 2018 at 06:26 PM 0
Share
  • 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

avatar image Bunny83 phileday · Mar 27, 2018 at 10:28 PM 0
Share

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 ^^.

Show more comments
avatar image cecarlsen · Dec 04, 2012 at 01:47 PM 1
Share

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.

avatar image abuklea · Mar 24, 2015 at 12:05 AM 0
Share

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?

avatar image Sphax84 abuklea · Nov 09, 2016 at 06:32 PM 0
Share

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

avatar image Calum1015 · Mar 24, 2015 at 12:10 AM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges