StandaloneFileBrowser.jslib 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. var StandaloneFileBrowserWebGLPlugin = {
  2. oldGameObjectName: '',
  3. // Open file.
  4. // gameObjectNamePtr: Unique GameObject name. Required for calling back unity with SendMessage.
  5. // methodNamePtr: Callback method name on given GameObject.
  6. // filter: Filter files. Example filters:
  7. // Match all image files: "image/*"
  8. // Match all video files: "video/*"
  9. // Match all audio files: "audio/*"
  10. // Custom: ".plist, .xml, .yaml"
  11. // multiselect: Allows multiple file selection
  12. UploadFile: function(gameObjectNamePtr, methodNamePtr, filterPtr, multiselect) {
  13. gameObjectName = Pointer_stringify(gameObjectNamePtr);
  14. methodName = Pointer_stringify(methodNamePtr);
  15. filter = Pointer_stringify(filterPtr);
  16. var fileInput = document.getElementById(this.oldGameObjectName);
  17. if (fileInput) {
  18. document.body.removeChild(fileInput);
  19. }
  20. fileInput = document.createElement('input');
  21. fileInput.setAttribute('id', gameObjectName);
  22. fileInput.setAttribute('type', 'file');
  23. fileInput.setAttribute('class', 'standalone-file-picker');
  24. if (multiselect) {
  25. fileInput.setAttribute('multiple', 'multiple');
  26. }
  27. if (filter) {
  28. fileInput.setAttribute('accept', filter);
  29. }
  30. fileInput.onclick = function(event) {
  31. event.stopPropagation();
  32. this.value = null;
  33. };
  34. fileInput.onchange = function(event) {
  35. event.stopPropagation();
  36. var urls = [];
  37. for (var i = 0; i < event.target.files.length; i++) {
  38. urls.push({
  39. "name": event.target.files[i].name,
  40. "url": URL.createObjectURL(event.target.files[i])
  41. });
  42. }
  43. SendMessage(gameObjectName, methodName, JSON.stringify(urls));
  44. document.body.removeChild(fileInput);
  45. };
  46. document.body.appendChild(fileInput);
  47. fileInput.focus();
  48. fileInput.click();
  49. this.oldGameObjectName = gameObjectName;
  50. },
  51. // Save file
  52. // DownloadFile method does not open SaveFileDialog like standalone builds, its just allows user to download file
  53. // gameObjectNamePtr: Unique GameObject name. Required for calling back unity with SendMessage.
  54. // methodNamePtr: Callback method name on given GameObject.
  55. // filenamePtr: Filename with extension
  56. // byteArray: byte[]
  57. // byteArraySize: byte[].Length
  58. DownloadFile: function(gameObjectNamePtr, methodNamePtr, filenamePtr, byteArray, byteArraySize) {
  59. gameObjectName = Pointer_stringify(gameObjectNamePtr);
  60. methodName = Pointer_stringify(methodNamePtr);
  61. filename = Pointer_stringify(filenamePtr);
  62. var bytes = new Uint8Array(byteArraySize);
  63. for (var i = 0; i < byteArraySize; i++) {
  64. bytes[i] = HEAPU8[byteArray + i];
  65. }
  66. var downloader = window.document.createElement('a');
  67. downloader.setAttribute('id', gameObjectName);
  68. downloader.href = window.URL.createObjectURL(new Blob([bytes], {
  69. type: 'application/octet-stream'
  70. }));
  71. downloader.download = filename;
  72. document.body.appendChild(downloader);
  73. document.onmouseup = function() {
  74. downloader.click();
  75. document.body.removeChild(downloader);
  76. document.onmouseup = null;
  77. SendMessage(gameObjectName, methodName);
  78. };
  79. }
  80. };
  81. mergeInto(LibraryManager.library, StandaloneFileBrowserWebGLPlugin);