新增web版视频下载功能

This commit is contained in:
shenjianxing 2025-05-14 11:54:12 +08:00
parent fb2bddcbae
commit 20afeb9fa1
3 changed files with 81 additions and 20 deletions

View File

@ -23,5 +23,11 @@ mergeInto(LibraryManager.library, {
});
} else {
}
},
WebGLDownloadVideo: function(ptr,fileName) {
var url = UTF8ToString(ptr);
var name = UTF8ToString(fileName);
DownloadVideoFromUrl(url,name);
}
});

View File

@ -16,6 +16,11 @@ public class WebGLDownLoadFile : MonoSingleton<WebGLDownLoadFile>
[DllImport("__Internal")]
private static extern void WebGLDownloadWord(byte[] array, int size, string reportjson);
[DllImport("__Internal")]
private static extern void WebGLDownloadVideo(string url, string fileName = "实验视频.mp4");
/// <summary>
/// 下载Word 方法
/// </summary>
@ -63,4 +68,9 @@ public class WebGLDownLoadFile : MonoSingleton<WebGLDownLoadFile>
WebGLDownloadWord(bytes, bytes.Length, reportjson);
}
public void DownloadVideo(string url, string fileName = "实验视频.mp4")
{
WebGLDownloadVideo(url, fileName);
}
}

View File

@ -1,30 +1,30 @@
<!DOCTYPE html>
<html lang="en-us">
<head>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>虚拟实验 | {{{ PRODUCT_NAME }}}</title>
<link rel="shortcut icon" href="TemplateData/favicon.ico">
<link rel="stylesheet" href="TemplateData/style.css">
<script type="text/javascript" src="StreamingAssets/WebGLDownloadWordJS/js/docxtemplater.js"></script>
<script type="text/javascript" src="StreamingAssets/WebGLDownloadWordJS/js/pizzip.js"></script>
<script type="text/javascript" src="StreamingAssets/WebGLDownloadWordJS/js/FileSaver.js"></script>
<script type="text/javascript" src="StreamingAssets/WebGLDownloadWordJS/js/pizzip-utils.js"></script>
<script type="text/javascript" src="StreamingAssets/WebGLDownloadWordJS/js/imagemodule.js"></script>
</head>
<body>
<script type="text/javascript" src="StreamingAssets/WebGLDownloadWordJS/js/docxtemplater.js"></script>
<script type="text/javascript" src="StreamingAssets/WebGLDownloadWordJS/js/pizzip.js"></script>
<script type="text/javascript" src="StreamingAssets/WebGLDownloadWordJS/js/FileSaver.js"></script>
<script type="text/javascript" src="StreamingAssets/WebGLDownloadWordJS/js/pizzip-utils.js"></script>
<script type="text/javascript" src="StreamingAssets/WebGLDownloadWordJS/js/imagemodule.js"></script>
</head>
<body>
<div id="unity-container" class="unity-desktop">
<canvas id="unity-canvas" width=1280 height=720></canvas>
<div id="unity-loading-bar">
<div id="unity-progress-bar-empty">
<div id="unity-progress-bar-full"></div>
<canvas id="unity-canvas" width=1280 height=720></canvas>
<div id="unity-loading-bar">
<div id="unity-progress-bar-empty">
<div id="unity-progress-bar-full"></div>
</div>
</div>
<div id="unity-warning"> </div>
<div id="unity-footer">
<div id="unity-fullscreen-button"></div>
<div id="unity-build-title">{{{ PRODUCT_NAME }}}</div>
</div>
</div>
<div id="unity-warning"> </div>
<div id="unity-footer">
<div id="unity-fullscreen-button"></div>
<div id="unity-build-title">{{{ PRODUCT_NAME }}}</div>
</div>
</div>
<script>
var container = document.querySelector("#unity-container");
@ -142,7 +142,7 @@
}
};
//添加功能---------
//添加功能---------
function HtmlDownloadWord(bytes, reportdata) {
var blob = new Blob([bytes]);
@ -356,7 +356,52 @@
});
}
// 添加视频下载功能
function HtmlDownloadVideo(bytes, fileName) {
// 创建Blob对象指定视频MIME类型
var blob = new Blob([bytes], { type: 'video/mp4' });
// 创建下载链接
var url = window.URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = fileName;
// 触发下载
document.body.appendChild(a);
a.click();
// 清理资源
setTimeout(() => {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 100);
}
// 如果需要从URL加载视频并下载
function DownloadVideoFromUrl(videoUrl, fileName) {
// 使用fetch API获取视频数据
fetch(videoUrl)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.arrayBuffer();
})
.then(bytes => {
// 调用下载函数
HtmlDownloadVideo(bytes, fileName);
})
.catch(error => {
console.error('error:', error);
unityShowBanner('error: ' + error.message, 'error');
});
}
document.body.appendChild(script);
</script>
</body>
</body>
</html>