Tanoda
StandaloneFileBrowserAndroid.cs
Go to the documentation of this file.
1#if UNITY_ANDROID && !UNITY_EDITOR
2using System;
3using System.Collections;
4using System.Collections.Generic;
5using System.IO;
6using UnityEngine;
7
8namespace TriLibCore.SFB
9{
10 public class StandaloneFileBrowserAndroidListener : AndroidJavaProxy
11 {
12 private Action<IList<ItemWithStream>> _multipleFilesCallback;
13
14 public StandaloneFileBrowserAndroidListener(Action<IList<ItemWithStream>> onMultipleFilesSelected) : base("com.sfb.standalonefilebrowser.StandaloneFileBrowserAndroidListener")
15 {
16 _multipleFilesCallback = onMultipleFilesSelected;
17 }
18
19 public void onFilesSelected(string filenames)
20 {
21 if (_multipleFilesCallback != null) {
22 var files = filenames.Split('|');
23 var itemsWithStream = new ItemWithStream[files.Length];
24 for (var i = 0; i < itemsWithStream.Length; i++) {
25 itemsWithStream[i] = new ItemWithStream() {
26 Name = files[i]
27 };
28 }
29 _multipleFilesCallback(itemsWithStream);
30 }
31 }
32 }
33
34 public class StandaloneFileBrowserAndroid : IStandaloneFileBrowser<ItemWithStream>
35 {
36 private AndroidJavaClass _standaloneFileBrowser;
37 private AndroidJavaObject _activity;
38
39 public StandaloneFileBrowserAndroid()
40 {
41 using (AndroidJavaObject unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
42 {
43 _activity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
44 _standaloneFileBrowser = new AndroidJavaClass("com.sfb.standalonefilebrowser.StandaloneFileBrowser");
45 }
46 }
47
48 public IList<ItemWithStream> OpenFilePanel(string title, string directory, ExtensionFilter[] extensions, bool multiselect)
49 {
50 throw new NotSupportedException();
51 }
52
53 public void OpenFilePanelAsync(string title, string directory, ExtensionFilter[] extensions, bool multiselect, Action<IList<ItemWithStream>> cb)
54 {
55 var listener = new StandaloneFileBrowserAndroidListener(cb);
56 var args = new object[] {_activity, title, multiselect, listener};
57 _standaloneFileBrowser.CallStatic("showOpenFileDialog", args);
58 }
59
60 public IList<ItemWithStream> OpenFolderPanel(string title, string directory, bool multiselect)
61 {
62 throw new NotSupportedException();
63 }
64
65 public void OpenFolderPanelAsync(string title, string directory, bool multiselect, Action<IList<ItemWithStream>> cb)
66 {
67 throw new NotSupportedException();
68 }
69
70 public ItemWithStream SaveFilePanel(string title, string directory, string defaultName, ExtensionFilter[] extensions)
71 {
72 throw new NotSupportedException();
73 }
74
75 public void SaveFilePanelAsync(string title, string directory, string defaultName, ExtensionFilter[] extensions, Action<ItemWithStream> cb)
76 {
77 throw new NotSupportedException();
78 }
79 }
80}
81#endif