yasirkula / UnitySimpleFileBrowser

A uGUI based runtime file browser for Unity 3D (draggable and resizable)
MIT License
817 stars 111 forks source link

Using a custom canvas for FileBrowser on Android 10 #41

Closed astaikos316 closed 3 years ago

astaikos316 commented 3 years ago

I am trying to figure out if I can use a custom canvas instead of having to call ShowLoadDialog on Android 10. I have been developing an application for quite some time now and have been using this great browser on other platforms. In my app I set up a custom canvans to place the file browser window in a specific area on the screen. To open the file browser canvas I used to just activate and deactivate the Canvas GameObject during runtime and this still works on Windows Platforms. On Android 10 however, if i try to use my custom canvas, when I activate the gameobject I get no file listings and the quick links is blank. I tryid a simple test on Android by calling ShowLoadDialog instead of my custom canvas and the browser does give me the file listings and quick links shows primary drive. Is there a way to get the file listings to show up on Android 10 without having to call ShowLoadDialog as it works on Windows?

yasirkula commented 3 years ago

You can call ShowLoadDialog after activating your own Canvas GameObject. Calling ShowLoadDialog is inevitable. This working fine on Windows must be a minor bug.

astaikos316 commented 3 years ago

filebrowser As shown in the attached, I customized the prefab for some specific functionality and use it as a GameObject window that I SetActive when the window is required. I do not have to run ShowLoadDialog explicitly on windows to give me the file listings. When I try to run it the same way on Android, my quick inks and file list is blank. No errors in debug in Android Studio. If i deactivate this custom gameobject and just run showloaddialog, i get the default Prefab Canvas come up and the file list is shown. Is there something i am not understanding?

yasirkula commented 3 years ago

You must show the file dialog as follows, there is no workaround for it:

yourCustomFileBrowserCanvas.gameObject.SetActive(true);
FileBrowser.ShowLoadDialog(somethingsomething);

As I said, the behaviour on Windows must be a bug, the browser has to be shown with ShowLoadDialog. FileBrowser will use your canvas when ShowLoadDialog is called if your dialog is active in the scene at that time.

astaikos316 commented 3 years ago

Ok i think I understand a little more now. Since I am using some custom buttons, i am not sure what the ShowLoadDialog funtion options should be. Can you provide any insight there?

yasirkula commented 3 years ago

You can pass null to onSuccess and onCancel parameters.

astaikos316 commented 3 years ago

Do I have to pass anything to the title and button text options or can I leave those out or null?

yasirkula commented 3 years ago

You need to pass the same title and button text or your changes will be overridden. You can modify FileBrowser.cs to ignore those parameters, if you wish. Don't hesitate give it a shot.

astaikos316 commented 3 years ago

You must show the file dialog as follows, there is no workaround for it:

yourCustomFileBrowserCanvas.gameObject.SetActive(true);
FileBrowser.ShowLoadDialog(somethingsomething);

As I said, the behaviour on Windows must be a bug, the browser has to be shown with ShowLoadDialog. FileBrowser will use your canvas when ShowLoadDialog is called if your dialog is active in the scene at that time.

I have tried this, however when I build out to Android and run it on the device, my filebrowser window continues to show empty.

yasirkula commented 3 years ago

Does the Browse... button not show up in quick links section? What are the error messages in logcat?

astaikos316 commented 3 years ago

I was able to figure out the issue. There was an issue with upgrading the overall project to the latest version. I am able to open and browse on Android now. However, I need to be able to create directories in C# and write files to them. It is not clear how to use FileBrowserHelpers.CreateFolderInDirectory( string directoryPath, string folderName ) function call as I do not know what the directoryPath variable should be.

yasirkula commented 3 years ago

It is the path returned by FileBrowser.ShowLoadDialog when pickMode is set to Folders. If it is set to Files and you'd like to create a folder in the selected file's parent directory, you'd first have to get that parent directory's path via FileBrowserHelpers.GetDirectoryName.

astaikos316 commented 3 years ago

Is there a way with getting a directory listing in c# without interacting with the GUI?

yasirkula commented 3 years ago

Without interacting with the GUI, only persistentDataPath and temporaryCachePath directories can be queried. Storage Access Framework requires us to explicitly ask the user for permission to browse a folder's contents. Hence you see the "Browse..." button in the file browser on Android 10+.

Even if the user had granted permission for e.g. /mnt/0/DCIM folder, you still can't get the files inside it using that raw path. You need the SAF path returned by the file browser. But, if you cache the picked directory's SAF path, later on you can access this directory without interacting with the GUI using that SAF path (as long as the user doesn't delete that directory).

astaikos316 commented 3 years ago

I actually do not get the "Browse..." button in the fie browser. I see Primary Drive in the quick links section and the folder list when opening the browser.

yasirkula commented 3 years ago

In that case, you can use C#'s File/Directory API on that device. On Android 11+, always Storage Access Framework is used so my previous message applies to those devices.