Just another File Dialog for Unity, with easy implementation. Using Ookii Dialogs. Rather than putting everything in .dll, i've done this simply with a class, so you can extend it based on Ookii Dialogs.
Download the latest release from Releases. Import it to Unity. Or, if you want download the project, just clone it.
After importing, change Change API Compatibility Level to .NET 4.x, from Project Settings -> Player -> Other Settings
. And, change Scripting Backend to Mono.
Implementation is quite easy, but before implementing let's know about BrowserProperties constructor.
BrowserProperties
public class BrowserProperties
{
public string title; //Title of the Dialog
public string initialDir; //Where dialog will be opened initially
public string filter; //aka File Extension for filtering files
public int filterIndex; //Index of filter, if there is multiple filter. Default is 0.
public bool restoreDirectory = true; //Restore to last return directory
public BrowserProperties() { }
public BrowserProperties(string title) { this.title = title; }
}
Make sure to define AnotherFileBrowser.Windows
namespace.
var bp = new BrowserProperties();
bp.filter = "txt files (*.txt)|*.txt|All Files (*.*)|*.*";
bp.filterIndex = 0;
new FileBrowser().OpenFileBrowser(bp, path =>
{
//Do something with path(string)
Debug.Log(path);
});
var bp = new BrowserProperties();
bp.filter = "txt files (*.txt)|*.txt|All Files (*.*)|*.*";
bp.filterIndex = 0;
new FileBrowser().OpenMultiSelectFileBrowser(bp, path =>
{
//Do something with path(string[])
string s = "";
for (int i = 0; i < path.Length; i++)
{
s += path[i] + "\n";
}
});
var bp = new BrowserProperties();
bp.filter = "txt files (*.txt)|*.txt|All Files (*.*)|*.*";
bp.filterIndex = 0;
new FileBrowser().OpenFolderBrowser(bp, path =>
{
//Do something with path(string)
Debug.Log(path);
});
var bp = new BrowserProperties();
bp.filter = "txt files (*.txt)|*.txt";
bp.filterIndex = 0;
new FileBrowser().SaveFileBrowser(bp, "test", ".txt", path =>
{
//Do something with path(string)
Debug.Log(path);
});
var bp = new BrowserProperties();
bp.filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png";
bp.filterIndex = 0;
new FileBrowser().OpenFileBrowser(bp, path =>
{
//Load image from local path with UWR
StartCoroutine(LoadImage(path));
});
//Load Image from Local
IEnumerator LoadImage(string path)
{
using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(path))
{
yield return uwr.SendWebRequest();
if (uwr.isNetworkError || uwr.isHttpError)
{
Debug.Log(uwr.error);
}
else
{
var uwrTexture = DownloadHandlerTexture.GetContent(uwr);
//placeholderImage is an Image Component of Unity UI
placeholderImage.sprite = Sprite.Create(uwrTexture, new Rect(0, 0, uwrTexture.width, uwrTexture.height), new Vector2(0, 0));
}
}
}
Use preprocessor for Platform Dependent Compilation, so there won't be any issue while building for other platforms.
I'm facing crashes if try to open dialog from il2cpp build. If you want to try in il2cpp, you need to add Mono.Posix
& Mono.WebBrowser
from C:\Program Files\Unity\Editor\Data\MonoBleedingEdge\lib\mono\gac
to \Plugins
folder.
There must be a workaround, you can check crash logs after crash. I will try to fix it, if you already fixed it, open a PR, it would be highly appreciated.
This is only for Windows. You can check out UnityNativeFilePicker by Yasirkula. There are plenty of native plugins, but this one support iOS too.
Indie Game Programmer
Mail: srejonkhan9@gmail.com