StandaloneFileBrowserWebGL.cs 5.5 KB

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