Tanoda
StandaloneFileBrowserWebGL.cs
Go to the documentation of this file.
1#if UNITY_WEBGL && !UNITY_EDITOR
2using System;
3using System.Collections;
4using System.Collections.Generic;
5using System.IO;
6using System.Runtime.InteropServices;
7using Newtonsoft.Json.Linq;
8using UnityEngine;
9
10namespace TriLibCore.SFB
11{
12 public class StandloneFileBrowserWebGLHelper : MonoBehaviour
13 {
14 public Action<IList<ItemWithStream>> MultipleFilesCallback;
15 public Action<ItemWithStream> SingleFileCallback;
16
17 private IEnumerator InvokeCallback(string json)
18 {
19 var browserFiles = JArray.Parse(json);
20 var browserItemsWithStream = new ItemWithStream[browserFiles.Count];
21 if (browserFiles.Count > 0)
22 {
23 for (var i = 0; i < browserFiles.Count; i++)
24 {
25 var browserFile = browserFiles[i];
26 var loader = new WWW(browserFile.SelectToken("url").ToString());
27 yield return loader;
28 if (string.IsNullOrWhiteSpace(loader.error))
29 {
30 browserItemsWithStream[i] = new ItemWithStream
31 {
32 Name = browserFile.SelectToken("name").ToString(),
33 Stream = new MemoryStream(loader.bytes)
34 };
35 }
36 else
37 {
38 throw new Exception(loader.error);
39 }
40 }
41 }
42 if (MultipleFilesCallback != null) {
43 MultipleFilesCallback.Invoke(browserItemsWithStream);
44 } else if (SingleFileCallback != null && browserItemsWithStream.Length > 0) {
45 SingleFileCallback.Invoke(browserItemsWithStream[0]);
46 }
47 SingleFileCallback = null;
48 MultipleFilesCallback = null;
49 Destroy(gameObject);
50 }
51 }
52
53 public class StandaloneFileBrowserWebGL : IStandaloneFileBrowser<ItemWithStream>
54 {
55 [DllImport("__Internal")]
56 private static extern void UploadFile(string gameObjectName, string methodName, string filter, bool multiple);
57
58 [DllImport("__Internal")]
59 private static extern void DownloadFile(string gameObjectName, string methodName, string filename, byte[] byteArray, int byteArraySize);
60
61 private bool _processing;
62
63 public byte[] Data;
64
65 public StandaloneFileBrowserWebGL()
66 {
67 }
68
69 public IList<ItemWithStream> OpenFilePanel(string title, string directory, ExtensionFilter[] extensions, bool multiselect)
70 {
71 throw new NotSupportedException();
72 }
73
74 public IList<ItemWithStream> OpenFolderPanel(string title, string directory, bool multiselect)
75 {
76 throw new NotSupportedException();
77 }
78
79 public ItemWithStream SaveFilePanel(string title, string directory, string defaultName, ExtensionFilter[] extensions)
80 {
81 throw new NotSupportedException();
82 }
83
84 public void OpenFilePanelAsync(string title, string directory, ExtensionFilter[] extensions, bool multiselect, Action<IList<ItemWithStream>> cb)
85 {
86 var helper = new GameObject(Guid.NewGuid().ToString()).AddComponent<StandloneFileBrowserWebGLHelper>();
87 helper.MultipleFilesCallback = cb;
88 UploadFile(helper.name, "InvokeCallback", GetFilterFromFileExtensionList(extensions), multiselect);
89 }
90
91 public void OpenFolderPanelAsync(string title, string directory, bool multiselect, Action<IList<ItemWithStream>> cb)
92 {
93 throw new NotSupportedException();
94 }
95
96 public void SaveFilePanelAsync(string title, string directory, string defaultName, ExtensionFilter[] extensions, Action<ItemWithStream> cb)
97 {
98 if (Data == null)
99 {
100 return;
101 }
102 var helper = new GameObject(Guid.NewGuid().ToString()).AddComponent<StandloneFileBrowserWebGLHelper>();
103 helper.SingleFileCallback = cb;
104 DownloadFile(helper.name, "InvokeCallback", defaultName, Data, Data.Length);
105 }
106
107 private static string GetFilterFromFileExtensionList(ExtensionFilter[] extensions)
108 {
109 var filterString = "";
110 var addedFormats = new List<string>();
111 if (extensions != null)
112 {
113 foreach (var extension in extensions)
114 {
115 foreach (var format in extension.Extensions)
116 {
117 if (format == "*.*" || format == ".*" || format == "*") {
118 continue;
119 }
120 if (filterString != "")
121 {
122 filterString += ", ";
123 }
124 if (!addedFormats.Contains(format)) {
125 filterString += "." + (format[0] == '.' ? format.Substring(1) : format);
126 addedFormats.Add(format);
127 }
128 }
129 }
130 }
131 return filterString;
132 }
133 }
134}
135#endif