提交修改,打包须知。index文件

This commit is contained in:
李浩 2025-06-23 16:38:25 +08:00
parent 7675674e93
commit 1bd932f48f
2 changed files with 73 additions and 149 deletions

View File

@ -109,252 +109,176 @@
progressBarFull.style.width = 100 * progress + "%"; progressBarFull.style.width = 100 * progress + "%";
}).then((unityInstance) => { }).then((unityInstance) => {
loadingBar.style.display = "none"; loadingBar.style.display = "none";
// 保存Unity实例引用供其他函数使用
window.unityInstance = unityInstance;
// 修复全屏按钮点击事件
fullscreenButton.onclick = () => { fullscreenButton.onclick = () => {
// 优先使用Unity内置的全屏方法
unityInstance.SetFullscreen(1); 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) => { }).catch((message) => {
alert(message); alert(message);
}); });
}; };
var inputObj;
function Test() { // 修复Test函数确保只添加一次事件监听器
console.log("1"); window.Test = function() {
if (inputObj != null) document.body.removeChild(inputObj); console.log("1");
inputObj = document.createElement('input'); if (inputObj != null) document.body.removeChild(inputObj);
inputObj.setAttribute('id', '_ef'); inputObj = document.createElement('input');
inputObj.setAttribute('type', 'file'); inputObj.setAttribute('id', '_ef');
inputObj.setAttribute("style", 'visibility:hidden'); inputObj.setAttribute('type', 'file');
document.body.appendChild(inputObj); inputObj.setAttribute("style", 'visibility:hidden');
document.addEventListener('input', imgChange); document.body.appendChild(inputObj);
var file = document.getElementById("_ef");
file.click();
file.value;
console.log("2");
}
function imgChange(obj) { // 移除之前的事件监听器,避免重复添加
console.log("3"); document.removeEventListener('input', window.imgChange);
var file = document.getElementById("_ef"); document.addEventListener('input', window.imgChange);
var imgUrl = window.URL.createObjectURL(file.files[0]);
if (instance != null) { var file = document.getElementById("_ef");
instance.SendMessage("Web", "CallBack", imgUrl); file.click();
} file.value;
}; console.log("2");
};
//添加功能--------- //添加功能---------
function HtmlDownloadWord(bytes, reportdata) { function HtmlDownloadWord(bytes, reportdata) {
var blob = new Blob([bytes]); var blob = new Blob([bytes]);
var url = window.URL.createObjectURL(blob); var url = window.URL.createObjectURL(blob);
generate(url, reportdata); generate(url, reportdata);
} }
function loadFile(url, callback) { function loadFile(url, callback) {
PizZipUtils.getBinaryContent(url, callback); PizZipUtils.getBinaryContent(url, callback);
} }
//处理base64数据 //处理base64数据
const base64Regex = const base64Regex =
/^data:image\/(png|jpg|svg|svg\+xml);base64,/; /^data:image\/(png|jpg|svg|svg\+xml);base64,/;
const validBase64 = const validBase64 =
/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/; /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
function base64Parser(dataURL) { function base64Parser(dataURL) {
if ( if (
typeof dataURL !== "string" || typeof dataURL !== "string" ||
!base64Regex.test(dataURL) !base64Regex.test(dataURL)
) { ) {
return false; return false;
} }
const stringBase64 = dataURL.replace(base64Regex, ""); const stringBase64 = dataURL.replace(base64Regex, "");
if (!validBase64.test(stringBase64)) { if (!validBase64.test(stringBase64)) {
throw new Error( throw new Error(
"Error parsing base64 data, your data contains invalid characters" "Error parsing base64 data, your data contains invalid characters"
); );
} }
// For nodejs, return a Buffer // For nodejs, return a Buffer
if (typeof Buffer !== "undefined" && Buffer.from) { if (typeof Buffer !== "undefined" && Buffer.from) {
return Buffer.from(stringBase64, "base64"); return Buffer.from(stringBase64, "base64");
} }
// For browsers, return a string (of binary content) : // For browsers, return a string (of binary content) :
const binaryString = window.atob(stringBase64); const binaryString = window.atob(stringBase64);
const len = binaryString.length; const len = binaryString.length;
const bytes = new Uint8Array(len); const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) { for (let i = 0; i < len; i++) {
const ascii = binaryString.charCodeAt(i); const ascii = binaryString.charCodeAt(i);
bytes[i] = ascii; bytes[i] = ascii;
} }
return bytes.buffer; return bytes.buffer;
} }
const imageOptions = { const imageOptions = {
getImage(tag) { getImage(tag) {
return base64Parser(tag); return base64Parser(tag);
}, },
getSize() { getSize() {
console.log("大小已被调用"); console.log("大小已被调用");
return [384, 216]; return [384, 216];
}, },
}; };
function generate(url, reportdata) { function generate(url, reportdata) {
loadFile( loadFile(
url, url,
function (error, content) { function (error, content) {
if (error) { if (error) {
throw error; throw error;
} }
//先处理unity传入的数据得到base64 //先处理unity传入的数据得到base64
reportdata = (reportdata.replace(/<(.|\n)*?>/g, '') || ' ') reportdata = (reportdata.replace(/<(.|\n)*?>/g, '') || ' ')
.replace(/&lt;/g, '<') .replace(/&lt;/g, '<')
.replace(/&gt;/g, '>'); .replace(/&gt;/g, '>');
reportdata = JSON.parse(reportdata) 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 imageModule = new ImageModule(imageOptions);
var zip = new PizZip(content); var zip = new PizZip(content);
var doc = new window.docxtemplater(zip, { var doc = new window.docxtemplater(zip, {
paragraphLoop: true, paragraphLoop: true,
linebreaks: true, linebreaks: true,
modules: [imageModule] modules: [imageModule]
}); });
//const image = doc.Media.addImage(doc, imageBytes, 230, 230);
doc.compile(); doc.compile();
//const data = {
// eid: reportdata.eid,
// name: reportdata.name,
// scroe: reportdata.scroe,
// image: imageBase64,
//}
//渲染模板
doc.render(reportdata); doc.render(reportdata);
var out = doc.getZip().generate({ var out = doc.getZip().generate({
type: "blob", type: "blob",
mimeType: mimeType:
"application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
compression: "DEFLATE", compression: "DEFLATE",
}); });
saveAs(out, "实验报告.docx"); saveAs(out, "实验报告.docx");
}); });
} }
document.body.appendChild(script); document.body.appendChild(script);
</script> </script>

View File

@ -1 +1 @@
把Assets同级文件夹直接复制一份到打包程序里里边是下载报告用的模板 把Assets同级文件夹“Data”直接复制一份到打包程序里,里边是下载报告用的模板