diff --git a/Assets/WebGLTemplates/MyIndex/index.html b/Assets/WebGLTemplates/MyIndex/index.html index 0be9841..140269a 100644 --- a/Assets/WebGLTemplates/MyIndex/index.html +++ b/Assets/WebGLTemplates/MyIndex/index.html @@ -109,254 +109,178 @@ progressBarFull.style.width = 100 * progress + "%"; }).then((unityInstance) => { loadingBar.style.display = "none"; + + // 保存Unity实例引用供其他函数使用 + window.unityInstance = unityInstance; + + // 修复全屏按钮点击事件 fullscreenButton.onclick = () => { + // 优先使用Unity内置的全屏方法 unityInstance.SetFullscreen(1); + + // 如果Unity方法不生效,尝试使用浏览器全屏API + if (container.requestFullscreen) { + container.requestFullscreen(); + } else if (container.webkitRequestFullscreen) { + container.webkitRequestFullscreen(); + } else if (container.msRequestFullscreen) { + container.msRequestFullscreen(); + } + }; + + // 添加全屏状态变化事件监听器 + function handleFullscreenChange() { + if (document.fullscreenElement || document.webkitFullscreenElement || document.msFullscreenElement) { + // 全屏状态下的样式调整 + document.body.style.margin = '0'; + document.body.style.overflow = 'hidden'; + container.style.width = '100%'; + container.style.height = '100%'; + canvas.style.width = '100%'; + canvas.style.height = '100%'; + } else { + // 退出全屏状态的样式恢复 + document.body.style.margin = ''; + document.body.style.overflow = ''; + container.style.width = ''; + container.style.height = ''; + canvas.style.width = '1280px'; + canvas.style.height = '720px'; + } + } + + // 监听不同浏览器的全屏变化事件 + document.addEventListener('fullscreenchange', handleFullscreenChange); + document.addEventListener('webkitfullscreenchange', handleFullscreenChange); + document.addEventListener('msfullscreenchange', handleFullscreenChange); + + // 修复imgChange函数中的instance引用 + window.imgChange = function(obj) { + console.log("3"); + var file = document.getElementById("_ef"); + var imgUrl = window.URL.createObjectURL(file.files[0]); + if (window.unityInstance != null) { + window.unityInstance.SendMessage("Web", "CallBack", imgUrl); + } }; }).catch((message) => { alert(message); }); }; - var inputObj; - - function Test() { - console.log("1"); - if (inputObj != null) document.body.removeChild(inputObj); - inputObj = document.createElement('input'); - inputObj.setAttribute('id', '_ef'); - inputObj.setAttribute('type', 'file'); - inputObj.setAttribute("style", 'visibility:hidden'); - document.body.appendChild(inputObj); - document.addEventListener('input', imgChange); - var file = document.getElementById("_ef"); - file.click(); - file.value; - console.log("2"); - } - - function imgChange(obj) { - console.log("3"); - var file = document.getElementById("_ef"); - var imgUrl = window.URL.createObjectURL(file.files[0]); - if (instance != null) { - instance.SendMessage("Web", "CallBack", imgUrl); - } - }; + + // 修复Test函数,确保只添加一次事件监听器 + window.Test = function() { + console.log("1"); + if (inputObj != null) document.body.removeChild(inputObj); + inputObj = document.createElement('input'); + inputObj.setAttribute('id', '_ef'); + inputObj.setAttribute('type', 'file'); + inputObj.setAttribute("style", 'visibility:hidden'); + document.body.appendChild(inputObj); + + // 移除之前的事件监听器,避免重复添加 + document.removeEventListener('input', window.imgChange); + document.addEventListener('input', window.imgChange); + + var file = document.getElementById("_ef"); + file.click(); + file.value; + console.log("2"); + }; //添加功能--------- function HtmlDownloadWord(bytes, reportdata) { - var blob = new Blob([bytes]); - var url = window.URL.createObjectURL(blob); - generate(url, reportdata); - } - - function loadFile(url, callback) { - PizZipUtils.getBinaryContent(url, callback); - } - - //处理base64数据 - const base64Regex = - /^data:image\/(png|jpg|svg|svg\+xml);base64,/; - const validBase64 = - /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/; - - function base64Parser(dataURL) { - if ( - typeof dataURL !== "string" || - !base64Regex.test(dataURL) - ) { - return false; - } - - const stringBase64 = dataURL.replace(base64Regex, ""); - - if (!validBase64.test(stringBase64)) { - throw new Error( - "Error parsing base64 data, your data contains invalid characters" - ); - } - - // For nodejs, return a Buffer - if (typeof Buffer !== "undefined" && Buffer.from) { - return Buffer.from(stringBase64, "base64"); - } - - // For browsers, return a string (of binary content) : - const binaryString = window.atob(stringBase64); - const len = binaryString.length; - const bytes = new Uint8Array(len); - for (let i = 0; i < len; i++) { - const ascii = binaryString.charCodeAt(i); - bytes[i] = ascii; - } - return bytes.buffer; - } const imageOptions = { - getImage(tag) { - return base64Parser(tag); - }, - getSize() { - console.log("大小已被调用"); - return [384, 216]; - }, - }; - - function generate(url, reportdata) { - loadFile( - url, - function (error, content) { - if (error) { - throw error; - } - - //先处理unity传入的数据得到base64 - reportdata = (reportdata.replace(/<(.|\n)*?>/g, '') || ' ') - .replace(/</g, '<') - .replace(/>/g, '>'); - reportdata = JSON.parse(reportdata) - - - //var imageBytes = reportdata.imageData; // 图片字节数组数据 - - - - // 将图片数据转换为 base64 格式 - - //var imageBase64 = btoa(String.fromCharCode.apply(null, imageBytes)); - - //imageBase64 = "data:image/png;base64," + imageBase64; - - - - //console.log(imageBase64); - var imageModule = new ImageModule(imageOptions); - var zip = new PizZip(content); - var doc = new window.docxtemplater(zip, { - paragraphLoop: true, - linebreaks: true, - modules: [imageModule] - }); - - - //const image = doc.Media.addImage(doc, imageBytes, 230, 230); - - - doc.compile(); - - - - //const data = { - - // eid: reportdata.eid, - - // name: reportdata.name, - - // scroe: reportdata.scroe, - - // image: imageBase64, - - //} - - //渲染模板 - doc.render(reportdata); - var out = doc.getZip().generate({ - type: "blob", - mimeType: - "application/vnd.openxmlformats-officedocument.wordprocessingml.document", - compression: "DEFLATE", - }); - saveAs(out, "实验报告.docx"); - }); - } document.body.appendChild(script); - + \ No newline at end of file diff --git a/Assets/_Scenes/网页端打包须知.txt b/Assets/_Scenes/网页端打包须知.txt index f7bd235..df89058 100644 --- a/Assets/_Scenes/网页端打包须知.txt +++ b/Assets/_Scenes/网页端打包须知.txt @@ -1 +1 @@ -把Assets同级文件夹直接复制一份到打包程序里,里边是下载报告用的模板 +把Assets同级文件夹“Data”直接复制一份到打包程序里,里边是下载报告用的模板