|
| 1 | +# Unity Simple File Browser |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +##Features## |
| 6 | +- Costs about 10 draw calls and 3 SetPass calls |
| 7 | +- Ability to search by name or filter by type |
| 8 | +- Quick links |
| 9 | +- Simple user interface |
| 10 | +- Draggable and resizable |
| 11 | +- Behaves similar to Windows file chooser |
| 12 | +- Ability to choose folders instead of files |
| 13 | +- Slightly optimized (reuses inactive file templates but does not recycle them while scrolling) |
| 14 | + |
| 15 | +-- *Documentation soon; you can inspect the example code below for now to have an idea...* |
| 16 | + |
| 17 | +##Example Code## |
| 18 | +```csharp |
| 19 | +using UnityEngine; |
| 20 | +using System.Collections; |
| 21 | + |
| 22 | +public class FileBrowserTest : MonoBehaviour |
| 23 | +{ |
| 24 | + // Warning: paths returned by SimpleFileBrowser dialogs do not contain a '\' character at the end |
| 25 | + // Warning: SimpleFileBrowser can only show 1 dialog at a time |
| 26 | +
|
| 27 | + void Start() |
| 28 | + { |
| 29 | + // Set filters (optional) |
| 30 | + SimpleFileBrowser.SetFilters( ".jpg", ".png" ); |
| 31 | + |
| 32 | + // Set default filter that is selected when the dialog is shown (optional) |
| 33 | + // Returns true if the default filter is set successfully |
| 34 | + SimpleFileBrowser.SetDefaultFilter( ".jpg" ); |
| 35 | + |
| 36 | + // Set excluded file extensions (by default, .lnk and .tmp extensions are excluded) |
| 37 | + // Note that when you use this function, .lnk and .tmp extensions will no longer be |
| 38 | + // excluded unless you add them as parameters to the function |
| 39 | + SimpleFileBrowser.SetExcludedExtensions( ".lnk", ".tmp", ".zip", ".rar", ".exe" ); |
| 40 | + |
| 41 | + // Add a new quick link to the browser (returns true if quick link is added successfully) |
| 42 | + // Icon: default (folder icon) |
| 43 | + // Name: Users |
| 44 | + // Path: C:\Users |
| 45 | + // Warning: it is sufficient to add a quick link just once, otherwise there may be duplicates |
| 46 | + SimpleFileBrowser.AddQuickLink( null, "Users", "C:\\Users" ); |
| 47 | + |
| 48 | + // Show a save file dialog |
| 49 | + // onSuccess event: not registered (which means this dialog is pretty useless) |
| 50 | + // onCancel event: not registered |
| 51 | + // Save file/folder: file, Initial path: "C:\", Title: "Save As", submit button text: "Save" |
| 52 | + // SimpleFileBrowser.ShowSaveDialog( null, null, false, "C:\\", "Save As", "Save" ); |
| 53 | +
|
| 54 | + // Show a select folder dialog |
| 55 | + // onSuccess event: print the selected folder's path |
| 56 | + // onCancel event: print "Canceled" |
| 57 | + // Load file/folder: folder, Initial path: default (Documents), Title: "Select Folder", submit button text: "Select" |
| 58 | + // SimpleFileBrowser.ShowLoadDialog( (path) => { Debug.Log( "Selected: " + path ); }, |
| 59 | + // () => { Debug.Log( "Canceled" ); }, |
| 60 | + // true, null, "Select Folder", "Select" ); |
| 61 | +
|
| 62 | + // Coroutine example |
| 63 | + StartCoroutine( ShowLoadDialogCoroutine() ); |
| 64 | + } |
| 65 | + |
| 66 | + IEnumerator ShowLoadDialogCoroutine() |
| 67 | + { |
| 68 | + // Show a load file dialog and wait for a response from user |
| 69 | + // Load file/folder: file, Initial path: default (Documents), Title: "Load File", submit button text: "Load" |
| 70 | + yield return StartCoroutine( SimpleFileBrowser.WaitForLoadDialog( false, null, "Load File", "Load" ) ); |
| 71 | + |
| 72 | + // Dialog is closed |
| 73 | + // Print whether a file is chosen (SimpleFileBrowser.Success) |
| 74 | + // and the path to the selected file (SimpleFileBrowser.Result) (null, if SimpleFileBrowser.Success is false) |
| 75 | + Debug.Log( SimpleFileBrowser.Success + " " + SimpleFileBrowser.Result ); |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +##Method Signatures## |
| 81 | + |
| 82 | +```csharp |
| 83 | +public delegate void OnSuccess( string path ); |
| 84 | +public delegate void OnCancel(); |
| 85 | + |
| 86 | +public static bool ShowSaveDialog( OnSuccess onSuccess, OnCancel onCancel, |
| 87 | + bool folderMode = false, string initialPath = null, |
| 88 | + string title = "Save", string saveButtonText = "Save" ); |
| 89 | + |
| 90 | +public static bool ShowLoadDialog( OnSuccess onSuccess, OnCancel onCancel, |
| 91 | + bool folderMode = false, string initialPath = null, |
| 92 | + string title = "Load", string loadButtonText = "Select" ); |
| 93 | + |
| 94 | +public static IEnumerator WaitForSaveDialog( bool folderMode = false, string initialPath = null, |
| 95 | + string title = "Save", string saveButtonText = "Save" ); |
| 96 | + |
| 97 | +public static IEnumerator WaitForLoadDialog( bool folderMode = false, string initialPath = null, |
| 98 | + string title = "Load", string loadButtonText = "Select" ); |
| 99 | + |
| 100 | +public static bool AddQuickLink( Sprite icon, string name, string path ); |
| 101 | + |
| 102 | +public static void SetExcludedExtensions( params string[] excludedExtensions ); |
| 103 | + |
| 104 | +public static void SetFilters( List<string> filters ); |
| 105 | +public static void SetFilters( params string[] filters ); |
| 106 | +public static bool SetDefaultFilter( string defaultFilter ) |
| 107 | +``` |
0 commit comments