StandaloneFileBrowserWinRT.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #if UNITY_WSA && !UNITY_EDITOR
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Threading.Tasks;
  6. using Windows.Storage;
  7. using TriLibCore;
  8. using UnityEngine;
  9. namespace TriLibCore.SFB
  10. {
  11. public class StandaloneFileBrowserWinRT : IStandaloneFileBrowser<ItemWithStream>
  12. {
  13. public IList<ItemWithStream> OpenFilePanel(string title, string directory, ExtensionFilter[] extensions, bool multiselect)
  14. {
  15. return null;
  16. }
  17. public IList<ItemWithStream> OpenFolderPanel(string title, string directory, bool multiselect)
  18. {
  19. return null;
  20. }
  21. public ItemWithStream SaveFilePanel(string title, string directory, string defaultName, ExtensionFilter[] extensions)
  22. {
  23. return null;
  24. }
  25. public void OpenFilePanelAsync(string title, string directory, ExtensionFilter[] extensions, bool multiselect, Action<IList<ItemWithStream>> cb)
  26. {
  27. UnityEngine.WSA.Application.InvokeOnUIThread(async () =>
  28. {
  29. var filePicker = new Windows.Storage.Pickers.FileOpenPicker();
  30. if (extensions != null)
  31. {
  32. var existingExtensions = new List<string>();
  33. foreach (var extension in extensions)
  34. {
  35. foreach (var filter in extension.Extensions)
  36. {
  37. if (filter == "*" || filter == ".*" || filter == "*.*" || existingExtensions.Contains(filter)) {
  38. continue;
  39. }
  40. filePicker.FileTypeFilter.Add("." + filter);
  41. existingExtensions.Add(filter);
  42. }
  43. }
  44. }
  45. if (multiselect)
  46. {
  47. var files = await filePicker.PickMultipleFilesAsync();
  48. var result = new ItemWithStream[files.Count];
  49. for (var i = 0; i < files.Count; i++)
  50. {
  51. result[i] = new ItemWithStream()
  52. {
  53. Name = files[i].Name,
  54. Stream = await ReadStorageFile(files[i])
  55. };
  56. }
  57. await Task.Run(() => cb(result));
  58. }
  59. else
  60. {
  61. var file = await filePicker.PickSingleFileAsync();
  62. var fileWithStream = new ItemWithStream()
  63. {
  64. Name = file.Name,
  65. Stream = await ReadStorageFile(file)
  66. };
  67. await Task.Run(() => cb(new[] { fileWithStream }));
  68. }
  69. }, false);
  70. }
  71. public void OpenFolderPanelAsync(string title, string directory, bool multiselect, Action<IList<ItemWithStream>> cb)
  72. {
  73. UnityEngine.WSA.Application.InvokeOnUIThread(async () =>
  74. {
  75. var folderPicker = new Windows.Storage.Pickers.FolderPicker();
  76. var folder = await folderPicker.PickSingleFolderAsync();
  77. var folderWithStream = new ItemWithStream()
  78. {
  79. Name = folder.Name
  80. };
  81. await Task.Run(() => cb(new[] { folderWithStream }));
  82. }, false);
  83. }
  84. public void SaveFilePanelAsync(string title, string directory, string defaultName, ExtensionFilter[] extensions, Action<ItemWithStream> cb)
  85. {
  86. UnityEngine.WSA.Application.InvokeOnUIThread(async () =>
  87. {
  88. var filePicker = new Windows.Storage.Pickers.FileSavePicker();
  89. filePicker.SuggestedFileName = defaultName;
  90. foreach (var extension in extensions)
  91. {
  92. filePicker.FileTypeChoices.Add(extension.Name, extension.Extensions);
  93. }
  94. var file = await filePicker.PickSaveFileAsync();
  95. var fileWithStream = new ItemWithStream()
  96. {
  97. Name = file.Name
  98. };
  99. await Task.Run(() => cb(fileWithStream));
  100. }, false);
  101. }
  102. private static async Task<Stream> ReadStorageFile(StorageFile storageFile)
  103. {
  104. return await storageFile.OpenStreamForReadAsync();
  105. }
  106. }
  107. }
  108. #endif