导入Paroxe PDFRenderer 插件
首先导入Paroxe PDFRenderer 插件
显示PDF
PC
参考
实现
UnityWebRequest 读取PDF
UnityWebRequest request = UnityWebRequest.Get(url);
yield return request.SendWebRequest();
if (!request.isNetworkError && request.isDone)
{
bytesDocument = request.downloadHandler.data;
}
使用获取到的字节数组打开PDF
documentPDF = new PDFDocument(bytesDocument);
RawImage 显示PDF
currentPage = 0;
Texture2D pdf = documentPDF.Renderer.RenderPageToTexture(documentPDF.GetPage(currentPage));
rawImageShowPDF.texture = pdf;
显示效果
WebGL
实现
UnityWebRequest 读取PDF
UnityWebRequest request = UnityWebRequest.Get(url);
yield return request.SendWebRequest();
if (!request.isNetworkError && request.isDone)
{
bytesDocument = request.downloadHandler.data;
}
使用获取到的字节数组打开PDF
PDFJS_Promise<PDFDocument> documentPromise = PDFDocument.LoadDocumentFromBytesAsync(bytesDocument);
while (!documentPromise.HasFinished)
{
yield return null;
}
if (!documentPromise.HasSucceeded)
{
yield break;
}
documentPDF = documentPromise.Result;
RawImage 显示PDF
currentPage = 0;
PDFJS_Promise<PDFPage> pagePromise = documentPDF.GetPageAsync(currentPage);
while (!pagePromise.HasFinished)
{
yield return null;
}
if (!pagePromise.HasSucceeded)
{
yield break;
}
PDFPage pagePDF = pagePromise.Result;
PDFJS_Promise<Texture2D> renderPromise = PDFRenderer.RenderPageToTextureAsync(pagePDF, (int)pagePDF.GetPageSize().x, (int)pagePDF.GetPageSize().y);
while (!renderPromise.HasFinished)
{
yield return null;
}
if (!renderPromise.HasSucceeded)
{
yield break;
}
Texture2D pdf = renderPromise.Result;
rawImageShowPDF.texture = pdf;
显示效果
翻页
PC 平台通过PDFDocument.GetPage(int) 获取指定页码的PDF,WebGL 平台通过PDFDocument.GetPageAsync(int) 获取指定页码的PDF,修改参数即可实现翻页
sumPages = documentPDF.GetPageCount(); //PDF总页码
//下一页
currentPage += 1;
if (currentPage >= sumPages)
{
currentPage = 0;
}
/* todo
* 切换显示PDF
*/
//上一页
currentPage -= 1;
if (currentPage < 0)
{
currentPage = sumPages - 1;
}
/* todo
* 切换显示PDF
*/
下载PDF
PC
参考
实现
if (!Directory.Exists(downloadPath))
{
Directory.CreateDirectory(downloadPath);
}
string downloadFileName = url.Substring(url.LastIndexOf('/') + 1);
string localPath = downloadPath + "/" + downloadFileName;
UnityWebRequest webRequest = UnityWebRequest.Get(url);
yield return webRequest.SendWebRequest();
if (webRequest.isNetworkError)
{
if (File.Exists(localPath))
{
File.Delete(localPath);
}
}
else
{
var file = webRequest.downloadHandler.data;
FileStream fileStream = new FileStream(localPath, FileMode.Create);
fileStream.Write(file, 0, file.Length);
fileStream.Close();
}
WebGL
参考
UnityWebGL截图/图片/文件调用浏览器下载 | 梓喵出没 (azimiao.com)https://www.azimiao.com/5355.html
实现
根据参考编写jslib,并放入Plugins 文件夹
引入dll
[DllImport("__Internal")]
private static extern void PDFDownloader(string strData, string fileName);
实现下载
string downloadFileName = url.Substring(url.LastIndexOf('/') + 1);
UnityWebRequest request = UnityWebRequest.Get(url);
yield return request.SendWebRequest();
if (!request.isNetworkError && request.isDone)
{
byte[] downloadBytes = request.downloadHandler.data;
if (downloadBytes != null)
{
PDFDownloader(System.Convert.ToBase64String(downloadBytes), downloadFileName);
}
}