StandaloneFileBrowserAndroid.cs 3.1 KB

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