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

UniFileBrowser - runtime file browsing

Discussion in 'Assets and Asset Store' started by Eric5h5, May 4, 2013.

  1. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Version 2.3 is out now! (On my site; will upload to the asset store shortly.)

    UniFileBrowser 2.3

    Additions:
    • DefaultFileName variable, so you can optionally have a file name automatically supplied when saving.
    • MaxFileNameLength variable, to limit the max characters entered when saving a file.
    • ShowDate variable, which toggles whether file modification dates are shown.
    • SortType variable, for sorting the file list by name or date (which can be newest first or oldest first).
    • VolumesAreSeparate variable, for displaying volumes in a separate box from the rest of the file list, in the case where ShowVolumes is used.
    • SetCustomFunction function, which enables you to run custom GUI code in the file window from your own script.
    • RemoveCustomFunction function, if you want the custom function to stop running.
    • SetAutoAddedExtension function, in case you want to change the auto-added file extension at runtime.
    • RefreshFileList function, for updating the folder/file list on demand from your own scripts.
    • UseFolderFilterFunction function, for running a delegate that you can use to determine if a folder should be included or not. For example, you could set it up so only folders that contain .jpg files are shown.
    • DontUseFolderFilterFunction function, for turning the custom folder filter off.
    • UseFileFilterFunction function, for using a delegate to make customized file filters in those cases where filtering by extension isn't enough.
    • DontUseFileFilterFunction function, for turning the custom file filter off.

    Changes:
    • If an extension is auto-added, users are prevented from changing it when entering a file name.
    • The file name textfield isn't displayed anymore when loading files...even disabled, there wasn't really much point having it there.

    Fixes:
    • Message window text positioning uses the file window inset rather than being hard-coded to 10 pixels from the top.
    • Hitting escape to close the file window closes it properly.
    • Filter button position is based on the window tab offset instead of being hard-coded.

    --Eric
     
  2. javdog

    javdog

    Joined:
    Dec 29, 2013
    Posts:
    7
    I wanted to say Thank You for this, I picked it up yesterday and it's just what I need. I'm new to scripts so please bear with me. I was hoping you could point me to the line in the script where the file path that was selected by the user, could be sent to fill in a field in another script (on the same camera object) that would do the loading? I'm trying to make it so the user doesn't have to type the path, merely find it with the file browser and that will populate the field in the other script. (I could email the script that I wanted populated if that wld help)

    Also, I wanted to just trigger the "Open File" browser when a 3d text is mouse clicked. I studied the demo, but that is using the Unity gui, so I wasn't quite able to reverse engineer a JS solution that I have for the 3D text trigger. I just don't know code well enough.

    Any help would be greatly appreciated.

    figured I'd add the short script I'm using attached to the 3Dtext trigger.
     
    Last edited: Apr 9, 2014
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Please use code tags when posting code. For opening the browser, all you need is the "UniFileBrowser.use.OpenFileWindow (NameOfFunction);" line of code, which you can put anywhere, probably OnMouseDown in your case. "NameOfFunction" should be replaced by the actual name of the function that you will use for the file path. In the example script, the function is called "OpenFile". The function receives the file path string.

    --Eric
     
  4. unclebob301

    unclebob301

    Joined:
    Jan 29, 2014
    Posts:
    20
    Hi Eric

    I am looking for a file browser.

    Just read the above and although I have not used yours yet but could javdog not simply hardcode his path as a variable for now?

    Perhaps thats what he means as simple?

    I am not sure if each of his objects would have a theme but if they did then using send message on mouse down with a variable, unique to that object - holding the path to the selection of textures that would be appropriate?

    Eg: Cube buildings, (buildings textures saved in c:\buildings)
    Mouse click Cube variable set to c:\buildings
    Your function called and the file browser opened on c:\buildings

    Cheers
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The issue turned out to be a syntax error in his code, so I think that's all set. UniFileBrowser has an option to specify a path, and there's a "limit to initial folder" option which prevents navigating to other folders, so yes, it's pretty simple to do what you describe.

    --Eric
     
  6. kurtdog

    kurtdog

    Joined:
    Mar 31, 2014
    Posts:
    14
    Hey Eric,

    I'm having a lot of trouble getting unifilebrowser to work on mobile. So far it works perfectly for PC, but when I try mobile I can't get the filebrowser window to open. I've been trying a lot of different things with SetPath, such as: UniFileBrowser.use.SetPath(Application.dataPath + "/../"). But can't get it to work. Can you point me in the right direction?

    Thanks,
    Kurt
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Mobile has restrictions on what directories you can use. Try Application.persistentDataPath.

    --Eric
     
  8. kurtdog

    kurtdog

    Joined:
    Mar 31, 2014
    Posts:
    14
    Hey Eric,

    Thanks for the quick response. I've tried Application.persistentDataPath as well and it still isn't working. But I do have a little more insight into the problem. It seems unifilebrowser isn't working on my windows build either. So it might not be something specific to mobile platforms. When I run my game within the Unity editor, everything works perfectly and the unifilebrowser window opens. But when I build my game, unifilebrowser doesn't open. Ideas?

    Thanks again,
    Kurt
     
  9. kurtdog

    kurtdog

    Joined:
    Mar 31, 2014
    Posts:
    14
    Nevermind,

    I fixed the issue. I was trying to call Unifilebrowser.use.OpenFileWindow(OpenFile) from within the Start() method. Which apparently doesn't work haha. After looking back at your code I saw you called it from OnGUI. I did the same and now have it working.

    -Kurt
     
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Hmm, it should work in Start. I tried that here and it does, in a build as well as the editor. But I guess it couldn't hurt to wait a frame before calling OpenFileWindow.

    --Eric
     
  11. kurtdog

    kurtdog

    Joined:
    Mar 31, 2014
    Posts:
    14
    Interesting.

    Ok I'll keep messing with it, but that's what fixed it for me. I've also been customizing the gui for my game, and for mobile. I've looked over old posts in the thread and have been changing variables in the UniFileBrowserGUISkin to get what I want. I have almost everything figured out, but I want to change the size of the folder selecter box, the toggle box at the top left that selects which folder to look in. I changed the font, but now the current folder appears outside of the box. You should be able to see what I mean from the attached pic. $box.png

    P.S. You're awesome for supporting your product so well
    -Kurt
     
  12. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    If you change the height of the public Popup Rect variable (on the UniFileBrowser script), that should work.

    --Eric
     
  13. jawwinker

    jawwinker

    Joined:
    Apr 26, 2014
    Posts:
    2
    Hello,

    So I have been trying to integrate UniFileBrowser into my project where I want to store an array of 4 different paths to folders. I created a static tempPath variable as well as imagePaths String Array to populate. I can access the tempPath variable data in the OpenFolder function, however when I try and access it in the OnGui it's giving me the error...

    UnityEngine.MonoBehaviour:print(Object)
    UniFileBrowserOpenFolderExample:OnGUI() (at Assets/UniFileBrowser Assets/UniFileBrowserOpenFolderExample.js:28)

    This is probably something simple I am missing, but it has been stopping me for hours now. If anyone has any ideas on what I am doing wrong please let me know.

    Thanks,

    Steve


    Code (csharp):
    1. private var pathChar = "/"[0];
    2. static var tempPath : String = "";
    3. static var imagePaths : String[];
    4.  
    5. function Start () {
    6.     if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer) {
    7.         pathChar = "\\"[0];
    8.     }
    9.    
    10.     // IMAGE PATHS
    11.     imagePaths = new String[4];
    12.     //front
    13.     imagePaths[0] = "No Path Currently Set";
    14.     //right
    15.     imagePaths[1] = "No Path Currently Set";
    16.     //back
    17.     imagePaths[2] = "No Path Currently Set";
    18.     //left
    19.     imagePaths[3] = "No Path Currently Set";
    20. }
    21.  
    22. function OnGUI () {
    23.     if (GUI.Button (Rect(100, 200, 95, 35), "Open Folder")) {
    24.         UniFileBrowser.use.OpenFolderWindow (true, OpenFolder);
    25.         print(tempPath);
    26.         imagePaths[0] = tempPath;
    27.         print(imagePaths[0]);
    28.     }
    29. }
    30.  
    31. function OpenFolder (pathToFolder : String) {
    32.     print(pathToFolder);
    33.     tempPath = pathToFolder;
    34.     print(tempPath);
    35. }
     
  14. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    When you call GUI.Button, all the code executes immediately. But you won't have the user-selected path until much later (relatively speaking), when the user eventually selects a path and the OpenFolder function runs. So the only code that you should have with the GUI.Button is the OpenFolderWindow line; none of the other stuff can work since the data is simply not available at that time.

    --Eric
     
  15. jawwinker

    jawwinker

    Joined:
    Apr 26, 2014
    Posts:
    2
    Oh, I get it! Thank you soooooo much!
     
  16. Evil-Dog

    Evil-Dog

    Joined:
    Oct 4, 2011
    Posts:
    134
    It seems to me that when a folder doesn't exist, it shouldn't go in it, say it doesn't exist and leave you with no file, no folder selection, stuck in a dead end. I get this with my D and E drives.
    It seems like the code should be like this so that it tells you it doesn't exist and remains where it was before you attempted to go in the non existing folder.

    Code (csharp):
    1.  
    2. // If user opened a folder, change directories
    3.     var prevPath:String = filePath;
    4.     if (selectedFileNumber >= 0  selectedFileNumber < dirList.Count) {
    5.         filePath += dirList[selectedFileNumber].name + pathChar;
    6.         if (showVolumes  selectedFileNumber < numberOfVolumes) {
    7.             if (windowsSystem) {
    8.                 filePath = dirList[selectedFileNumber].name + pathChar + pathChar;
    9.             }
    10.             else if (linuxSystem) {
    11.                 filePath = dirList[selectedFileNumber].name + pathChar;
    12.             }
    13.             else {
    14.                 filePath = "/Volumes/" + dirList[selectedFileNumber].name + pathChar;
    15.             }
    16.         }
    17.         var good:boolean = GetCurrentFileInfo();
    18.         if(!good)
    19.         {
    20.             filePath = prevPath;
    21.             GetCurrentFileInfo();
    22.         }
    23.         return;
    24.     }
    25.  
     
  17. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I could do something like that, though I'm curious as to how you get a non-existing folder in the first place?

    --Eric
     
  18. Evil-Dog

    Evil-Dog

    Joined:
    Oct 4, 2011
    Posts:
    134
    My D and E drives show up in the list when volumes are enabled, D is my empty DVD drive and the E drive is...heck if I know haha but it's there and it's a dead-end when I check it since the folder selection dropdown goes away and there's no file to browse so you have to close the file browser and reopen it.
    I say folder but I'm really talking about drives here, obviously non existing folders are....well...non existing :)

    That part of the code hits.
    Code (csharp):
    1.  
    2. var info = new DirectoryInfo(filePath);
    3.     if (!info.Exists) {
    4.         HandleError ("The directory \"" + filePath + "\" does not exist");
    5.         return false;
    6.     }
    7.  
     
    Last edited: May 2, 2014
  19. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    OK, thanks for the info!

    --Eric
     
  20. Evil-Dog

    Evil-Dog

    Joined:
    Oct 4, 2011
    Posts:
    134
    No problem
    I was wondering, am I experiencing a bug or is it by design that the folder dropdown works only by holding the mouse button down, so you can't click on it to open the dropdown and then click on the folder you wanna go back up to? Am I being clear?
     
  21. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's by design.

    --Eric
     
  22. Evil-Dog

    Evil-Dog

    Joined:
    Oct 4, 2011
    Posts:
    134
    Would be cool if it was an option to have it work by click. Having to hold is a bit unintuitive, for me at least. Seemed like a bug at first.
    Thanks for the answers Eric
    Cheers
     
  23. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,459
    +1 for click to open. There are many drop down that work in this way. An option for both seems reasonable.
     
  24. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    972
    How would you reuse the previous file selection?
    Say I have opened a file with UFB.
    Then I like to save it to the same location.
    How to make the filename reappear in the save window without making the selection again?
     
  25. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Once the user has selected a file, you can extract the path using GetDirectoryName to use with the UniFileBrowser SetPath function, and GetFileNameWithoutExtension to extract the file name to use with the default file name. i.e., UniFileBrowser.use.defaultFileName = Path.GetFileNameWithoutExtension (pathToFile).

    --Eric
     
  26. kurtdog

    kurtdog

    Joined:
    Mar 31, 2014
    Posts:
    14
    Hey Eric,

    I recently ran into something with the browser. It used to work fine in both the Unity editor and in my Android build, but it no longer works in the Android build and I have on idea why. I'm getting the file browser to show up. But for some reason I can no longer open folders. When I try to open a folder, I get a little window that pops up and says "error". Any ideas as to what may be causing this? sdcard permissions maybe? Again it works on my computer in the editor, but not in my android build.

    Thanks,
    Kurt
     
  27. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Someone sent me a PM recently:

    --Eric
     
  28. kurtdog

    kurtdog

    Joined:
    Mar 31, 2014
    Posts:
    14
    Thanks!

    Once again you've saved me Eric, that was it.

    -Kurt
     
  29. Qwolf

    Qwolf

    Joined:
    Feb 1, 2014
    Posts:
    28
    I have an issue where whenever I have some sort of input, mouse/key down/up, it gives me this message in the console as an error : !dest.m_MultiFrameGUIState.m_NamedKeyControlList

    Any idea what it is?
     
  30. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That's a Unity bug which is fixed in 4.5.

    --Eric
     
  31. Qwolf

    Qwolf

    Joined:
    Feb 1, 2014
    Posts:
    28
    I am using unity 4.5.1f3 though :/
     
  32. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Are you sure? ;) That happened in a few earlier versions such as Unity 4.1-4.3, and definitely doesn't happen anymore in 4.5 here since the bug that caused seems to have been fixed. Try reimporting, and try the demo scene to rule out the possibility that it's some other code interfering.

    --Eric
     
  33. Qwolf

    Qwolf

    Joined:
    Feb 1, 2014
    Posts:
    28
    It appears to happen in ALL scenes when ever you hit save.


    Untitled.png

    It will only do this AFTER you have opened a file browser window then ended the preview. When you save, it will show that message. Re Importing did nothing :/

    The screenshot is in the demo scene after i started, used the browser, then saved the scene. It is really weird.
     
  34. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You're right, that bug still happens when you save. It used to always happen after play mode regardless of saving or not, so I guess it's an improvement, sort of. :/ I checked the bug report and it's still listed as open. Note that the code in the docs for SetNextControlName shows the same behavior...maybe it will get actually fixed someday.... In the meantime, you can prevent the bug from happening just by never saving. ;)

    --Eric
     
    Qwolf likes this.
  35. Qwolf

    Qwolf

    Joined:
    Feb 1, 2014
    Posts:
    28

    It doesn't affect anything. Just REALLY annoying.

    Is having to hold down the mouse intentional for that drop-down in the browser? I honestly think you should just click and it stays open until you click somewhere else.
     
  36. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's intentional. I spent quite some time attempting to do the click-and-stay-open thing, but kept having random focus issues where the list would sometimes not highlight on mouseover. I eventually had to give up, which isn't something I do easily...either I was somehow relying on undefined behavior, or there's some problem with OnGUI. Bugs that are random are the absolute worst.... In any case, I'll give it another go if it makes sense to convert UFB to the upcoming new GUI.

    In the meantime, there's a new version out now on my site; should be available soon on the asset store:

    UniFileBrowser 2.4

    Additions:
    • DateWidthAdd variable, to manually add width to the date field (if used), in case the auto width computation is not accurate enough.

    Fixes:
    • Empty volumes are not included in the list.
    • Volumes work on Android.
    • When using OpenFolderWindow, limitToInitialFolder is forced to false, since it doesn't make any logical sense for it to be true in that case.

    --Eric
     
    Qwolf likes this.
  37. Qwolf

    Qwolf

    Joined:
    Feb 1, 2014
    Posts:
    28
    Ok. Thanks for the clarification.
     
  38. unclebob301

    unclebob301

    Joined:
    Jan 29, 2014
    Posts:
    20
    Hi Eric

    can you advise please? I have not downloaded your software yet but from reading this thread and your site would I be correct in saying that prior to displaying through onGui your software stores the file list and folder name as strings?

    If so are these exposed so that further work can be done with them?

    I am thinking about buying your work but presenting the data in a different way than onGui, do you think that is possible?

    Cheers
     
  39. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The file/folder lists are stored as strings, correct, although they aren't publicly exposed. With a little work you could change the source code to make them public if you want.

    --Eric
     
  40. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    New version:

    UniFileBrowser 2.4.1

    Fixes:
    • Reverted fix in 2.4 about empty volumes not being included in the list, since that was causing response delays for some people.
    • Empty volumes don't cause an error any more, so the file browser can continue to be used normally; instead there are simply no folders or files listed.
    • Fixed volume selection on Windows so "\" doesn't show up in the popup list followed by the volume; instead only the volume is listed.

    --Eric
     
  41. unclebob301

    unclebob301

    Joined:
    Jan 29, 2014
    Posts:
    20
    Thanks mate for getting back to me. I am guessing but are you dumping the strings into an array before displaying? Its changing the way that things are displayed that is important to me. Is passing the selected file a simple matter of storing the path and name as a string and then passing this to another function?

    Cheers
     
  42. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yep.

    --Eric
     
  43. ZeroSumGames

    ZeroSumGames

    Joined:
    May 7, 2013
    Posts:
    27
    When I put a filter for .jpg and .png on, and then I browse to certain folders, I'm getting a series of crash messages

    "InvalidOperationException: Operation is not valid due to the current state of the object
    System.Collections.Stack.Peek() at blah blah system.colelctions/Stack.cs:321

    Many folders work just fine. Can't figure out why some folders break it.

    Ideas?
     
  44. ZeroSumGames

    ZeroSumGames

    Joined:
    May 7, 2013
    Posts:
    27
    Was able to fix it by adding .PNG and .JPG to file extensions. Also removed some line in the inspector (auto add extension) where it just said jpg. Not sure what the deal is there but it works now.
     
  45. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That's strange, seems like some kind of internal Mono error? I'm not able to reproduce that; if I only use ".jpg" then it just does't list any ".JPG" files, as expected. The auto added extension is for saving files.

    --Eric
     
  46. Qwolf

    Qwolf

    Joined:
    Feb 1, 2014
    Posts:
    28
    While using the file browser with C#, this warning pops up: "Error CS0012: The type 'Boo.Lang.ICallable' is defined in an assembly that is not referenced. You must add a reference to assembly 'Boo.Lang, Version=2.0.9.5, Culture=neutral, PublicKeyToken=32c39770e9a21a67'. (CS0012) (Assembly-CSharp)" Any fixes?
     
  47. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I'm not seeing that...if I use the C# demo script in the demo scene, for example, there are no errors.

    --Eric
     
  48. Qwolf

    Qwolf

    Joined:
    Feb 1, 2014
    Posts:
    28
    I think this may be one of the reasons... Maybe This. How would you go about fixing this? Then again. I also have a function that doesn't use a coroutine and still returns the error. error.PNG error2.PNG
     
  49. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    If that's just in MonoDevelop then you can ignore it. The only errors that matter are what's printed in the Unity console.

    --Eric
     
  50. Qwolf

    Qwolf

    Joined:
    Feb 1, 2014
    Posts:
    28
    Is there a way of hiding specific errors?