123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- var StandaloneFileBrowserWebGLPlugin = {
- // Open file.
- // gameObjectNamePtr: Unique GameObject name. Required for calling back unity with SendMessage.
- // methodNamePtr: Callback method name on given GameObject.
- // filter: Filter files. Example filters:
- // Match all image files: "image/*"
- // Match all video files: "video/*"
- // Match all audio files: "audio/*"
- // Custom: ".plist, .xml, .yaml"
- // multiselect: Allows multiple file selection
- UploadFile: function(gameObjectNamePtr, methodNamePtr, filterPtr, multiselect) {
- gameObjectName = Pointer_stringify(gameObjectNamePtr);
- methodName = Pointer_stringify(methodNamePtr);
- filter = Pointer_stringify(filterPtr);
- // Delete if element exist
- var fileInput = document.getElementById(gameObjectName)
- if (fileInput) {
- document.body.removeChild(fileInput);
- }
- fileInput = document.createElement('input');
- fileInput.setAttribute('id', gameObjectName);
- fileInput.setAttribute('type', 'file');
- fileInput.setAttribute('style','display:none;');
- fileInput.setAttribute('style','visibility:hidden;');
- if (multiselect) {
- fileInput.setAttribute('multiple', '');
- }
- if (filter) {
- fileInput.setAttribute('accept', filter);
- }
- fileInput.onclick = function (event) {
- // File dialog opened
- this.value = null;
- };
- fileInput.onchange = function (event) {
- // multiselect works
-
- for (var i = 0; i < event.target.files.length; i++) {
- //urls.push(URL.createObjectURL(event.target.files[i]));
- var reader = new FileReader();
- reader.readAsDataURL(event.target.files[i]);
- var url = URL.createObjectURL(event.target.files[i]);
- var f_name= event.target.files[i].name;
- var base64data;
- reader.onload = function () {
- var urls = [];
- base64data = reader.result;
- console.log(base64data);
- urls.push({
- "url": url,
- "file_name": f_name,
- "base64": base64data
- });
- //console.log('js: '+JSON.stringify(urls));
- SendMessage(gameObjectName, methodName, JSON.stringify(urls));
- };
- reader.onerror = function (error) {
- console.log('Error: ', error);
- //base64data= error;
- };
-
- /*urls.push({
- "url": URL.createObjectURL(event.target.files[i]),
- "file_name": event.target.files[i].name,
- "base64": base64data
- }); */
- }
- // File selected
- //SendMessage(gameObjectName, methodName, JSON.stringify(urls));
- // Remove after file selected
- document.body.removeChild(fileInput);
- }
- document.body.appendChild(fileInput);
- document.onmouseup = function() {
- fileInput.click();
- document.onmouseup = null;
- }
- },
- // 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.
- // methodNamePtr: Callback method name on given GameObject.
- // filenamePtr: Filename with extension
- // byteArray: byte[]
- // byteArraySize: byte[].Length
- DownloadFile: function(gameObjectNamePtr, methodNamePtr, filenamePtr, byteArray, byteArraySize) {
- gameObjectName = Pointer_stringify(gameObjectNamePtr);
- methodName = Pointer_stringify(methodNamePtr);
- filename = Pointer_stringify(filenamePtr);
- var bytes = new Uint8Array(byteArraySize);
- for (var i = 0; i < byteArraySize; i++) {
- bytes[i] = HEAPU8[byteArray + 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);
- }
- }
- };
- mergeInto(LibraryManager.library, StandaloneFileBrowserWebGLPlugin);
|