尊重原著:Unity WebGL 生成doc保存到本地电脑_unity webgl 保存文件-CSDN博客
1.UI方面
新建一个输入框和一个按钮
2.C#代码
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;
public class WebDocCreate : MonoBehaviour
{
public Button btn;
public InputField text;
private void Start()
{
btn.onClick.AddListener(() =>
{
Application.ExternalCall("HelloWorld", text.text);
});
}
// Update is called once per frame
void Update()
{
}
}
3.打包出来,在index.html <script>内 输入以下代码块
function HelloWorld(content) {
exportRaw('text.doc', content)
}
function fakeClick(obj) {
var ev = document.createEvent("MouseEvents");
ev.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
obj.dispatchEvent(ev);
}
function exportRaw(name, data) {
var urlObject = window.URL || window.webkitURL || window;
var export_blob = new Blob([data]);
var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a")
save_link.href = urlObject.createObjectURL(export_blob);
save_link.download = name;
fakeClick(save_link);
}
4.最终效果,点击按钮生成文档