Tanoda
WebGLFileUploadManager.cs
Go to the documentation of this file.
1using UnityEngine;
2using System.Collections;
3using System.Collections.Generic;
4using System;
5using System.Runtime.InteropServices;
6
8{
15 public static class WebGLFileUploadManager
16 {
20 public static event Action<UploadedFileInfoV2[]> onFileUploaded;
21
32 public static bool Show (bool isDropInput = false, bool isOverlay = false, int x = -1, int y = -1, int width = -1, int height = -1)
33 {
34 #if UNITY_WEBGL && !UNITY_EDITOR
35 bool success = WebGLFileUploadManager.Unity_FileUploadManager_Show(isDropInput, isOverlay, x, y, width, height);
36 WebGLFileUploadManager.Unity_FileUploadManager_SetCallback (WebGLFileUploadManager.Callback);
37 return success;
38 #else
39 return false;
40 #endif
41 }
42
50 public static bool PopupDialog (string titleText = "", string uploadBtnText = "", string cancelBtnText = "")
51 {
52 #if UNITY_WEBGL && !UNITY_EDITOR
53 if (Screen.fullScreen)
54 {
55 if( Unity_FileUploadManager_IsRunningOnEdgeBrowser() ){
56 Screen.fullScreen = false;
57 }else{
58 Unity_FileUploadManager_HideUnityScreenIfHtmlOverlayCant();
59 }
60 }
61 bool success = WebGLFileUploadManager.Unity_FileUploadManager_PopupDialog(titleText, uploadBtnText, cancelBtnText);
62 WebGLFileUploadManager.Unity_FileUploadManager_SetCallback (WebGLFileUploadManager.Callback);
63 return success;
64 #else
65 return false;
66 #endif
67 }
68
72 public static void Hide ()
73 {
74 #if UNITY_WEBGL && !UNITY_EDITOR
75 WebGLFileUploadManager.Unity_FileUploadManager_Hide ();
76 #endif
77 }
78
79 public static void Dispose ()
80 {
81 #if UNITY_WEBGL && !UNITY_EDITOR
82 WebGLFileUploadManager.Unity_FileUploadManager_Dispose();
83 #endif
84 }
85
86 public static void Enable ()
87 {
88 #if UNITY_WEBGL && !UNITY_EDITOR
89 WebGLFileUploadManager.Unity_FileUploadManager_Enable ();
90 #endif
91 }
92
93 public static void Disable ()
94 {
95 #if UNITY_WEBGL && !UNITY_EDITOR
96 WebGLFileUploadManager.Unity_FileUploadManager_Disable();
97 #endif
98 }
99
100 public static void SetDescription (string description)
101 {
102 #if UNITY_WEBGL && !UNITY_EDITOR
103 WebGLFileUploadManager.Unity_FileUploadManager_SetDescription (description);
104 #endif
105 }
106
107 public static void SetAllowedFileName (string filenameReg)
108 {
109 #if UNITY_WEBGL && !UNITY_EDITOR
110 WebGLFileUploadManager.Unity_FileUploadManager_SetAllowedFileName (filenameReg);
111 #endif
112 }
113 public static void SetImageEncodeSetting (bool enable, int threshold = -1)
114 {
115 #if UNITY_WEBGL && !UNITY_EDITOR
116 WebGLFileUploadManager.Unity_FileUploadManager_SetImageEncodeSetting (enable, threshold);
117 #endif
118 }
119
120 public static void SetImageShrinkingSize (int width = 640, int height = 480)
121 {
122 #if UNITY_WEBGL && !UNITY_EDITOR
123 WebGLFileUploadManager.Unity_FileUploadManager_SetImageShrinkingSize (width, height);
124 #endif
125 }
126
127 public static bool IsDropInput {
128 get {
129 #if UNITY_WEBGL && !UNITY_EDITOR
130 return WebGLFileUploadManager.Unity_FileUploadManager_IsDropInput ();
131 #else
132 return false;
133 #endif
134 }
135 }
136
137 public static bool IsOverlay {
138 get {
139 #if UNITY_WEBGL && !UNITY_EDITOR
140 return WebGLFileUploadManager.Unity_FileUploadManager_IsOverlay ();
141 #else
142 return false;
143 #endif
144 }
145 }
146
147 public static bool IsPopupDialog {
148 get {
149 #if UNITY_WEBGL && !UNITY_EDITOR
150 return WebGLFileUploadManager.Unity_FileUploadManager_IsPopupDialog ();
151 #else
152 return false;
153 #endif
154 }
155 }
156
157 public static void SetDebug (bool value)
158 {
159 #if UNITY_WEBGL && !UNITY_EDITOR
160 WebGLFileUploadManager.Unity_FileUploadManager_SetDebug (value);
161 #endif
162 }
163
164 public static OS_NAME getOS {
165 get {
166 #if UNITY_WEBGL && !UNITY_EDITOR
167 string osName = Unity_FileUploadManager_GetOS();
168 OS_NAME platform;
169
170 switch (osName) {
171 case "iOS":
172 platform = OS_NAME.iOS;
173 break;
174 case "Mac":
175 platform = OS_NAME.Mac;
176 break;
177 case "Android":
178 platform = OS_NAME.Android;
179 break;
180 case "Windows":
181 platform = OS_NAME.Windows;
182 break;
183 case "Chrome OS":
184 platform = OS_NAME.ChromeOS;
185 break;
186 case "FireFox OS":
187 platform = OS_NAME.FireFoxOS;
188 break;
189 default:
190 platform = OS_NAME.UNKNOWN;
191 break;
192 }
193 return platform;
194 #else
195 return OS_NAME.UNKNOWN;
196 #endif
197 }
198 }
199
200 public static bool IsMOBILE {
201 get {
202 #if UNITY_WEBGL && !UNITY_EDITOR
203 return Unity_FileUploadManager_IsMobile();
204 #else
205 return false;
206 #endif
207 }
208 }
209
210 public static bool IsIOS {
211 get {
212 #if UNITY_WEBGL && !UNITY_EDITOR
213 return (getOS == OS_NAME.iOS);
214 #else
215 return false;
216 #endif
217 }
218 }
219
220 public static bool IsMac {
221 get {
222 #if UNITY_WEBGL && !UNITY_EDITOR
223 return (getOS == OS_NAME.Mac);
224 #else
225 return false;
226 #endif
227 }
228 }
229
230 public static bool IsAndroid {
231 get {
232 #if UNITY_WEBGL && !UNITY_EDITOR
233 return (getOS == OS_NAME.Android);
234 #else
235 return false;
236 #endif
237 }
238 }
239
240 public static bool IsWindows {
241 get {
242 #if UNITY_WEBGL && !UNITY_EDITOR
243 return (getOS == OS_NAME.Windows);
244 #else
245 return false;
246 #endif
247 }
248 }
249
250 public static bool IsChromeOS {
251 get {
252 #if UNITY_WEBGL && !UNITY_EDITOR
253 return (getOS == OS_NAME.ChromeOS);
254 #else
255 return false;
256 #endif
257 }
258 }
259
260 public static bool IsFireFoxOS {
261 get {
262 #if UNITY_WEBGL && !UNITY_EDITOR
263 return (getOS == OS_NAME.FireFoxOS);
264 #else
265 return false;
266 #endif
267 }
268 }
269
270 public static string GetUserAgent
271 {
272 get {
273 #if UNITY_WEBGL && !UNITY_EDITOR
274 return Unity_FileUploadManager_GetUserAgent();
275 #else
276 return "";
277 #endif
278 }
279 }
280
281 #if UNITY_WEBGL
282
283 [DllImport ("__Internal")]
284 private static extern bool Unity_FileUploadManager_Show (bool isDropInput, bool isOverlay, int x, int y, int width, int height);
285
286 [DllImport ("__Internal")]
287 private static extern bool Unity_FileUploadManager_PopupDialog (string title, string uploadBtnText, string cancelBtnText);
288
289 [DllImport ("__Internal")]
290 private static extern void Unity_FileUploadManager_Hide ();
291
292 [DllImport ("__Internal")]
293 private static extern void Unity_FileUploadManager_Dispose ();
294
295 [DllImport ("__Internal")]
296 private static extern void Unity_FileUploadManager_Enable ();
297
298 [DllImport ("__Internal")]
299 private static extern void Unity_FileUploadManager_Disable ();
300
301 [DllImport ("__Internal")]
302 private static extern void Unity_FileUploadManager_SetCallback (Action<string> callback);
303
304 [AOT.MonoPInvokeCallback (typeof(Action<string>))]
305 private static void Callback (string fileUploadDataJSON)
306 {
307 Debug.Log ("Callback called " + fileUploadDataJSON);
308
309 if(onFileUploaded == null) {
310 Debug.Log ("onFileUploaded == null");
311 return;
312 }
313
314 UploadedFileInfoV2[] files;
315 if (!string.IsNullOrEmpty(fileUploadDataJSON))
316 {
317 Debug.Log($"fileUploadDataJSON");
318 //Debug.Log ("JsonUtility.FromJson");
319
320 //files = JsonUtility.FromJson<FileUploadResultV2>(fileUploadDataJSON).files;
321
322 var filePathIndex = fileUploadDataJSON.IndexOf("filePath\":\"") + 11;
323 var fileNameIndex = fileUploadDataJSON.IndexOf("name\":\"") + 7;
324 //Debug.Log ($"filePathIndex: {filePathIndex}");
325 var filePath = fileUploadDataJSON.Remove(0, filePathIndex);
326 var fileName = fileUploadDataJSON.Remove(0, fileNameIndex);
327 Debug.Log($"filePath1: {filePath}");
328 filePath = filePath.Remove(filePath.IndexOf('"')).Replace("\"", "");
329 fileName = fileName.Remove(fileName.IndexOf('"')).Replace("\"", "");
330 Debug.Log($"filePath2: {filePath}");
331 Debug.Log($"fileName: {fileName}");
332 files = new[]
333 {
335 {
336 filePath = filePath,
337 isSuccess = fileUploadDataJSON.Contains("true"),
338 name = fileName
339 }
340 };
341
342 Debug.Log(files);
343 //Debug.Log ("after JsonUtility.FromJson");
344 onFileUploaded.Invoke(files);
345 }
346 else
347 {
348 files = new UploadedFileInfoV2[0] { };
349 onFileUploaded.Invoke(files);
350 }
351
352 //Debug.Log (onFileUploaded);
353 }
354
355 [DllImport ("__Internal")]
356 private static extern void Unity_FileUploadManager_SetDescription (string str);
357
358 [DllImport ("__Internal")]
359 private static extern void Unity_FileUploadManager_SetAllowedFileName (string str);
360
361 [DllImport ("__Internal")]
362 private static extern void Unity_FileUploadManager_SetImageShrinkingSize (int width, int height);
363
364 [DllImport ("__Internal")]
365 private static extern void Unity_FileUploadManager_SetImageEncodeSetting (bool enable, int threshold);
366
367 [DllImport ("__Internal")]
368 private static extern bool Unity_FileUploadManager_IsDropInput ();
369
370 [DllImport ("__Internal")]
371 private static extern bool Unity_FileUploadManager_IsOverlay ();
372
373 [DllImport ("__Internal")]
374 private static extern bool Unity_FileUploadManager_IsPopupDialog ();
375
376 [DllImport ("__Internal")]
377 private static extern void Unity_FileUploadManager_SetDebug (bool value);
378
379 [DllImport ("__Internal")]
380 private static extern string Unity_FileUploadManager_GetOS ();
381
382 [DllImport ("__Internal")]
383 private static extern bool Unity_FileUploadManager_IsMobile ();
384
385 [DllImport ("__Internal")]
386 private static extern string Unity_FileUploadManager_GetUserAgent ();
387
388 [DllImport("__Internal")]
389 private static extern void Unity_FileUploadManager_HideUnityScreenIfHtmlOverlayCant();
390
391 [DllImport("__Internal")]
392 private static extern bool Unity_FileUploadManager_IsRunningOnEdgeBrowser();
393 #endif
394
395 [Serializable]
396 class FileUploadResult
397 {
398 public UploadedFileInfo[] files = new UploadedFileInfo[] {};
399 }
400
401 [Serializable]
402 struct FileUploadResultV2
403 {
404 public UploadedFileInfoV2[] files;
405 }
406 }
407
408 [Serializable]
409 public class UploadedFileInfo
410 {
411 public string name = "";
412 public string type = "";
413 public int size = 0;
414 public int lastModified = 0;
415 public string filePath = "";
416 public bool isSuccess = false;
417 public int errorCode = 0;
418 }
419
420 [Serializable]
421 public struct UploadedFileInfoV2
422 {
423 public string name;
424 public string type;
425 public int size;
426 public int lastModified;
427 public string filePath;
428 public bool isSuccess;
429 public int errorCode;
430 }
431
432 public enum ERROR_CODE :int
433 {
434 NONE = 0,
435 NOT_FOUND_ERR = 1,
436 SECURITY_ERR = 2,
437 ABORT_ERR = 3,
439 ENCODING_ERR = 5,
440 FS_IO_ERRO = 6,
442 }
443
445}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
delegate void Callback()