Unity Standalone File Browser

repository·master·Indexed 25 days ago

https://github.com/gkngkc/unitystandalonefilebrowser

A Unity wrapper for native Open, Save, and Folder file dialogs supporting Windows, Mac, Linux, and WebGL. Provides synchronous and asynchronous methods via StandaloneFileBrowser, including OpenFilePanel and SaveFilePanel, with support for file type filtering using ExtensionFilter.

Tokens
1.3K
Snippets
4
Records
5
Agent score
32%

What's inside unitystandalonefilebrowser

  1. WebGL Support and Limitations

    master

    The package provides basic support for WebGL, primarily focused on file upload and download functionality.

    Important Considerations:

    • WebGL does not directly implement the standard Open/Save calls because browsers require specific handling for file operations.
    • Support for file filters is included but is not extensively tested and may be unreliable.
    • For implementation details, refer to CanvasSampleScene.unity and the provided canvas sample scripts in the package.
  2. Install Unity Standalone File Browser

    master

    To use this package in your Unity project, download the .unitypackage from the releases page and import it into your project.

    Windows Requirements:

    • Set the API Compatibility Level to .NET 2.0.
    • Ensure plugin import settings for Ookii and System.Forms are configured correctly as per the project documentation.

    Platform Support:

    • Windows, Mac, and Linux (Runtime and Editor).
    • Basic WebGL support (via specific canvas/upload/download implementations).
    [Download Package](https://github.com/gkngkc/UnityStandaloneFileBrowser/releases/download/1.2/StandaloneFileBrowser.unitypackage)
  3. Use ExtensionFilter for file type filtering

    master

    To restrict the files visible in the dialog to specific types, create an array of ExtensionFilter objects. Each filter takes a display name and one or more extensions.

    Example:

    var extensions = new [] {
        new ExtensionFilter("Image Files", "png", "jpg", "jpeg" ),
        new ExtensionFilter("All Files", "*" ),
    };
    var extensions = new [] {
        new ExtensionFilter("Image Files", "png", "jpg", "jpeg" ),
        new ExtensionFilter("Sound Files", "mp3", "wav" ),
        new ExtensionFilter("All Files", "*" ),
    };
  4. Open files and folders with StandaloneFileBrowser

    master

    Use StandaloneFileBrowser.OpenFilePanel for synchronous file selection or StandaloneFileBrowser.OpenFilePanelAsync for asynchronous selection.

    Parameters for OpenFilePanel:

    • title (string): The title of the dialog.
    • directory (string): The starting directory.
    • extensions (ExtensionFilter[]): An array of ExtensionFilter objects to restrict file types.
    • multiselect (bool): Whether to allow selecting multiple files.

    Note for Windows users: Async methods are currently wrappers around synchronous methods and do not provide true non-blocking behavior.

    // Open file (Sync)
    var paths = StandaloneFileBrowser.OpenFilePanel("Open File", "", "", false);
    
    // Open file (Async)
    StandaloneFileBrowser.OpenFilePanelAsync("Open File", "", "", false, (string[] paths) => {  });
    
    // Open file with filters
    var extensions = new [] {
        new ExtensionFilter("Image Files", "png", "jpg", "jpeg" ),
        new ExtensionFilter("Sound Files", "mp3", "wav" ),
        new ExtensionFilter("All Files", "*" ),
    };
    var paths = StandaloneFileBrowser.OpenFilePanel("Open File", "", extensions, true);
  5. Save files with StandaloneFileBrowser

    master

    Use StandaloneFileBrowser.SaveFilePanel for synchronous saving or StandaloneFileBrowser.SaveFilePanelAsync for asynchronous saving.

    Parameters for SaveFilePanel:

    • title (string): The title of the dialog.
    • directory (string): The starting directory.
    • defaultName (string): The default filename.
    • extension (string): The default extension.
    • extensions (ExtensionFilter[]): An array of ExtensionFilter objects to restrict file types.

    Note for Mac users: Synchronous calls may throw an exception in development builds when the native panel loses/gains focus. It is recommended to use the Async methods on macOS.

    // Save file (Sync)
    var path = StandaloneFileBrowser.SaveFilePanel("Save File", "", "", "");
    
    // Save file (Async)
    StandaloneFileBrowser.SaveFilePanelAsync("Save File", "", "", "", (string path) => {  });
    
    // Save file with filters
    var extensionList = new [] {
        new ExtensionFilter("Binary", "bin"),
        new ExtensionFilter("Text", "txt"),
    };
    var path = StandaloneFileBrowser.SaveFilePanel("Save File", "", "MySaveFile", extensionList);