Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Native File Browser - Use native File Browser on Mac and Windows!

Discussion in 'Assets and Asset Store' started by PygmyMonkey, Jul 26, 2016.

  1. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    Native File Browser
    Use native File Browser on Mac and Windows!
    Native File Browser provides an easy way to use the native File Browser on Mac and Windows.

    Features
    - Open File Panel / Open Multiple Files Panel,
    - Open Folder Panel / Open Multiple Folders Panel,
    - Save File Panel,
    - Define a starting directory,
    - Define the file extensions to be used,
    - Clean and documented C# code.


    Links
    - Available on the Asset Store,
    - Website with full documentation, examples and FAQ.



     
    Last edited: Apr 6, 2021
    AlanMattano and Lohoris2 like this.
  2. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
  3. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
    Nice!
    Does it behave like the usual Filebrowser as we´re used to, e.g is it dragable and resizable?
     
  4. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    Yes it's the real native file browser from Windows and Mac :)
    Windows: dragable and resizable
    Mac: resizable but not dragable (because I made it attached to the top of the window, see screenshot below)

    (The blur in the background is not a result of opening FileBrowserNative, I just edited the screenshot to hide something I'm working on :D)
     
  5. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
    Nice :)
    Is it possible to trial?
     
  6. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    Well, there's no way to do that with the Asset Store :/
    I'll PM you to see what we can do.
     
    Lohoris2 likes this.
  7. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
    Just bought it:
    It works like a charm and is exactly what I have been waiting for! :)
     
    PygmyMonkey likes this.
  8. exitsimulation

    exitsimulation

    Joined:
    Feb 10, 2014
    Posts:
    82
    Hey!

    It seems to work very nicely so far.
    However, I am facing some problems using your onDone action. It seems that many operations are not allowed here because the action is invoked from another thread (verified by looking at your source code). For example I cannot save to PlayerPrefs here or access gameObjects without throwing an exception (e.g. "get_gameObject can only be called from the main thread.")

    Could you move the invocation of the onDone action to the main thread somehow or are there other workarounds for this problem? Then I think your plug-in would be perfect.

    Thanks a lot!

    Edit:
    Also I have problems setting the starting path of the browser. It does not seem to accept special paths like Application.streamingAssetsPath or strings like "D:\"... How can I use it correctly? So far it only works with the Environment.Special folders for me.
     
    Last edited: Aug 5, 2016
  9. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,251
    This is a much needed feature for some types of Unity project!
    How does it work technically - does it run a separate exe that creates the file browser window and then communicates back to the main app?
     
  10. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    Hey,
    Thanks for pointing this out!

    In order for this to work, you'll have to use a Dispatcher to run something on the main frame whenever possible. Because FileBrowser (on Windows) is launched calling a FileBrowser.exe via Process.Start(), I launch that in a thread, otherwise it blocks the entire game window. And so you're not on the main Unity thread, and can't do normal actions.

    So you'll have to use a Dispatcher. You can find one right here:
    https://github.com/nickgravelyn/UnityToolbag/tree/master/Dispatcher
    With some explanations on the blog post here:
    http://blog.theknightsofunity.com/using-threads-unity/

    It's real easy to use :)
    Instead of directly calling your code like : MyMethod();
    You just do : Dispatcher.Invoke(MyMethod); or Dispatcher.Invoke(() => { myCodeHere... });

    I'll add that to the readme and the demo script, thanks!
     
  11. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    Hello!

    - On Mac, it uses native plugins, that is some Objective-C code, compiled into a bundle and used/called directly from Unity.
    - On Windows, at first I wanted to make a DLL that does all the work. But in order to use the FileBrowser UI we're used to, you have to target at least .NET 4.5 when creating the DLL. Which is not compatible and won't load in Unity...

    So you'll have to target .NET 3.5. But when you're doing that, C# will use an old, ugly UI for the FIleBrowser:



    So yes, as you said. I build a FileBrowser.exe file (with .NET 4.5), place it in the StreamingAssets folder (to be able to find/load it at runtime in a build), and communicates back to the main app by reading the return value of the Process.Start method (see FileBrowserWindows.cs for that).
     
  12. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    Thanks a lot for this too :)
    You're right, there's an issue with startingDirectory!
    But it only happens if the path you specify in startingDirectory contains / instead of \
    So for now, try doing yourStartingDirectory.Replace("/", @"\") for now, and I'll fix that for the next version :)
     
  13. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    NativeFileBrowser 1.0.1 is now available on the Asset Store :)
    • FIX: Fixed Native File Browser not working when building with architecture x86 on Mac. It now works with any architecture (Universal, x86 and x64)
    • NEW: The FileBrowser on Mac now always display the “Create folder” button
     
  14. exitsimulation

    exitsimulation

    Joined:
    Feb 10, 2014
    Posts:
    82
    Thanks for the help with the dispatcher! Works now. :)

    I did that and it works without problems for most paths. Root paths of volumes however produce problems even though I verified that they are formatted with backslash (e.g. "D:\").
     
    Last edited: Aug 10, 2016
  15. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    Yes, I also fixed this, don't worry :)
    If you can't wait for the update, you should be able to use "D:\\"
     
  16. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    NativeFileBrowser 1.0.2 is now available on the Asset Store!
    • FIX: Windows: Fix startingDirectory not working if the path contained / instead of \.
      You can now safely use paths with either / or \.
    • FIX: Windows: Fixed possible issue because result return was not Trim(),
    • NEW: Added documentation on how to call Unity main thread methods in the FileBrowser callback.
      See the section called: 'Fix "Action invoked from another thread"',
    • NEW: Added FAQ "Why are you using a .exe file on Windows?".
     
  17. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    NativeFileBrowser 1.0.3 is now available on the Asset Store!
    • FIX: Windows: Fixed issue happening when a LOT of files were selected.
     
  18. ModSimTeam

    ModSimTeam

    Joined:
    Sep 9, 2012
    Posts:
    7
    This is seems to work great, however, in full screen mode Unity gets minimized. This is sort of ok if Unity gets maximized again after file or folder selection. My two questions are:

    1. Can we prevent Unity from being minimized when the file dialog is opened?
    2. If we can't prevent Unity from minimizing, can we have Unity maximized again after the file dialog is closed?
     
  19. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    Hey :)

    1. I don't think I can prevent that. As I told you in an email, we call an external process to open the Native File Browser, and we can't have two processes at the same time in fullscreen mode. We'll have to wait for Unity to update .NET to 4.5 to be able to hopefully open the File Browser without leaving the fullscreen mode.

    2. Good idea, I'll try to take a look into it!
     
  20. sumpfkraut

    sumpfkraut

    Joined:
    Jan 18, 2013
    Posts:
    242
    1. This addon works very well!
    2. Yes, Fullscreen without minimize unity wold be nice.
    3. It seems you can't open files with browsing Network. It works only with connected Network Drives.
    4. WebGL support would be nice :) I currently use jslib's
     
  21. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    Thanks a lot :)

    3. "It seems you can't open files with browsing Network. It works only with connected Network Drives."
    Can you elaborate a little bit on this? Thanks!

    4. And yes, WebGL support would be nice, but it's not really in my skills :/
     
    Last edited: Oct 6, 2016
  22. sumpfkraut

    sumpfkraut

    Joined:
    Jan 18, 2013
    Posts:
    242
    path with: \\Servername\.... fail.
    same path with a assigned drive letter... like D:\.... works.
     

    Attached Files:

  23. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    Hey!

    Sorry I was on vacation and couldn't answer...
    So, I've just tried it, and everything seems to be working fine :/
    I don't really know what your problem is. Can you elaborate?
    Are you trying to open a file? a folder? When does the issue happen? When you have the file browser opened and select a file/folder? Then what? The game crashes?

    Thanks :)
     
  24. sumpfkraut

    sumpfkraut

    Joined:
    Jan 18, 2013
    Posts:
    242
    It seems the problem is.... i tried to load a texture with WWW(url), and this does not work with shared network locations.
    But this is not a problem with your script, sorry.

    The errror message was: Could not resolve host: \\hostname...
    I made a exception for network paths now. my code if you are interested...
    Code (CSharp):
    1.         Texture2D texture = null;
    2.         if(url[0].ToString() == "\\")
    3.         {
    4.             #if UNITY_STANDALONE
    5.             byte[] filedata;
    6.             filedata = System.IO.File.ReadAllBytes(url);
    7.             yield return filedata;
    8.             texture = new Texture2D (1, 1);
    9.             texture.LoadImage(filedata);
    10.             #else
    11.             Debug.Log("Shared network locations for standalone only");
    12.             #endif
    13.         }
    14.         else
    15.         {
    16.             WWW image = new WWW ("file:///" + url);
    17.             yield return image;
    18.             texture = new Texture2D (1, 1);
    19.             image.LoadImageIntoTexture (texture);
    20.         }
     
  25. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    Thanks for sharing that.
    But it's weird that you can't do that with WWW...
    Maybe try doing something like:
    file://\\hostname...
    Always start with file:// (with two slashes) and add the path.

    Let me know if that works :)
     
  26. sumpfkraut

    sumpfkraut

    Joined:
    Jan 18, 2013
    Posts:
    242
    true, it works with 2 slashes.
    and i made a other mistake with the url, because WebGL dont need the file:// part.
    I have tested this now and it works for win, osx and webgl.

    Code (CSharp):
    1.  
    2.         #if UNITY_STANDALONE
    3.             url = "file://" + url;
    4.         #endif
    5.         WWW image = new WWW (url); //WebGL, url as blob (blob:null/xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx)
    6.         yield return image;
    7.         Texture2D texture = new Texture2D (1, 1);
    8.         image.LoadImageIntoTexture (texture);
    9.  
     
  27. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    Perfect! Great to know :)
     
  28. mowax74

    mowax74

    Joined:
    Mar 3, 2015
    Posts:
    96
    On Win64 it works well, no problems at all. But i had problems when i tested it on an old laptop with Win32 Vista installed. when i click and hold the title bar of the panel (to drag it), the panel crashes and closes. i can reproduce it, it happens every time.
    Can someone confirm this as a bug of the plug-In, or is it just our old laptop? Opening a dialog panel from another program and dragging it works without problems.
    the problem is, that we have to support older windows computers with win32 installed as well to run our software.
     
  29. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    Hey!

    Thanks a lot for reporting the issue.
    I think I found where the problem is. Can you email me (contact at pygmymonkey.com) so I can send you the new version? You can then test it and tell me if the problem is gone :)

    Have a great day.
     
  30. JayHuang

    JayHuang

    Joined:
    Jul 1, 2014
    Posts:
    4
    Question regarding license. Just to confirm that this extension requires one license per seat for developer. And, I can distribute my application without extra license. Correct?
     
  31. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    Hi!

    Yes no problem :)
    This is the standard Asset Store license (from what I know this is the only one available for everyone).
    - If only one developer is using the package, you just need one license and can then sell your game/software/application to millions
    - But if two developers are working with it, you need two licenses. Even if you don't sell anything

    Have a great day!
     
  32. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    NativeFileBrowser 1.0.5 is now available on the Asset Store!
    • NEW: Windows: Having multiple extensions is now displayed under a single dropdown (instead of multiples).
    I also forgot to post the 1.0.4 release notes, so here they are:
    • NEW: Added possibility to define the button name on Mac (not easily possible on Windows, sorry).
     
  33. Hertzole

    Hertzole

    Joined:
    Jul 27, 2013
    Posts:
    421
    Hello!

    That's a really lovely asset you've created, and I can't wait until I get my hands on it! But I would also like to ask, just to be really, really sure. I'm currently making a runtime level editor and I would like to ask for permission just to include support for your asset as an addon for my level editor? And with "addon", I don't mean completely including all your source code, I just mean to have a separate Unity package with my level editor that you can import that will hook into your asset and my asset. Basically just making a bridge! :p

    I would just like to be 100% sure if I could do this or not, just in case I ever sell it on the assets store. :)
     
  34. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    Hello and thanks a lot!

    As long as the source code is not included, you can add any support you want :)
    What I can recommend, is using custom defines (https://docs.unity3d.com/Manual/PlatformDependentCompilation.html)

    You can include all the code related to Native File Browser inside #If #else like this:
    #if NATIVE_FILE_BROWSER
    // Code using the Native File Browser API
    #endif

    And the only thing people would have to do, is add to the top of the file:
    #define NATIVE_FILE_BROWSER

    So you could include this last line at the top fo your file, comment it, and add a comment next to it saying "uncomment this to add support for Native File Browser (https://www.assetstore.unity3d.com/#!/content/68064)

    I will add the #define NATIVE_FILE_BROWSER line to my package for the future update, so you can safely use #if NATIVE_FILE_BROWSER inside your code, and it will be available only when people imported Native File Browser.

    Is that what you wanted?
     
  35. Hertzole

    Hertzole

    Joined:
    Jul 27, 2013
    Posts:
    421
    That is exactly what I wanted! Thank you! :D Now I just need to save up to buy the package itself :p
     
    PygmyMonkey and nahoy like this.
  36. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,936
    @PygmyMonkey

    Hi there,

    I'm using your FileBrowser Plugin and its great.

    However there is a problem, your plugin returns selected file path, how can i get file name without path, extension, file size, file type etc seperately?

    Thanks

     
  37. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    Hi!

    Well you can do everything you want with the filePath using the C# api.
    Just add "using System.IO" at the top of your file and:
    - Get the file name: Path.GetFileName(filePath)
    - Get the extension: Path.GetExtension(filePath)
    - Get logical file size: var length =newSystem.IO.FileInfo(path).Length;
    - About getting the file type, i'm not sure what you mean by that. Based on the extension you already know the file type right?
     
    jGate99 likes this.
  38. 21312313123321

    21312313123321

    Joined:
    Dec 5, 2014
    Posts:
    13
    I had to use that Dispatcher to execute in the correct thread, this is not really acceptable.

    The API you provided should had handled that out of the box by default to some extent, if it gives me a call back
    it should run on the thread that called it, no excuse!
     
    andyz likes this.
  39. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    Hey :)

    You are 20000% right and I was having the exact same though a few days ago.
    It should be there for the next update!

    PS: Aren't you having a hard time typing your username to login? lol
     
  40. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,936
    @PygmyMonkey

    Can you please provide a new plugin that support chaning standalone icon on runtime, bounce, and add notificaiton symbol in it? I'll be the first one to buy that plugin.

    Thanks
     
  41. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    Hey,

    Sorry but I don't really have time to do this :/
    I think it's pretty easy to change the icon at runtime, at least for OS X. For Windows I don't know.
    For Mac: The icon is located at: YourApp.app/Contents/Resources/PlayerIcon.icns
    At runtime, you can just access this file and update it, and it should update the icon. Maybe you'll need to restart the computer to have the icon updated, or maybe there's something else to do to "ping" the OS to refresh the icon, but I don't know this part.

    About adding icon numbers, well, just include that in the icon texture maybe when you update the icon?

    And about bounce, I don't think you can make icon bounce. I don't know a way to do that (and can't think it's possible) on any platform.
     
  42. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,251
    Seconded, please including thread handling in your windows code.
    Is the update coming?
     
  43. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    Thanks for your wait and the suggestion!
    I've just submitted an update to the Asset Store, so it should be here in a few days ;)
     
  44. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    NativeFileBrowser 1.0.6 is now available on the Asset Store!
    • NEW: Added #define NATIVE_FILE_BROWSER in case you need to use this with scripting define symbol,
    • NEW: A dispatcher is now automatically included and use to "push" results to the main thread.
     
  45. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I've just bought this, and all was looking good at first... but the first time I tried to Cancel an open-file dialog (on Mac, in the editor), it crashed Unity completely. :(

    Here's the top of the crash log, in case it helps:

    Anybody else experiencing crashes, or is it just me?

    EDIT: Just tried again with a fresh launch of Unity, and again crashed in exactly the same way, as soon as I hit Cancel. I guess it's back to non-native file browsing for me. :(
     
    Last edited: Apr 13, 2017
  46. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    OK, maybe not.

    It turns out I can avoid the crash by not doing anything with the filePath parameter (to the callback delegate) when canceled is true. I had been doing stuff like:

    Code (CSharp):
    1.             csvFilePath = filePath;
    2.             if (!string.IsNullOrEmpty(csvFilePath)) OpenLastFile();
    ...supposing that if the dialog were canceled, the filePath would be null or empty. Apparently it's neither of those, but instead is some random junk that causes C# to bring down the entire app when you try to access it.

    Simply adding "if (canceled) return;" to the top of my delegate avoids the problem. No more crash.

    However, @PygmyMonkey, I'd like to suggest for your next update that you should return null or empty string in this case. It's perfectly valid to say "always check canceled before filePath," but even so, failure to do so shouldn't crash the app.
     
  47. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Grr, maybe so after all.

    This time I got a crash when clicking OK.

    Again it looks very much like a bogus string (std::length_error: basic_string::_S_create), but since this was after clicking OK rather than Cancel, I don't see how it's my fault this time, or what I might do about it.

    My code in this case looks like this:
    Code (CSharp):
    1.         string dir;
    2.         try {
    3.             dir = System.IO.Path.GetDirectoryName(haulsFilePath);
    4.         } catch {
    5.             dir = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
    6.         }
    7.         FileBrowser.OpenFilePanel("Select a Hauls Profile File", dir, new string[]{"csv"}, null, (bool canceled, string filePath) => {
    8.             if (canceled) return;
    9.             haulsFilePath = filePath;
    10.             LoadTruckHauls(filePath);          
    11.         });
    Any ideas?
     
  48. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    No comments on the completely crashing the app? @PygmyMonkey? :(
     
  49. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    Hello,
    Super sorry for the time I took to answer, I was having some vacations!

    This is indeed super weird. Can you give me more details, like what version of OS X are you using?
    Also, can you give me the full path of the file you're trying to open ? Maybe it contains a specific character that breaks things, like %!#, i don't know :)

    I don't have any issue at all, even without using if (canceled) return;
    And clicking cancel should return the string "cancel" in the filePath, not empty or null :/

    Also, are you using the very latest version? 1.0.6?
    Because if not, it should fix this issue :)
     
  50. PygmyMonkey

    PygmyMonkey

    Joined:
    Jan 13, 2014
    Posts:
    306
    Hey @bobmarley!

    About this issue you had, a user told me via email he found a solution, so I'll post it here :)
    "It seems so far that checking the box (Edit -> "Project Settings" -> Player -> "Resolution and Presentation" -> "Visible in Background") fixed it for me."