文章目录
插件包介绍
主要解决:Unity WebGL 下载替换Word模板功能一、导入自己Word模板
注意:Word模板路径必须在此文件夹下
二、设置Word模板
Word文档中使用“{}”符号,中间写入替换的字段名 :{关键字段}
三、创建报告基础数据
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
/// <summary>
/// 实验报告基础参数
/// </summary>
[Serializable]
public class LabReprotData
{
#region 基础数据
[Header("EID")]
public int eid;
[Header("名字")]
public string name;
[Header("得分")]
public float scroe;
#endregion
#region 空数据如果初始化没有自动设置默认
public string test1;
public float test2;
public int test3;
#endregion
}
四、初始化下载Word 参数
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Networking;
/// <summary>
/// 下载Word脚本
/// </summary>
public class WebGLDownLoadFile : MonoBehaviour
{
[DllImport("__Internal")]
private static extern void WebGLDownloadWord(byte[] array, int size, string reportjson);
/// <summary>
/// 下载Word 方法
/// </summary>
public void Download()
{
#region 初始化
LabReprotData reprotData = new LabReprotData();
reprotData.eid = 1001;
reprotData.name = "小陈";
reprotData.scroe = 100;
// 转json
string json = JsonUtility.ToJson(reprotData, true);
#endregion
// 调用携程下载数据
StartCoroutine(WebGLDownloadWord(json));
}
/// <summary>
/// 下载Word
/// </summary>
/// <param name="json">实验报告json</param>
/// <returns></returns>
IEnumerator WebGLDownloadWord(string json)
{
System.Uri uri = new System.Uri(Path.Combine(Application.streamingAssetsPath + "/Template/", "模板.docx"));
UnityWebRequest request = UnityWebRequest.Get(uri);
yield return request.SendWebRequest();
if (request.isHttpError || request.isNetworkError)
{
Debug.Log("错误路径:" + request.error);
}
else
{
// 下载word
DownloadDocx(request.downloadHandler.data, json);
}
}
/// <summary>
/// 调用下载Word
/// </summary>
/// <param name="bytes">支持多种文件格式下载(doc docx xml xlsx txt等)</param>
/// <param name="reportjson">json数据</param>
public void DownloadDocx(byte[] bytes, string reportjson)
{
WebGLDownloadWord(bytes, bytes.Length, reportjson);
}
}
五、打包WebGL
注意:打包路径不能存在中文、项目路径也一样。
六、设置配置文件
1.文件位置
2.设置参数
注意:Pack path Name 目录名称必须修改,这个就是步骤五WebGL打包的文件夹名字
3.配置文件快捷键
七、创建Index
1.直接替换之前打包Index文件
八、浏览器打开WebGL
提示:建议使用本地、因为使用IIS效果一样。不嫌麻烦保证正确性使用IIS