123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- #if UNITY_WSA && !UNITY_EDITOR
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Threading.Tasks;
- using Windows.Storage;
- using TriLibCore;
- using UnityEngine;
- namespace TriLibCore.SFB
- {
- public class StandaloneFileBrowserWinRT : IStandaloneFileBrowser<ItemWithStream>
- {
- public IList<ItemWithStream> OpenFilePanel(string title, string directory, ExtensionFilter[] extensions, bool multiselect)
- {
- return null;
- }
- public IList<ItemWithStream> OpenFolderPanel(string title, string directory, bool multiselect)
- {
- return null;
- }
- public ItemWithStream SaveFilePanel(string title, string directory, string defaultName, ExtensionFilter[] extensions)
- {
- return null;
- }
- public void OpenFilePanelAsync(string title, string directory, ExtensionFilter[] extensions, bool multiselect, Action<IList<ItemWithStream>> cb)
- {
- UnityEngine.WSA.Application.InvokeOnUIThread(async () =>
- {
- var filePicker = new Windows.Storage.Pickers.FileOpenPicker();
- if (extensions != null)
- {
- var existingExtensions = new List<string>();
- foreach (var extension in extensions)
- {
- foreach (var filter in extension.Extensions)
- {
- if (filter == "*" || filter == ".*" || filter == "*.*" || existingExtensions.Contains(filter)) {
- continue;
- }
- filePicker.FileTypeFilter.Add("." + filter);
- existingExtensions.Add(filter);
- }
- }
- }
- if (multiselect)
- {
- var files = await filePicker.PickMultipleFilesAsync();
- var result = new ItemWithStream[files.Count];
- for (var i = 0; i < files.Count; i++)
- {
- result[i] = new ItemWithStream()
- {
- Name = files[i].Name,
- Stream = await ReadStorageFile(files[i])
- };
- }
- await Task.Run(() => cb(result));
- }
- else
- {
- var file = await filePicker.PickSingleFileAsync();
- var fileWithStream = new ItemWithStream()
- {
- Name = file.Name,
- Stream = await ReadStorageFile(file)
- };
- await Task.Run(() => cb(new[] { fileWithStream }));
- }
- }, false);
- }
- public void OpenFolderPanelAsync(string title, string directory, bool multiselect, Action<IList<ItemWithStream>> cb)
- {
- UnityEngine.WSA.Application.InvokeOnUIThread(async () =>
- {
- var folderPicker = new Windows.Storage.Pickers.FolderPicker();
- var folder = await folderPicker.PickSingleFolderAsync();
- var folderWithStream = new ItemWithStream()
- {
- Name = folder.Name
- };
- await Task.Run(() => cb(new[] { folderWithStream }));
- }, false);
- }
- public void SaveFilePanelAsync(string title, string directory, string defaultName, ExtensionFilter[] extensions, Action<ItemWithStream> cb)
- {
- UnityEngine.WSA.Application.InvokeOnUIThread(async () =>
- {
- var filePicker = new Windows.Storage.Pickers.FileSavePicker();
- filePicker.SuggestedFileName = defaultName;
- foreach (var extension in extensions)
- {
- filePicker.FileTypeChoices.Add(extension.Name, extension.Extensions);
- }
- var file = await filePicker.PickSaveFileAsync();
- var fileWithStream = new ItemWithStream()
- {
- Name = file.Name
- };
- await Task.Run(() => cb(fileWithStream));
- }, false);
- }
- private static async Task<Stream> ReadStorageFile(StorageFile storageFile)
- {
- return await storageFile.OpenStreamForReadAsync();
- }
- }
- }
- #endif
|