StandaloneFileBrowser.jslib 3.2 KB

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