Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

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,090
    Hi there,

    I have created a simple file browser to show save/load file dialogs for one of my projects. It is available as open source on GitHub. Enjoy!

    Asset Store: https://assetstore.unity.com/packages/tools/gui/runtime-file-browser-113006
    Also available at: https://github.com/yasirkula/UnitySimpleFileBrowser
    Support the developer ☕



    Features
    • Behaves similar to Windows file chooser
    • Costs 1 SetPass calls and ~10 batches (assuming that Sprite Packing is enabled in Editor Settings)
    • Ability to search by name or filter by type
    • Quick links
    • Simple user interface
    • Draggable and resizable
    • Ability to choose folders instead of files
    • Supports runtime permissions on Android M+
    • Optimized using a recycled list view (makes Instantiate calls sparingly)
    NOTE: Universal Windows Platform (UWP) and WebGL platforms aren't supported!

    FAQ
    • Android build fails, it says "error: attribute android:requestLegacyExternalStorage not found" in Console
    android:requestLegacyExternalStorage
    attribute in AndroidManifest.xml grants full access to device's storage on Android 10 but requires you to update your Android SDK to at least SDK 29. If this isn't possible for you, you should open SimpleFileBrowser.aar with WinRAR or 7-Zip and then remove the
    <application ... />
    tag from AndroidManifest.xml.
    • Can't show the file browser on Android, it says "java.lang.ClassNotFoundException: com.yasirkula.unity.FileBrowserPermissionReceiver" in Logcat
    If you are sure that your plugin is up-to-date, then enable Custom Proguard File option from Player Settings and add the following line to that file:
    -keep class com.yasirkula.unity.* { *; }

    • File browser doesn't show any files on Android 10+
    File browser uses Storage Access Framework on these Android versions and users must first click the Pick Folder button in the quick links section.
    • RequestPermission returns Permission.Denied on Android
    Declare the WRITE_EXTERNAL_STORAGE permission manually in your Plugins/Android/AndroidManifest.xml file with the
    tools:node="replace"
    attribute as follows:
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:node="replace"/>
    (you'll need to add the
    xmlns:tools="http://schemas.android.com/tools"
    attribute to the
    <manifest ...>
    element).

    You can visit the repo for more information...
     
    Last edited: Apr 12, 2021
  2. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,090
    Update (31.01.2017):

    - Optimized the scroll view using a recycled list view
    - Added multi-filter feature (more than one extensions (.jpg, .png, etc.) can be assigned to a single filter, see Example Code)
     
  3. Pandoflo

    Pandoflo

    Joined:
    May 31, 2017
    Posts:
    28
    hey there i have a question for you, if you are still working on this.
    i tried to use your filebrowser and i got it implemented in my unity game.
    but the thing is after i select a file i want to do something with that file. i have a script which defines what has to be done.
    but i cant get to the path of the selected file
    it is kinda imbossible for me to say in my other script. (" take the path from the selected file and use the information in the file")
    can u help me out?
     
  4. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,090
    In your script that defines what has to be done, can't you simply show the file browser using the SimpleFileBrowser.FileBrowser.ShowLoadDialog function and call your own function in the onSuccess delegate, like this:

    Code (CSharp):
    1. SimpleFileBrowser.FileBrowser.ShowLoadDialog( (path) => { YourFunctionThatNeedsThePathOfTheFile(path); }, null,
    2.         false, null, "Select File", "Select" );
    P.S: Also you can access the most recently selected file's path using SimpleFileBrowser.FileBrowser.Result property.
     
  5. ichbinsteffen

    ichbinsteffen

    Joined:
    Nov 11, 2017
    Posts:
    7
    When I import the package, the using directive doesn't work.
    SimpleFileBrowser now is now in the same directory as the other assets,
    but Visual Studio says that it cannot find the class or namespace SimpleFileBrowser.
    What can I do?
     
  6. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,090
    That surely is strange. Are there any errors in Unity when you return to Unity? If so, what is your Unity version? Where did you save your script? Is it in Standard Assets folder or another special folder?
     
  7. ichbinsteffen

    ichbinsteffen

    Joined:
    Nov 11, 2017
    Posts:
    7
    For some reason the next day it just worked.
    I didn't change anything, so I guess it was the restart of Unity and/or Visual Studio.

    Now that it's working there is just one thing bothering me a bit,
    when the class initializes my application is like ..freezed.. for 2-3 seconds.
    I have to investigate a little further into this, because it might be something about my own code.
    I have to initialize it in a more isolated environment and see how it behaves.
     
  8. ichbinsteffen

    ichbinsteffen

    Joined:
    Nov 11, 2017
    Posts:
    7
    So it seems the SimpleFileBrowser class causes the little freeze.
    I tried it in a isolated environment and it's still the same.
    It is a bit annoying.
     
  9. Lamarn

    Lamarn

    Joined:
    Nov 1, 2017
    Posts:
    2
    I am using this file picker for a long time now, and I am very happy, that you made it more public now :D
     
    yasirkula likes this.
  10. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,090
    Although months have passed since your reply, I was finally able to update the package. Along with some important bugfixes and implementation of runtime permissions on Android platform, I've also added a HideDialog function which can be used to initialize the browser ahead of time, as well. Calling this function when your app starts should eliminate the freeze.
     
  11. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,090
  12. Go2ready

    Go2ready

    Joined:
    May 8, 2015
    Posts:
    29
    Thank you for sharing! 5 stars
     
    yasirkula likes this.
  13. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,090
    I appreciate it, thanks.
     
  14. mborke

    mborke

    Joined:
    Jan 16, 2017
    Posts:
    5
    First of all, thank you for sharing this. Now my question:
    If I want to clear the FileBrowser.Result upon opening the Load dialog, how can I do that? In my app, there are a few different types of files that users load at different times. If they load one type and then try to load another type, the browser shows the correct files (of desired type) in the browsing portion, but still has the name + extension of the previously loaded file.
     
    yasirkula likes this.
  15. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,090
    Oh, I totally forgot to clear that input field. Thanks for catching that! To solve it, you can insert the following line to FileBrowser.Show function (line 750):

    Code (CSharp):
    1. filenameInputField.text = string.Empty;
     
  16. KhanDev

    KhanDev

    Joined:
    Jun 27, 2015
    Posts:
    4
    Thanks for sharing this awesome plugin!
    I have a question though, I'm trying to use it on a older version of Unity (5.6.0p2) and while the save and load dialog open, nothing is showed in the FilesContainer (But the gameobjects are there). I tried the same setup in the latest Unity (2017.3.0f3) and all is working as intended.

    Maybe there is an old Unity bug in the UI system in the 5.6.0p2 that i'm missing. Any clue on that? It's supposed to work on all versions or there is a minimun requirement?
    Besides that it's an amazing plugin!

    Unity 5.6.0p2


    Unity 2017.3.0f3
     
  17. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,090
    There is no known minimum requirement; it is supposed to work on a majority of the Unity 5 versions. I'm thinking that the Scroll Rect is not refreshed automatically. I'll look into it.
     
  18. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,090
    Just to be sure; you have the latest version on the Asset Store/GitHub repo, right?
     
  19. KhanDev

    KhanDev

    Joined:
    Jun 27, 2015
    Posts:
    4
    Sorry for the late response.
    Yeap I downloaded it yesterday from Github. It's weird to me that in Unity 2017.3.0f3 is working like a charm.

    Edit: Just searching for solutions to this problem I found this post: https://answers.unity.com/questions/1344044/scroll-rect-doesnt-display-items-unity-56.html

    It seems like it is a problem with the ScrollRect in that version of Unity. I'll look into fixing it without upgrading to 5.6.0p3.
     
    Last edited: Apr 1, 2018
  20. KhanDev

    KhanDev

    Joined:
    Jun 27, 2015
    Posts:
    4
    I found a way to make it work, but I want to know if it's ok to change this.

    If you remove the canvas and graphic raycaster from the Files Scroll Rect, then it shows the content normally.

    In the patch notes from Unity 5.6.0p3 there are 2 issues that could be related
    (893559) - UI: Fixed NaN issues with ScrollRect when using clamped mode.
    (892913) - UI: Fixed nested canvas returning a null world camera after its root canvas had been disabled and then re-enabled.
    For now I'll use it like this. I let you know if I found something weird happens after this change.

    Thanks for the help! Awesome plugin!

    fixthere.png
     
    yasirkula likes this.
  21. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,090
    Thanks for all the input! I had added Canvas component to Files object primarily for performance concerns but I think that might have been an overkill. Removing it will also reduce the number of SetPass calls and Batches. I may just get rid of it and push an update.
     
  22. KhanDev

    KhanDev

    Joined:
    Jun 27, 2015
    Posts:
    4
    Thanks to you for creating this in the first place! That recycled list view is great!

    Glad to help!
     
  23. jovemcaio

    jovemcaio

    Joined:
    Mar 19, 2018
    Posts:
    8
    Hey, thank you for the plugin!
    It is working fine, I just have some concerns about these messages appearing on the Debug Console saying there are multiple event systems on the scene. Is that normal?

    multiple event systems.png
     
  24. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,090
    It is a harmless warning: SimpleFileBrowserCanvas prefab includes an EventSystem object in case current scene doesn't have any. An EventSystem is mandatory to interact with UI. But if your scene already has an EventSystem, you can safely deactivate the EventSystem child object of the SimpleFileBrowserCanvas prefab. Or, if you won't mind the warnings, you can just leave everything as is.
     
  25. jovemcaio

    jovemcaio

    Joined:
    Mar 19, 2018
    Posts:
    8
    Thanks for the reply :)
     
  26. muhammad-qasim161

    muhammad-qasim161

    Joined:
    Apr 24, 2018
    Posts:
    3
    could i import a 3d model or 3d object through this project.. file extension could be any!!
     
  27. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,090
    This plugin will only return the path of the selected file. It is then up to you to process that file. It seems like there are some runtime OBJ importers out there already but you may have a harder time finding an FBX importer. Supporting any extension will be even harder.
     
  28. PhonArt

    PhonArt

    Joined:
    Dec 17, 2015
    Posts:
    9
    Hello,
    first at all, thank you for this great asset !

    But i have a small issue :
    When i build my game to android, in development build, the explorer works great ! But if i export it in release build, the file explorer don't shows up...

    Thank you in advance,
    Best Reguards,
    Alexandre Desfontaines.
     
  29. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,090
    Are you using Mono or IL2CPP? Can you check logcat for error messages?
     
  30. PhonArt

    PhonArt

    Joined:
    Dec 17, 2015
    Posts:
    9
    I'm using "Mono" scripting backend and i've got "Object reference not set to an instance of an Object" error in logcat.

    Thank you for your fast response !
    Best Reguards
    Alexandre Desfontaines.
     
  31. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,090
    I can't reproduce the issue on release Android build so we will have to dig deeper. Can you change the Instance property of FileBrowser.cs like this:

    Code (CSharp):
    1. private static FileBrowser Instance
    2. {
    3.     get
    4.     {
    5.         if( m_instance == null )
    6.         {
    7.             Debug.Log( "AAAAAA" );
    8.             m_instance = Instantiate( Resources.Load<GameObject>( "SimpleFileBrowserCanvas" ) ).GetComponent<FileBrowser>();
    9.             Debug.Log( "BBBBBB" );
    10.             DontDestroyOnLoad( m_instance.gameObject );
    11.             Debug.Log( "CCCCCC" );
    12.             m_instance.gameObject.SetActive( false );
    13.         }
    14.         Debug.Log( "DDDDDD" );
    15.         return m_instance;
    16.     }
    17. }
    Which debug lines are logged to logcat?
     
  32. PhonArt

    PhonArt

    Joined:
    Dec 17, 2015
    Posts:
    9
    I can see the "DDDDDD" debug.log but i've got the error after the debug.log.
     
  33. PhonArt

    PhonArt

    Joined:
    Dec 17, 2015
    Posts:
    9
    I've got something more that could be interesting : http://prntscr.com/jmvg98
    Hope that could help.
    ExportCoroutine is my coroutine to export a file.
     
  34. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,090
    If you have a SimpleFileBrowserCanvas instance in your scene, try deleting it and see if it changes anything. If not, insert a Debug.Log to each line of FileBrowser.SetFiltersPostProcessing function to pinpoint the problematic line.
     
  35. PhonArt

    PhonArt

    Joined:
    Dec 17, 2015
    Posts:
    9
    Code (CSharp):
    1.    
    2. for ( int i = 0; i < filters.Count; i++ )
    3.             {
    4.                 string filterStr = filters[i].ToString();
    5.                 dropdownValues.Add( filterStr );
    6.  
    7.                 maxFilterStrLength = Mathf.Max( maxFilterStrLength, Instance.CalculateLengthOfDropdownText( filterStr ) );
    8.             }
    9.  
    This is the part who crash the script, i don't get logs after, I'll try to isolate the line.
     
    yasirkula likes this.
  36. PhonArt

    PhonArt

    Joined:
    Dec 17, 2015
    Posts:
    9
    string filterStr = filters[i].ToString();


    This line is causing the error. I'll try to reimport the asset. Maybe i've touche something...
     
  37. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,090
    Probably allFilesFilter of the FileBrowser is not initialized (Awake function). But this doesn't make much sense because Awake is called immediately after an object is instantiated or when scene starts. Therefore, my guess is that FileBrowser.Awake throws an exception before allFilesFilter is initialized.

    Can you make sure that allFilesFilter is initialized by putting a Debug.Log after that line? Also it may be helpful to see the portion of your script that calls FileBrowser. Finally make sure there is no FileBrowser in your current scene or one of your previous scene(s).
     
  38. PhonArt

    PhonArt

    Joined:
    Dec 17, 2015
    Posts:
    9
    Your guess was right, the allFilters doesn't initialize. I don't get the Debug.Log statement.

    Here is amore complete screenshot : http://prntscr.com/jmw3dk
     
  39. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,090
    Does your project use Gradle build system and maybe make use of Proguard?
     
  40. PhonArt

    PhonArt

    Joined:
    Dec 17, 2015
    Posts:
    9
    Yep, i need the use of Gradle to keep using the Google Play Service. But nope, i don't use Proguard.
    Do i need to switch it back to internal ?
     
  41. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,090
    It isn't needed. I've tried the plugin with Gradle build system with "Export Project" selected and also with it unselected and file browser worked fine.

    The issue is that FileBrowser's native Java interface com.yasirkula.unity.FileBrowser is apparently excluded from your build. If you find a proguard file somewhere in your Unity project (can be inside an .aar file) or Android Studio project, try adding the following line to it:

    -keep class com.yasirkula.unity.* { *; }
     
  42. PhonArt

    PhonArt

    Joined:
    Dec 17, 2015
    Posts:
    9
    Sorry for the late responde, but IT WORKS ! Thank you for your awesome support ! And your fasts replies !

    Best Reguards,
    Alexandre Desfontaines.
     
    kamran-bigdely and yasirkula like this.
  43. mariosch

    mariosch

    Joined:
    Aug 14, 2017
    Posts:
    1
    any chance of making an example project/scene for the complete dummies among us.. (like me)..
     
  44. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,090
  45. rdunagan

    rdunagan

    Joined:
    Jun 8, 2018
    Posts:
    2
    First of all, this is an amazing plugin! You're amazing for being willing to share this for free! Thanks a ton.

    Second of all, I'm trying to see if I can't get this working in VR on an Oculus GO. My first thought would be to reference the Canvas once it's instantiated in the scene and set its Render Mode to World Space. Would this work, or is there some other way I could go about it?
     
  46. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,090
    You can have an instance of SimpleFileBrowserCanvas in your scene with Render Mode set to World Space, and hide the dialog in Start function:
    FileBrowser.HideDialog();
    . This way, SimpleFileBrowser will use your instance.

    However, there are two possible issues with this setup: I don't think it is possible to double-click files fast enough in VR, and dragging the file browser is simply broken in World Space rendering. I'll try to implement a fix for these issues soon.
     
  47. rdunagan

    rdunagan

    Joined:
    Jun 8, 2018
    Posts:
    2
    Thanks for the super quick response! This is just about what I've done so far, and now I'm working on the UI interaction. Can you think of any reason why there may be any weird interactions between SimpleFileBrowser and the OVRRaycast component? I'm not sure if you've worked with the Oculus SDK before, but it's essentially the same as the Standalone Input Module in EventSystem, but it raycasts from the HMD Eye View. Using the OVRInput module or enabling the Standalone Input Module breaks the functionality of all worldspace UI in the scene.
     
  48. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,090
    @rdunagan I've updated the GitHub repository and added a static FileBrowser.SingleClickMode property while fixing the world space canvas issues.

    I don't have any VR experience, but try disabling all Standalone Input Module object(s) in your scene. If that doesn't work, then I'm out of ideas, sorry.
     
  49. bz_tian

    bz_tian

    Joined:
    Jun 28, 2018
    Posts:
    4
    Very nice, definitely saved me time.. Thank you!
     
  50. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,090
    Hi!

    If you want to modify the UI of the file browser (object positions, sprites, fonts etc.), then you should indeed modify SimpleFileBrowserCanvas and apply your changes to the prefab. If you don't want to position your canvas in world space, then you can delete the instance in your scene because plugin automatically creates an instance of the prefab.

    If you want to modify the properties that are exposed to Scripting API, you should do so in your own scripts (e.g. just before showing the file browser). Don't modify FileBrowser.cs unless you want to extend the plugin's functionality.

    To show only folders, you can set the third parameter (folderMode) of the ShowSaveDialog/ShowLoadDialog function to true.
     
unityunity