diff --git a/StandaloneFileBrowser/Plugins/StandaloneFileBrowser.jslib b/StandaloneFileBrowser/Plugins/StandaloneFileBrowser.jslib index 48c8739..ca09823 100644 --- a/StandaloneFileBrowser/Plugins/StandaloneFileBrowser.jslib +++ b/StandaloneFileBrowser/Plugins/StandaloneFileBrowser.jslib @@ -54,6 +54,39 @@ var StandaloneFileBrowserWebGLPlugin = { } }, + // Save file from span of bytes + // DownloadFile method does not open SaveFileDialog like standalone builds, its just allows user to download file + // gameObjectNamePtr: Unique GameObject name. Required for calling back unity with SendMessage. + // methodNamePtr: Callback method name on given GameObject. + // filenamePtr: Filename with extension + // byteArray: byte[] to pull a span from + // byteSpanStart: start index of span + // byteSpanSize: length of span + DownloadFileSpan: function(gameObjectNamePtr, methodNamePtr, filenamePtr, byteArray, byteSpanStart, byteSpanSize) { + gameObjectName = UTF8ToString(gameObjectNamePtr); + methodName = UTF8ToString(methodNamePtr); + filename = UTF8ToString(filenamePtr); + + var bytes = new Uint8Array(byteSpanSize); + for (var i = 0; i < byteSpanSize; i++) { + bytes[i] = HEAPU8[byteArray + byteSpanStart + i]; + } + + var downloader = window.document.createElement('a'); + downloader.setAttribute('id', gameObjectName); + downloader.href = window.URL.createObjectURL(new Blob([bytes], { type: 'application/octet-stream' })); + downloader.download = filename; + document.body.appendChild(downloader); + + document.onmouseup = function() { + downloader.click(); + document.body.removeChild(downloader); + document.onmouseup = null; + + SendMessage(gameObjectName, methodName); + } + }, + // Save file // DownloadFile method does not open SaveFileDialog like standalone builds, its just allows user to download file // gameObjectNamePtr: Unique GameObject name. Required for calling back unity with SendMessage. diff --git a/StandaloneFileBrowser/package.json b/StandaloneFileBrowser/package.json index 9b3c951..2ca4129 100644 --- a/StandaloneFileBrowser/package.json +++ b/StandaloneFileBrowser/package.json @@ -3,7 +3,7 @@ "description": "Native open and save UIs for Windows, MacOS, Linux, WebGL", "name": "com.virtualmaker.filebrowser", "unity": "2019.4", - "version": "1.0.3", + "version": "1.0.5", "category": "utilities", "dependencies": {} }