Search Unity

Simple File Browser [Open Source]

Discussion in 'Assets and Asset Store' started by yasirkula, Nov 19, 2016.

  1. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    Code (CSharp):
    1. FileBrowser.SetFilters( false, ".png" );
    2. FileBrowser.SetDefaultFilter( ".png" );
    3. FileBrowser.ShowLoadDialog( ( path ) => { Debug.Log( path ); }, null );
     
  2. said123sawn

    said123sawn

    Joined:
    Oct 31, 2019
    Posts:
    26
    how do I access the name of the selected file
     
  3. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    Please see:

     
  4. Doigyfu

    Doigyfu

    Joined:
    Nov 21, 2019
    Posts:
    3
    Hey! First of all, i'd like to say thank you for releasing this awesome asset for free. Can you tell me how do I "freeze" the script execution while the user selects a file? Also, how do I set the file format for saving a new file. Last one: when using the coroutine approach, FileBrowser.Result still holds the result outside the coroutine?
     
  5. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    You can't freeze script execution because the file browser runs on the same thread that all the other scripts are running, as well. The best workaround would be to set Time.timeScale to 0 to "pause" the game. You can call the FileBrowser.SetFilters function to specify an extension (see example code). And you are right, FileBrowser.Result's value will stay intact until another file browser is shown.
     
  6. samson2020

    samson2020

    Joined:
    Dec 16, 2019
    Posts:
    8
    Thanks mehn. You're a genius.
     
    yasirkula likes this.
  7. bimarck123

    bimarck123

    Joined:
    Jan 21, 2020
    Posts:
    1
    Hi.
    I'm trying to use your plugin in my unity project.
    And it works well in PC.
    I want to use it on mobile(android/ios) but it's not working well.
    Please help me.
    :)
     
  8. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    What is the issue you are having?
     
  9. ryancrooger

    ryancrooger

    Joined:
    Jan 29, 2020
    Posts:
    1
    I'm trying to receive the callback of the selected filepath. Unfortunately, It is not giving any callbacks at all.

    Following is my code,

    Code (CSharp):
    1. public class FileChooser : MonoBehaviour
    2. {
    3.    
    4.     void Start()
    5.     {
    6.         FileBrowser.SetFilters( false, ".jpg" );
    7.         FileBrowser.SetDefaultFilter( ".jpg" );
    8.         FileBrowser.ShowLoadDialog( ( path ) => {CodelabUtils._ShowAndroidToastMessage("showig"); }, null );
    9.     }
    10.    
    11. }
     
  10. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    The callback runs after user clicks the Select button and passes the selected file's path to the callback. So the callback doesn't run immediately after calling ShowLoadDialog. If the callback isn't called even after clicking the Select button, please check logcat (Android), Xcode console (iOS) or Player.log file (Standalone) for any meaningful error messages. Alternatively, you can use a runtime debug console like this to see error messages directly in-game.
     
  11. skoteskote

    skoteskote

    Joined:
    Feb 15, 2017
    Posts:
    87
    Trying it out on iOS now and it seems to work except I can't see any files except Unity related stuff. What am I missing?
     
  12. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    iOS doesn't allow accessing most of the directories. Thus, only persistentDataPath, temporaryCachePath and streamingAssetsPath should be accessed on iOS.
     
  13. skoteskote

    skoteskote

    Joined:
    Feb 15, 2017
    Posts:
    87
    Last edited: Feb 7, 2020
  14. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    NativeGallery has LoadImageAtPath function for creating Texture2D from a local image. You can use local videos directly with VideoPlayer component. To create AudioClip from a local SFX, you'll have to google "unity runtime audioclip".
     
    skoteskote likes this.
  15. Zapan15

    Zapan15

    Joined:
    Apr 11, 2011
    Posts:
    186
    Very nice plugin! Could you add a multi selection feature (maybe with checkboxes or the like)? That would be awesome!
     
  16. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
  17. Nachtmarv

    Nachtmarv

    Joined:
    Jul 28, 2018
    Posts:
    1
    So, for my thesis I'm working on a VR project and I needed a file browser. I took yours, made some adjustments, and it works :)
    I still need to replace all text with TMP, but at least I have the funtionality, thanks for that.

    I have one problem though: when using the scrollbars, they jump around for a hot second before moving smoothly. Any immediate idea what could be the cause of this? probably some interaction between XR raycaster and some canvas, so certainly not your problem, but maybe you have an Idea.

    Another thing: I would like to add this asset to the github of my project, since I had to modify some of the prefabs and scripts. I'm not familiar with unity and asset licenses. Is that allowed under the current license?
     
  18. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    I couldn't visualize the scrollbar issue, so I can't comment on that one. Honestly, neither do I know how permissive the Asset Store EULA is but you have my permission to include this asset on your GitHub project.
     
  19. Max_depa

    Max_depa

    Joined:
    Jul 2, 2018
    Posts:
    10
    Hi Yasirkula!
    Thanks for this amazing plugin.

    I have a doubt.
    I'm using this to save an image from a directory into the "Resources" folder so that i can later use it.
    The problem is that when I save it i can't see it in the resources folder unless I minimize unity and then reopen it. So I can't save it and use it, but I have to save it, close/reopen unity and then use it.

    Do you have any suggestion to solve this?

    This is my code:

    Code (CSharp):
    1.     IEnumerator ShowLoadDialogCoroutine()
    2.     {
    3.         // Show a load file dialog and wait for a response from user
    4.         // Load file/folder: file, Initial path: default (Documents), Title: "Load File", submit button text: "Load"
    5.         yield return FileBrowser.WaitForLoadDialog(false, null, "Load File", "Load");
    6.  
    7.         // Dialog is closed
    8.         // Print whether a file is chosen (FileBrowser.Success)
    9.         // and the path to the selected file (FileBrowser.Result) (null, if FileBrowser.Success is false)
    10.         Debug.Log(FileBrowser.Success + " " + FileBrowser.Result);
    11.  
    12.         if (FileBrowser.Success)
    13.         {
    14.  
    15.             //Get the name of the file
    16.             newName = FileBrowserHelpers.GetFilename(FileBrowser.Result);
    17.  
    18.             //Copy the file from file browser to the relative path
    19.             FileBrowserHelpers.WriteCopyToFile(
    20.                 Application.dataPath + "/Resources/" + relativePath + newName,
    21.                 FileBrowser.Result);
    22.  
    23.             //Check if the new file exists
    24.             Debug.Log(newName + " exists: " + FileBrowserHelpers.FileExists(Application.dataPath + "/Resources/" + relativePath + newName));
    25.  
    26.             //Load new sprite
    27.             Sprite newSprite = Resources.Load<Sprite>(relativePath + Path.GetFileNameWithoutExtension(newName));
    28.             Debug.Log(newSprite + " loaded!");
    29.  
    30.             //Get the new sprite
    31.             myImage.sprite = newSprite;
    32.         }
    33.     }



    When I check if the new file exists, the log says true, but when I load the new sprite nothing is loaded (unless I close/reopen unity).


    Cheers!
     
  20. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    This code will only work in the Editor. You can't modify the contents of the Resources directory after building the game. Even if you do, Resources.Load won't recognize it. If you are already aware of that but want the code to run in the Editor only, then you can call
    UnityEditor.AssetDatabase.SaveAssets();
    and
    UnityEditor.AssetDatabase.Refresh();
    after WriteCopyToFile.
     
  21. Max_depa

    Max_depa

    Joined:
    Jul 2, 2018
    Posts:
    10
    Ok no I wasn't aware of that... thanks for pointing that out!

    So, there's a way to access to those file even without Resources.Load?
    Thanks!
     
  22. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    You can save those files to Application.persistentDataPath and then load them at runtime with the corresponding asset APIs. For example, you can load textures with Texture2D.LoadImage at runtime but it supports only png and jpeg formats and the created texture will be uncompressed, resulting in more RAM usage.
     
  23. Max_depa

    Max_depa

    Joined:
    Jul 2, 2018
    Posts:
    10
    Thanks for your help! It was really appreciated!
     
    yasirkula likes this.
  24. riqmariz

    riqmariz

    Joined:
    May 14, 2019
    Posts:
    2
    Is there a way that I can change the default folder when start the file browser? I dont know how, I just know how to add a quick link. I already know the path that I want to be the default folder when start the file browser.
     
    Last edited: Mar 14, 2020
  25. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    Yes, the file browser functions take an optional parameter called initialPath. Simply pass the directory there.
     
  26. riqmariz

    riqmariz

    Joined:
    May 14, 2019
    Posts:
    2
    IT WAS IN FRONT OF ME ALL THE TIME ._. thanks, very helpful your asset
     
    yasirkula likes this.
  27. NayaKim

    NayaKim

    Joined:
    Jul 17, 2017
    Posts:
    5
    It is a beginner.
    I want to import local files (images, texts) from Unity through this asset.
    What should I do?
    What does the ok button return?
    help me please,,
     
    Last edited: Mar 23, 2020
  28. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
  29. NayaKim

    NayaKim

    Joined:
    Jul 17, 2017
    Posts:
    5
    Thank you for quick response.
    I found that 'FileBrowser.Result' at the bottom of the example returns the path. Can you search for an example of opening a file with the returned path separately?
     
    Last edited: Mar 23, 2020
  30. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    There you go:
    Code (CSharp):
    1. FileBrowser.ShowLoadDialog( ( path ) =>
    2. {
    3.     Debug.Log( "Selected file: " + path );
    4.     Application.OpenURL( "file://" + path );
    5. }, () =>
    6. {
    7.     Debug.Log( "Clicked Cancel" );
    8. }, false, null, "Select File", "Select" );
    I don't think it will work on mobile platforms, though.
     
  31. NayaKim

    NayaKim

    Joined:
    Jul 17, 2017
    Posts:
    5
    When clicking the button through the example file, the corresponding browser example event is generated.
    On first button click
    IEnumerator ShowLoadDialogCoroutine ()
    Debug.Log (FileBrowser.Success + "," + FileBrowser.Result);
    The FileBrowser.Success bool variable returns false.
    The file does not open even if the correct path is loaded.
    Click the second button to open the image file. Is there a reason?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using SimpleFileBrowser;
    6. using System.IO;
    7.  
    8. public class ExplorerBtn : MonoBehaviour
    9. {
    10.     public GameObject canvas;
    11.     public RawImage image;
    12.  
    13.     private void Awake()
    14.     {
    15.         canvas.SetActive(false);
    16.         image.gameObject.SetActive(false);
    17.     }
    18.     public void Onclick()
    19.     {
    20.         canvas.SetActive(true);
    21.  
    22.         FileBrowser.SetFilters(true, new FileBrowser.Filter("Images", ".jpg", ".png"), new FileBrowser.Filter("Text Files", ".txt", ".pdf"));
    23.         FileBrowser.SetDefaultFilter(".jpg");
    24.         FileBrowser.SetExcludedExtensions(".lnk", ".tmp", ".zip", ".rar", ".exe");
    25.         FileBrowser.AddQuickLink("Users", "C:\\Users", null);
    26.  
    27.         StartCoroutine(ShowLoadDialogCoroutine());
    28.  
    29.     }
    30.  
    31.     IEnumerator ShowLoadDialogCoroutine()
    32.     {
    33.         yield return FileBrowser.WaitForLoadDialog(false, null, "Load File", "Load");
    34.         Debug.Log(FileBrowser.Success + ", " + FileBrowser.Result);
    35.  
    36.         if (FileBrowser.Success)
    37.         {
    38.             using (FileStream fs = new FileStream(FileBrowser.Result, FileMode.Open, FileAccess.Read))
    39.             {
    40.                 byte[] bytes = new byte[fs.Length];
    41.                 fs.Read(bytes, 0, bytes.Length);
    42.  
    43.                 image.gameObject.SetActive(true);
    44.                 Texture2D t2 = new Texture2D(2, 2);
    45.                 t2.LoadImage(bytes);
    46.                 image.texture = t2;
    47.                 image.SetNativeSize();
    48.             }
    49.         }
    50.     }
    51. }
     

    Attached Files:

  32. imaginationrabbit

    imaginationrabbit

    Joined:
    Sep 23, 2013
    Posts:
    349
    Thank you for this wonderful free asset :)

    I'm using one menu where I want the player to be able to load .wav files which I've got working perfectly with this- on the same menu I want the player to be able to load .png files as well- how would I set this up?

    I created a second script that initialized SimpleFileBrowser and set new filters with .png-

    The .wav selection/loading works- but when I click on the button that is supposed to load a .png, it is still using the .wav filter in the menu-

    I'm guessing I did it wrong- how do you reinitialize the filters? Thank you.

    EDIT- I just set the filters on opening the window instead of on Start and it seems to work fine
     
  33. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    @NayaKim Your code looks correct. Try removing the
    canvas.SetActive(true);
    line from Onclick, it is causing the error message in the console.

    @mdotstrange Calling
    FileBrowser.SetFilters(false, ".wav", ".png");
    before displaying the dialog should be sufficient. Glad the issue is resolved!
     
    imaginationrabbit likes this.
  34. unitysrd_phantom

    unitysrd_phantom

    Joined:
    Mar 26, 2020
    Posts:
    2
    Hi yasirkula, thanks for a very nice plugin.

    I have been using your file browser to select and play a video and it works totally fine on Windows.

    Now, I want to use this plugin on Pico VR headset which is Android platform.
    According to your previous replies, I tried to add a "SimpleFileBrowserCanvas", set the render mode to "World Space" and set the camera to the headset. Then, I have my own instance of file browser and put the FileBrowser.HideDialog(); at the start.

    Code (CSharp):
    1. public class FileBrowserTest : MonoBehaviour
    2. {
    3.     public string directory;
    4.  
    5.     void Start()
    6.     {
    7.         FileBrowser.HideDialog();
    8.         FileBrowser.SetFilters(true, new FileBrowser.Filter("Video", ".mp4"));
    9.         FileBrowser.SetDefaultFilter(".mp4");
    10.         FileBrowser.SingleClickMode = true;
    11.         FileBrowser.SetExcludedExtensions(".lnk", ".tmp", ".zip", ".rar", ".exe");
    12.         FileBrowser.AddQuickLink("Users", "C:\\Users", null);
    13.         StartCoroutine(ShowLoadDialogCoroutine());
    14.     }
    15.  
    16.     IEnumerator ShowLoadDialogCoroutine()
    17.     {
    18.         yield return FileBrowser.WaitForLoadDialog(false, null, "Load File", "Load");
    19.         directory = FileBrowser.Result;
    20.         Debug.Log(FileBrowser.Success + " " + FileBrowser.Result);
    21.     }
    22. }
    The file browser is hidden and my instance is not showing.

    Could you please see if I did something wrong? I am very new to Unity and C# programming, pardon my inexperience.
     

    Attached Files:

  35. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    If you are deactivating the SimpleFileBrowserCanvas object before building the game, then you shouldn't. Its Awake function needs to be called for the plugin to know that it needs to present that particular dialog. It is fine to call HideDialog in Start.

    If the object is already active while building the game, then perhaps it is very far away from the VR camera. You can try setting its position to somewhere near 0,0,0. You may also have to modify its Scale value. Create a Cube object at Position 0,0,0 and scale the dialog by comparing its size to the Cube.
     
  36. unitysrd_phantom

    unitysrd_phantom

    Joined:
    Mar 26, 2020
    Posts:
    2
    Thank you for the quick reply! Your suggestion helped me out a lot! Apparently, I could not see the browser because it doesn't have thickness. After I change its position and size around, Now, I see the browser normally.
     
    yasirkula likes this.
  37. Andrey_M

    Andrey_M

    Joined:
    Oct 17, 2015
    Posts:
    7
    Thanks for the wonderful free asset!
    Is it possible to change the veiw of icons in the browser?
    (I mean, loading real images into icon sprites, not prefabs. For example, like in Windows changing the view to "huge icons", 'large icons', 'ordinary icons').
     
  38. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
  39. Andrey_M

    Andrey_M

    Joined:
    Oct 17, 2015
    Posts:
    7
    Something like that!
    I think it is necessary to add coroutine to load textures approximately into this area of the code, but I don’t understand where to assign these values to the icons.

    upload_2020-3-30_21-23-11.png
    Thanks!
     
  40. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
  41. blevok

    blevok

    Joined:
    Feb 16, 2017
    Posts:
    75
    Hi, thanks for making this great file browser. I have it mostly working, but i have a couple questions/issues.

    I have an app that has two UI modes, one for normal touch control with a screen space UI, and one for VR control with a world space UI. I need to show a file browser in both modes.
    In touch mode, i have a button to open files, which starts a coroutine that calls the file browser with WaitForLoadDialog.
    In VR mode, a button starts a different coroutine that first does a HideDialog and then WaitForLoadDialog.

    When i open a file in touch mode, and then switch to VR mode and try to open another file, it does switch from the screen space browser to the world space browser, but the quick links don't load. Clicking folders or the navigation buttons doesn't make it refresh the quick links. Any ideas on that? It does work normally when i open a file in VR mode without using the screen space browser first.

    Also, if i load a file in VR mode with the world space browser, amd then switch back to touch mode and try to load a file, it still opens the world space browser. I've been playing with it all day and i can't figure out how to get the screen space browser back after using HideDialog.

    Should i actually place my own screen space file browser in the scene also? But then how do i control which one i'm calling? Also i'm guessing that will generate errors due to two browsers being in the scene.
    I was thinking maybe doing that and then dragging it into my assets to create a new prefab, and then modify FileBrowser.cs FileBrowser Instance with an if statement to detect the control mode and load my new prefab instead of the one in Resources when control mode is VR.

    Do you think i'm on the right track here, or is there a simpler way?
    Thanks
     
  42. blevok

    blevok

    Joined:
    Feb 16, 2017
    Posts:
    75
    The forum won't let me edit my post. The last line of that last paragraph should be "when control mode is not VR."
     
  43. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    I'd recommend you to make FileBrowser.Instance property public. This way, you can easily access the instance, fetch its
    GetComponent<Canvas>()
    and make it world-space or screen-space. Converting to screen-space should be as trivial as settings its renderMode to ScreenSpaceOverlay. When converting to world-space, after changing the renderMode, you should also set the Canvas' worldCamera property and change its Transform's position, rotation and localScale values. For this, I'd recommend you to keep an empty Transform in your scene, modify its Position, Rotation, Scale accordingly and copy these values to the files browser when switching to world-space.
     
    blevok likes this.
  44. blevok

    blevok

    Joined:
    Feb 16, 2017
    Posts:
    75
    Thanks for the quick reply. This sounds like a great solution, and it should also solve the quick links issue, since it will still be the same instance. I'll be getting back to it later today, and i'll post again if i end up having to do anything that greatly deviates from your suggestion. Thanks again.

    edit: It works perfectly. Thanks
     
    Last edited: Apr 1, 2020
    yasirkula likes this.
  45. dimib

    dimib

    Joined:
    Apr 16, 2017
    Posts:
    50
    Hey @yasirkula , this is a great plugin, great work with SAF on Android! :)

    However, I have an issue where I can read a recorded video file, but I can not play it via the Unity VideoPlayer. It seems not feasible to copy the file into a byte-array into memory and then somehow create a VideoClip from it for playback. It would be better if the file could be directly passed to the VideoPlayer through SAF.

    Do you know how this can be done?
     
  46. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    The only way I can think of would be to read the video file from a Stream but I don't think VideoPlayer has any API for that. Even if it did, with SAF, I could return a Stream in the Java (native Android) environment, I don't know if it is possible to somehow convert it to a managed C# Stream.
     
  47. dimib

    dimib

    Joined:
    Apr 16, 2017
    Posts:
    50
    Another idea: May it be possible to retrieve an absolute file path from the SAF content URI like
    file:///path/to/file.mp4
    from
    content://media/.../1234
    ?
     
  48. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    There are some tricks here and there to convert SAF-path to absolute path but they don't work in all cases (most of the snippets that can be found on Stackoverflow can't return the absolute path if the file is stored on external SD card). And File API probably won't work for those files. I can't help you with this issue, sorry.
     
  49. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,875
    dimib likes this.
  50. RafaelDev

    RafaelDev

    Joined:
    Feb 15, 2017
    Posts:
    12
    Is possible to see images as miniatures instead of icons?