1#if UNITY_WEBGL && !UNITY_EDITOR
3using System.Collections;
4using System.Collections.Generic;
6using System.Runtime.InteropServices;
7using Newtonsoft.Json.Linq;
12 public class StandloneFileBrowserWebGLHelper : MonoBehaviour
14 public Action<IList<ItemWithStream>> MultipleFilesCallback;
15 public Action<ItemWithStream> SingleFileCallback;
17 private IEnumerator InvokeCallback(
string json)
19 var browserFiles = JArray.Parse(json);
20 var browserItemsWithStream =
new ItemWithStream[browserFiles.Count];
21 if (browserFiles.Count > 0)
23 for (var i = 0; i < browserFiles.Count; i++)
25 var browserFile = browserFiles[i];
26 var loader =
new WWW(browserFile.SelectToken(
"url").ToString());
28 if (
string.IsNullOrWhiteSpace(loader.error))
30 browserItemsWithStream[i] =
new ItemWithStream
32 Name = browserFile.SelectToken(
"name").ToString(),
33 Stream =
new MemoryStream(loader.bytes)
38 throw new Exception(loader.error);
42 if (MultipleFilesCallback !=
null) {
43 MultipleFilesCallback.Invoke(browserItemsWithStream);
44 }
else if (SingleFileCallback !=
null && browserItemsWithStream.Length > 0) {
45 SingleFileCallback.Invoke(browserItemsWithStream[0]);
47 SingleFileCallback =
null;
48 MultipleFilesCallback =
null;
53 public class StandaloneFileBrowserWebGL : IStandaloneFileBrowser<ItemWithStream>
55 [DllImport(
"__Internal")]
56 private static extern void UploadFile(
string gameObjectName,
string methodName,
string filter,
bool multiple);
58 [DllImport(
"__Internal")]
59 private static extern void DownloadFile(
string gameObjectName,
string methodName,
string filename,
byte[] byteArray,
int byteArraySize);
61 private bool _processing;
65 public StandaloneFileBrowserWebGL()
69 public IList<ItemWithStream> OpenFilePanel(
string title,
string directory, ExtensionFilter[] extensions,
bool multiselect)
71 throw new NotSupportedException();
74 public IList<ItemWithStream> OpenFolderPanel(
string title,
string directory,
bool multiselect)
76 throw new NotSupportedException();
79 public ItemWithStream SaveFilePanel(
string title,
string directory,
string defaultName, ExtensionFilter[] extensions)
81 throw new NotSupportedException();
84 public void OpenFilePanelAsync(
string title,
string directory, ExtensionFilter[] extensions,
bool multiselect, Action<IList<ItemWithStream>> cb)
86 var helper =
new GameObject(Guid.NewGuid().ToString()).AddComponent<StandloneFileBrowserWebGLHelper>();
87 helper.MultipleFilesCallback = cb;
88 UploadFile(helper.name,
"InvokeCallback", GetFilterFromFileExtensionList(extensions), multiselect);
91 public void OpenFolderPanelAsync(
string title,
string directory,
bool multiselect, Action<IList<ItemWithStream>> cb)
93 throw new NotSupportedException();
96 public void SaveFilePanelAsync(
string title,
string directory,
string defaultName, ExtensionFilter[] extensions, Action<ItemWithStream> cb)
102 var helper =
new GameObject(Guid.NewGuid().ToString()).AddComponent<StandloneFileBrowserWebGLHelper>();
103 helper.SingleFileCallback = cb;
104 DownloadFile(helper.name,
"InvokeCallback", defaultName, Data, Data.Length);
107 private static string GetFilterFromFileExtensionList(ExtensionFilter[] extensions)
109 var filterString =
"";
110 var addedFormats =
new List<string>();
111 if (extensions !=
null)
113 foreach (var extension
in extensions)
115 foreach (var format
in extension.Extensions)
117 if (format ==
"*.*" || format ==
".*" || format ==
"*") {
120 if (filterString !=
"")
122 filterString +=
", ";
124 if (!addedFormats.Contains(format)) {
125 filterString +=
"." + (format[0] ==
'.' ? format.Substring(1) : format);
126 addedFormats.Add(format);