StandaloneFileBrowserWebGL.cs 5.1 KB

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