Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

File Browser

Discussion in 'Immediate Mode GUI (IMGUI)' started by Daniel_Brauer, Apr 6, 2011.

  1. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,351
    I needed a file browser, so I made this today: check it out on the wiki.

    It uses an object to store state data, and a delegate for reporting results. It uses mostly built-in GUI controls so it's quite skinnable.
     
  2. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    Cool. Always good to see new stuff appear in the Wiki! :)
     
  3. reissgrant

    reissgrant

    Joined:
    Aug 20, 2009
    Posts:
    726
    Very cool! Nice work!
     
  4. Mr.badatscripting

    Mr.badatscripting

    Joined:
    Jul 1, 2010
    Posts:
    132
    i no this is a random question but do any of you know how to save a game?
     
  5. 3nd3r

    3nd3r

    Joined:
    Apr 10, 2011
    Posts:
    5
    Congratulations!! Excellent work!

    I'm having problems with GUISkin. Unity is unable to recognize the Custom Style "List Item ". Could you pass a package with GUISkin you've defined to make it work?
    I'd be very grateful.
    Greetings;)
     
  6. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,351
    Sure, here's the base skin with a working list item style. All I did was add the custom style, call it "List Item", add a blue background for OnNormal, and then copy the rest of the settings from the Label style.
     

    Attached Files:

  7. 3nd3r

    3nd3r

    Joined:
    Apr 10, 2011
    Posts:
    5
    What speed!! Thank you very much. When I can test it, I mention the result.
    Really, thanks.
     
  8. CHPedersen

    CHPedersen

    Joined:
    Mar 2, 2011
    Posts:
    63
    This is wonderful. :)

    Thanks a lot for sharing your contribution!
     
  9. 3nd3r

    3nd3r

    Joined:
    Apr 10, 2011
    Posts:
    5
    I'm still the same warning:
    "Unable to find style 'List Item' in skin 'GameSkin' (Layout | Repaint | mouseDown | Used | ignore | mouseUp)"
    Do you have to do something special to make it work?

    To prove I have a project with:
    - FileBrowser.cs
    - GUILayoutx.cs
    - TextFileFinder.cs
    - GUISkin with CustomStyle you've uploaded in a package.

    Thanks, I tried all my ideas and do not understand where it fails. The problem is that the warnings do to prevent it navigable.

    Thanks in advance, Greetings
     
    liju likes this.
  10. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,351
    You have to set GUI.skin at the beginning of your OnGUI() function. Otherwise it will use the built-in skin, which doesn't have a list item style.
     
  11. CHPedersen

    CHPedersen

    Joined:
    Mar 2, 2011
    Posts:
    63
    I had time at work to get back to this and test it properly today. I'm sorry to say I ran into a bit of trouble as well. Hopefully this is something local with me or my machine.

    I got it installed and set it properly with the skin and everything; no warnings. But I couldn't get it to navigate. It opens up a window with my files and directories in it, and I can click on an item and it will move a blue beam to indicate I have (single) clicked that particular item.

    But I can't get the double click callback to fire. I traced through the code and determined it had to do with the conditional in one of the overloaded SelectionList-methods in the class GUILayoutx. Specifically, it checks,

    Code (csharp):
    1. else if (hover  callback != null  Event.current.type == EventType.MouseUp  Event.current.clickCount == 2)
    I stuck all 4 values in a Debug.Log on top of the first if-sentence, and it turns out that on my computer at least, Event.current.type is never MouseUp at the same time as hover = true and the clickCount is 2. The closest I got was this line, copied from my Debug console:

    hover: True, callback: GUILayoutx+DoubleClickCallback, event: mouseDown, clickCount: 2

    There is but one instance of that combination; It marks the very beginning of the doubleclick, but, as said, there is no case where,

    hover: True, callback: GUILayoutx+DoubleClickCallback, event: mouseUp, clickCount: 2

    , which is what I would need to see for this SelectionList-overload to fire the provided callback. This was tested primarily to switch into a lower directory, but the same happens with files. I can't select them by doubleclicking.

    Any ideas? Am I doing something wrong or is this something with my machine, or do I need to change something in the code?
     
  12. CHPedersen

    CHPedersen

    Joined:
    Mar 2, 2011
    Posts:
    63
    Edit: I have fixed this situation by changing,

    Code (csharp):
    1.             if (hover  Event.current.type == EventType.MouseDown)
    2.             {
    3.                 selected = i;
    4.                 Event.current.Use();
    5.             }                
    6.             else if (hover  callback != null  Event.current.type == EventType.MouseUp  Event.current.clickCount == 2)
    7.             {
    8.                 callback(i);
    9.                 Event.current.Use();
    10.             }
    11.             else if (Event.current.type == EventType.repaint)
    12.             {
    13.                 elementStyle.Draw(elementRect, list[i], hover, false, i == selected, false);
    14.             }
    To,

    Code (csharp):
    1.             if (hover  Event.current.type == EventType.MouseDown  Event.current.clickCount == 1)
    2.             {
    3.                 selected = i;
    4.                 Event.current.Use();
    5.             }                
    6.             else if (hover  callback != null  Event.current.type == EventType.MouseDown  Event.current.clickCount == 2)
    7.             {
    8.                 callback(i);
    9.                 Event.current.Use();
    10.             }
    11.             else if (Event.current.type == EventType.repaint)
    12.             {
    13.                 elementStyle.Draw(elementRect, list[i], hover, false, i == selected, false);
    14.             }
    I can now still select things at normal, but the fix causes the doubleclickdelegate to be fired on MouseDown rather than up. I considered that a minor thing. :)
     
    Me_hungry and Meister-Eder like this.
  13. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,351
    I wonder if that's a Windows thing. I haven't tested that code on Windows yet.
     
  14. CHPedersen

    CHPedersen

    Joined:
    Mar 2, 2011
    Posts:
    63
    It very likely is. It would be interesting to hear from anyone else to see if they can reproduce the behavior on a different windows machine as well.
     
  15. Dover8

    Dover8

    Joined:
    Aug 20, 2010
    Posts:
    84
    Thanks for this fix, also worked for me on Windows where it wasn't before.
     
  16. deddytati

    deddytati

    Joined:
    Jul 22, 2011
    Posts:
    16
    say..
    is there any extension to this tutorial like for saving or loading file??
     
    Last edited: Jul 25, 2011
  17. deddytati

    deddytati

    Joined:
    Jul 22, 2011
    Posts:
    16
    anyone can make it run in webplayer??
     
    Last edited: Jul 25, 2011
  18. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,082
    No, there is no System.IO or local file access in the webplayer.

    --Eric
     
  19. pierresusset

    pierresusset

    Joined:
    Feb 7, 2009
    Posts:
    52
    Really nice contribution...
    I'll use it for my project and i think i will do a save File Dialog Box too...

    also, it's good to expose the file type filter (and maybe a multiple file type filter could be usefull) as a variable so we can make a generic openDialogBox.

    I'll post this two addition soon.
     
  20. jasonbelmonti

    jasonbelmonti

    Joined:
    Nov 1, 2011
    Posts:
    2
    This file browser is totally rad. But does it work on iPad?
     
  21. bkazour

    bkazour

    Joined:
    Dec 7, 2011
    Posts:
    98
    Hi
    I had this working on Unity 3.4, but now i have updated it to unity 3.5, and it is failing.
    It seems it is unable to find "List Item".
    I have reImported the skin but it didn't work.

    Anyone have any idea why this might have happened ?
     
  22. Rolands

    Rolands

    Joined:
    Aug 15, 2010
    Posts:
    90
    Can someone help me geting name of file selected files to string variable?
     
  23. PST

    PST

    Joined:
    May 21, 2012
    Posts:
    3
    for some reason i am not able open a directory and view the files in it. I checked the code, but couldn't find the answer
     
  24. PST

    PST

    Joined:
    May 21, 2012
    Posts:
    3
    Never mind fixed the problem. Thank you for posting the code. Also is there anyway to get .NET openfiledialog in Unity. I tried to work with that in the the begin. But, I was getting error: system.windows.forms not in Assembly. But, I added the system.windows.forms to the assembly and it still says that weird.

    I think unity should give an easy way to do this especially in javascript.
     
  25. jack_rabbit

    jack_rabbit

    Joined:
    Jun 12, 2012
    Posts:
    4
    I am running into a bit of trouble with the "TextFileFinder" example.
    When I attach it to the GameObject the options the Inspector gives me for the script are "Directory Image" and "File Image". When I attach a different GUI script example which I found somewhere else, it gives me he option "Skin With List Style". For the 2nd option I can attach the GUISkin to it and all works smoothly, but for TextFileFinder I get the "List Item"-problem, since I have no way of attaching the GUISkin.

    Does anyone know why this is happening and how I can get rid of it? Thanks!
     
  26. Neroshiny

    Neroshiny

    Joined:
    Jul 3, 2012
    Posts:
    7
    how I can start the file browser from the desktop?

    sorry for my bad english :S
     
  27. Timurov

    Timurov

    Joined:
    Oct 15, 2012
    Posts:
    5
    I've used the Improved File Browser from wiki but when I clicking on a folder there is no action for opening a folder, but for single file there is no problem, Is there any solution to solve this small bug?
     
    Last edited: Nov 29, 2012
  28. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,082
    If you want a supported file browser with a lot more features and thorough documentation, I'd recommend UniFileBrowser.

    --Eric
     
  29. Kehos

    Kehos

    Joined:
    Jul 26, 2012
    Posts:
    4
    I have to say, really thanks to you for this, I tried it and it works perfectly!
     
  30. fil

    fil

    Joined:
    Jul 17, 2012
    Posts:
    11
    could you explain how you did that? thanks

    *edit:
    I thing I found how to do this...

    in TextFileFinder.cs:

    public class TextFileFinder : MonoBehaviour {

    public GUISkin Skin; // added this

    .....

    protected void OnGUI () {
    GUI.skin = Skin; // added this
    ...

    then I attached GUISkin file to script


    ----------------

    Now I need to know how to change the folder to open when pressed the button.

    edit2: found it: FileBrowser.cs - line 125

    SetNewDirectory(Directory.GetCurrentDirectory());

    just changed to SetNewDirectory( Application.DataPath + "/folder");
     
    Last edited: Dec 27, 2012
  31. Foley67

    Foley67

    Joined:
    Mar 1, 2013
    Posts:
    1
    Hey, I seem to almost be there with this. I can select files, and move between folders, but it seems like there's no skin being applied. The font is pretty bad looking and mostly coloured red. I have added the two lines shown by the last post and dragged my skin onto the script, as well as added some images to the directory image and file image section of the script options, but they don't show either.
    Any idea on what the problem could be?
    Thanks

    Edit: Sort of solved... Turns out that I had to drag the GUISkin onto the script options after it had been applied to an object rather than dragging it onto the options on the script itself. It now looks better but only works with the skin supplied by the original poster and not any other custom skins, even when a custom style "List Item" is added. I guess I can mange for now
     
    Last edited: Mar 8, 2013
  32. entity476

    entity476

    Joined:
    Nov 2, 2012
    Posts:
    40
    I'm currently looking into it, but does anyone have a short answer of why the double-clicking doesn't function with Unity 4? It was absolutely working with Unity 3.5. I'm also asking because i'm not familiar with most of the code.

    EDIT: I noticed that when the Event.current.clickCount (in GUILayoutx.cs) is set to 1 (instead of 2) the selected file or directory is picked, which indicates that before the second click the if statement controlling the double-click is called again while the "callback" variable is null anymore. Or maybe something similar. Any guess?

    EDIT: I think that's the problem: "Mac OS X: Event.clickCount behavior for double-clicks now matches windows (clickCount is always 1 for MouseUp events)." taken from here. I'll come back when I get the answer.

    SOLUTION: Ok, people, Pablo Bollansée had posted already the answer. My fault I didn't check for updates….
     
    Last edited: Mar 22, 2013
  33. Sija

    Sija

    Joined:
    Sep 22, 2012
    Posts:
    15
  34. GrannySmith

    GrannySmith

    Joined:
    Jul 20, 2012
    Posts:
    8
    Hey guys. Unfortunately this still doesn't work for me. Does anyone of you know how to solve the problem? I can't navigate in the folders and such but I am able to choose a file if it is the correct fileformat. I am on a Windows 7 machine.

    thanks in advance
     
  35. CodeAssembler

    CodeAssembler

    Joined:
    Oct 15, 2011
    Posts:
    33

    This worked for me but the last line -> SetNewDirectory( Application.DataPath + "/folder"); Is not well explained ?

    also it should read instead -> SetNewDirectory( Application.dataPath + "/folder");

    but what would actually replace the /folder parameter here? Thanks!
     
  36. Ricardo Reis

    Ricardo Reis

    Joined:
    Sep 15, 2013
    Posts:
    1
    Hi!

    Newcommer to Unity, using this FileBrowser on Windows7-64,
    Version 4.2.1f4 (4d30acc925c2)
    Tue, 27 Aug 2013 08:35:08 GMT
    With MonoDevelop version 2.8.2.

    This browser always breaks on
    You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
    UnityEngine.MonoBehaviour:.ctor()
    FileBrowser:.ctor(Rect, String, FinishedCallback)
    TextFileFinder:OnGUIMain() (at Assets/Scripts/TextFileFinder.cs:33)
    TextFileFinder:OnGUI() (at Assets/Scripts/TextFileFinder.cs:21),


    referring to this line of code:
    m_fileBrowser = new FileBrowser (

    All my attempts at this resulted in an error. Apparently, it's not supposed to work, because MonoBehaviour does not accept the new constructor. Anyone with a workaround for this?
     
  37. winterkewl

    winterkewl

    Joined:
    Oct 28, 2011
    Posts:
    53
    Generally, you want to attach a monobehavior script to an object in your scene.
    You can do this by creating a prefab that already has that component attached to it and then instantiating that or by using the AddComponent<FileBrowser>() command.
     
  38. akkiDev

    akkiDev

    Joined:
    Aug 7, 2013
    Posts:
    70
    hi CHPedersen ,

    i am using this file browser, but facing a problem, the browser opens the current project directory, and i am unable to navigate to other directories , i am using windows, really very thanx for any help..
     
  39. him00105

    him00105

    Joined:
    Sep 1, 2013
    Posts:
    4
    in android , it gives its current installed directory... how can i navigate to its files folder... so can i choose any particular file...
     
  40. GregMeach

    GregMeach

    Joined:
    Dec 5, 2012
    Posts:
    249
    WARNING: necro-thread!

    I spent a few hours cleaning this up with the following purpose: To use it as a "pick saved file" dialog box so you can disable directory browsing and easily adjust to suit your GUISkin. I also revised the MouseUp->MouseDown events to fix the double click problem.

    Edit: I fixed the listing of files when using a mask (*.xml). For whatever reason I was seeing all files even with a mask.

    I tested the package a couple of times but do let me know if you run into any problems.
    View attachment $SavedFileBrowser_v0.1.zip
     
    Last edited: Feb 18, 2014
  41. ankurpatel

    ankurpatel

    Joined:
    Jul 2, 2013
    Posts:
    5
    will it work in Android?
     
  42. Destidom

    Destidom

    Joined:
    Apr 10, 2014
    Posts:
    1
    I don't know if it was your cleaning up of the FileBrowser.cs file or if the problem is in the original FileBrowser.cs, but when I tried to access the C:\ folder on a windows computer i got a null pointer exception and the C:\ folder refused to load.

    Added this little fix and it would work again, so if anyone dowloads the .zip file and run into the same problem as i did.


    insert this under protected void ReadDirectoryContents()

    Code (csharp):
    1.   protected void ReadDirectoryContents() {
    2.         if (m_currentDirectory == "/") {
    3.             m_currentDirectoryParts = new string[] {""};
    4.             m_currentDirectoryMatches = false;
    5.         } else {
    6.             m_currentDirectoryParts = m_currentDirectory.Split(Path.DirectorySeparatorChar);
    7.             Debug.Log("GetDirectoryName returns: " + Path.GetDirectoryName(m_currentDirectory) );
    8.             if (SelectionPattern != null) {
    9.                 string path = m_currentDirectory;
    10.                 if(path != "C:\\")
    11.                 {  
    12.                                         // If Path.GetDirectoryName gets a C:\ it returns a null??
    13.                     path = Path.GetDirectoryName(m_currentDirectory);
    14.                 }
    15.  
    16.                 string[] generation = Directory.GetDirectories(
    17.                     path,
    18.                     SelectionPattern
    19.                 );
    20.                 m_currentDirectoryMatches = Array.IndexOf(generation, m_currentDirectory) >= 0;
    21.             } else {
    22.                     m_currentDirectoryMatches = false;
    23.             }
    24.         }
    25.  
    26.                // .... rest of the code from source below

    Hopefully this will help some poeple out there.

    P.S
    For everyone asking if it works on android, yes it does! :D

    - Destidom
     
    Last edited: Apr 22, 2014
  43. CarterG81

    CarterG81

    Joined:
    Jul 25, 2013
    Posts:
    1,704

    Thank you.

    Someone needs to add this to the wiki.

    Was required for it to work for me.

    http://wiki.unity3d.com/index.php?title=ImprovedFileBrowser
     
  44. hadicoco

    hadicoco

    Joined:
    May 10, 2014
    Posts:
    13
    Thanks, looks interesting - hope you keep supporting it when the new Unity GUI comes out
     
  45. TechSupportIncoming1

    TechSupportIncoming1

    Joined:
    Mar 13, 2014
    Posts:
    4
    Splendid! I really needed this! Thanks for sharing it with us. (=
     
  46. muzhiguinie

    muzhiguinie

    Joined:
    Nov 7, 2014
    Posts:
    3
    Hi Everyone. First Thank you very much for all your excellent implementation. this thread code POST help me a lot!
    But now I have one problem with my FileBrowser. I use a open button on my Unity UI to invoke/open this FileBrowser. And there are a lot of buttons on the UI in the meantime. When I click a list item within the FileBrowser, I can also invoke/click the botton beneath the list item....I want the user can only manipulate the FileBrowser list and disable/inactive all the items under it. I tried to use GUI.depth setting and set the beneath buttons to another layer dynamically. But they all do not work .
    Does there anyone knows how to solve this problem. or is there any method to adjust the GUILayout depth/layer in the FileBrowser.cs file ?

    Thank you everyone in advance!
     
  47. liju

    liju

    Joined:
    Apr 4, 2014
    Posts:
    6
    Error In Game
    ArgumentNullException: Argument cannot be null.

    It cannot open a folder or directory


    Can you upload sample project
     
    Last edited: Jan 2, 2015
  48. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    1,758
    I also need file upload dialog box from within untiy standalone build to user let upload and use local hard drive's file.

    Can you upload working unity project files about this?
     
  49. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,560
    I was tinkering around with the package uploaded by @GregMeach and I also added the fix by @Destidom and here is the package (which worked fine for me on android) I used in Unity 5.3.4f1:

    https://www.dropbox.com/s/v7cc4uuv88t929u/SavedFileBrowser.unitypackage

    Cool share guys, so I re-up'd it. Could use being swapped over to new gui, but it shows how to do this pretty well anyway...
     
  50. agonzamart

    agonzamart

    Joined:
    Nov 14, 2014
    Posts:
    1
    Could you share the package again? the dropbox link is down. Thank you