优化:解决切换pdf操作多崩溃问题:

在 rawImage.GetComponent<RawImage>().texture赋值前先删除之前的texture

if ( rawImage.GetComponent<RawImage>().texture != null)
            {
                GameObject.Destroy( rawImage.GetComponent<RawImage>().texture);
            }

正文:

先导入插件 PDFRenderer

链接: https://pan.baidu.com/s/1Un-FoINPmK8iVBRLS0jkTw 提取码: z78q

然后使用以下代码就可以

using Paroxe.PdfRenderer;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;public class PlayPDF : MonoBehaviour
{public RawImage rawImage;   //显示PDF的UIPDFDocument pdfDocument;    //PDF文件PDFRenderer pdfRenderer;    //PDF渲染int curPDFPage;    //当前显示的PDF页数int countOfPDFAllPage;    //PDF文件总页数//PDF网络路径 (这里填入自己的网络PDF路径)string url= "https:// xx.xxxx.xxxx.pdf";// Start is called before the first frame updatevoid Start(){StartCoroutine(DownLoadFile(url));}/// <summary>/// 加载本地PDF文件/// </summary>/// <param name="url"></param>void LoadLocalPDF(string url){pdfDocument = new PDFDocument(url);if (pdfDocument.IsValid)    //判断该文档是否有效{curPDFPage = 0;countOfPDFAllPage = pdfDocument.GetPageCount();ScreenShowPDF(GetCurPagePDFTexture(curPDFPage));}else{if (File.Exists(url)){File.Delete(url);}pdfDocument = null;}}/// <summary>/// 获取当前页面的PDF画面/// </summary>/// <param name="page"></param>/// <returns></returns>Texture2D GetCurPagePDFTexture(int page){if (pdfDocument == null) return null;Texture2D tex = pdfDocument.Renderer.RenderPageToTexture(pdfDocument.GetPage(page));//纹理的过滤模式tex.filterMode = FilterMode.Bilinear;//随着值变大,纹理在浅角度下会更清晰。值越低,表示纹理在浅角度下更模糊。‎tex.anisoLevel = 8;return tex;}/// <summary>/// 下载网络PDF文件到本地/// </summary>/// <param name="url"></param>/// <returns></returns>IEnumerator DownLoadFile(string url){yield return new WaitForSeconds(0.5f);string directoryPath = Application.persistentDataPath + "/FileCache";if (!Directory.Exists(directoryPath)){Directory.CreateDirectory(directoryPath);}string downloadFileName = url.Substring(url.LastIndexOf('/') + 1);string localpath = directoryPath + "/" + downloadFileName;Debug.Log(downloadFileName);//MDebug(localpath);//如果本地文件已存在 直接加载if (File.Exists(localpath)){LoadLocalPDF(localpath);yield break;}//UnityWebRequest webRequest = new UnityWebRequest();UnityWebRequest webRequest = UnityWebRequest.Get(url);webRequest.timeout = 60;yield return webRequest.SendWebRequest();if (webRequest.isNetworkError){Debug.Log("Download Error: " + webRequest.error);if (File.Exists(localpath)){File.Delete(localpath);}}else{var file = webRequest.downloadHandler.data;FileStream nFile = new FileStream(localpath, FileMode.Create);nFile.Write(file, 0, file.Length);nFile.Close();LoadLocalPDF(localpath);}}/// <summary>/// 切换PDF页面/// </summary>void ChangePDFPage(){Texture2D tex= GetCurPagePDFTexture(curPDFPage);ScreenShowPDF(tex);}/// <summary>/// 显示PDF在UI或者物体上/// </summary>/// <param name="texture"></param>private void ScreenShowPDF(Texture2D texture){if (texture == null) return;if (texture.width >= 2048 || texture.height >= 2048)return;RectTransform recttrans = rawImage.GetComponent<RectTransform>();//3DUI PDF rawimage 大小float maxWidth = 1920;float maxHeight = 1080;float scalex = texture.width * 1.0f / maxWidth;float scaley = texture.height * 1.0f / maxHeight;if (scalex > scaley){float d = 1.0f / scalex;scaley = scaley * d;scalex = 1.0f;}else{float d = 1.0f / scaley;scalex = scalex * d;scaley = 1.0f;}recttrans.sizeDelta = new Vector2(maxWidth * scalex, maxHeight * scaley);rawImage.GetComponent<RawImage>().texture = texture;rawImage.GetComponent<RawImage>().color = new Color(1.0f, 1.0f, 1.0f, 1.0f);}// Update is called once per framevoid Update(){//翻页 翻页时关闭自动播放if (Input.GetKeyDown(KeyCode.RightArrow)){StopAllCoroutines();curPDFPage += 1;if (curPDFPage >= countOfPDFAllPage) curPDFPage = 0;ChangePDFPage();}if (Input.GetKeyDown(KeyCode.LeftArrow)){StopAllCoroutines();curPDFPage -= 1;if (curPDFPage < 0) curPDFPage = countOfPDFAllPage-1;ChangePDFPage();}//自动播放if (Input.GetKeyDown(KeyCode.A)){StartCoroutine(AutoTurnPage());}//停止自动播放if (Input.GetKeyDown(KeyCode.S)){StopAllCoroutines();}}
}

添加自动翻页功能

bool IsAutoPlayLoop = false;    //循环播放bool autoTurnPageOver = false;  //自动播放结束/// <summary>/// 自动翻页/// </summary>/// <returns></returns>IEnumerator AutoTurnPage(){yield return new WaitForSeconds(2);curPDFPage += 1;if (curPDFPage < countOfPDFAllPage){autoTurnPageOver = false;ChangePDFPage();StartCoroutine(AutoTurnPage());}else{if (IsAutoPlayLoop)  //循环自动播放{curPDFPage = 0;ChangePDFPage();StartCoroutine(AutoTurnPage());}else//非循环自动播放{if (autoTurnPageOver){curPDFPage = 0;ChangePDFPage();StartCoroutine(AutoTurnPage());}}autoTurnPageOver = true;}}

Unity3D 加载PDF文件以及简单的切换页面相关推荐

  1. Android 加载pdf文件

    今天来总结一下加载pdf文件. 加载pdf有好多办法, 一:使用webview来加载. mWebView.loadUrl("http://docs.google.com/gviewembed ...

  2. iframe加载pdf支持android,如何检查iframe是否已完全加载PDF文件

    如何在iframe加载PDF文件时显示加载指示符?如何检查iframe是否已完全加载PDF文件 在我部分工作的解决方案中,我执行以下操作:当模式对话框(其中包含iframe)打开时,会显示一个加载指示 ...

  3. android 上传pdf文件,Android 加载PDF文件

    今天按项目要求找了一个android的PDF控件,各种操作效果都非常好,在这里和大家分享一下. 用法很简单: 1.在build.gradle里面添加依赖 compile 'com.github.bar ...

  4. 使用 pdf.js 在网页中加载 pdf 文件

    在网页中加载并显示PDF文件是最常见的业务需求.例如以下应用场景:(1)在电商网站上购物之后,下载电子发票之前先预览发票.(2)电子政务管理系统中查看发布的公文,公文文件一般是PDF格式的文件. 目前 ...

  5. android 使用pdf文件大小,Android 加载PDF文件的使用

    https://www.jianshu.com/p/608b270a1ab8 1.1 PDF View的使用 首先我们需要加载依赖库 compile 'com.joanzapata.pdfview:a ...

  6. html 加载pdf文件内容不显示不出来,pdf.js首次加载pdf文件时找不到pdf文件,刷新后才能出现pdf文件...

    前台点击文件学码的定一近,更工广款近,更工广款近,更名后,后台通过openoffice将doc文件转为pdf格式文件,然后存放在服务器tomcat'中,pdf.js首次加载服务器中tomcat中的转化 ...

  7. Vue中怎么加载pdf文件

    Vue预览pdf文件,我觉得pdfh5方式比较简洁,还有vue-pdf也可以,但是如果文件内容较多的话,vue-pdf就需要做分页处理,就麻烦了:接下来,想分享下我使用pdfh5的代码逻辑.如下:(注 ...

  8. iframe 加载pdf文件

    使用iframe标签,给上高度.宽度,效果和用浏览器打开pdf文件一致.支持下载.打印等功能. <!DOCTYPE html> <html lang="en"&g ...

  9. .net使用pdfobject.js加载pdf文件

    1.下载pdfobject.js文件 2. 1 <script type="text/javascript" src="<%= Application[&qu ...

最新文章

  1. pycharm:格式化python代码
  2. python运用在哪些地方_必看 | 2020年,Python十大应用领域介绍!
  3. 授人以渔:stm32资料查询技巧
  4. C语言三个结束符:EOF ‘\0’ '\n'
  5. 哪句诗词最让你怦然心动?
  6. 利用 Zabbix 监控 mysqldump 定时备份数据库是否成功
  7. mysql丢数据无法启动mysql_mysql InnoDB数据无法启动解决办法
  8. 自己手写WEB程序框架并执行
  9. 将VS2010的工程转换为VS2005,即用VS2005打开
  10. UART协议就应该这么理解
  11. 计算机网络1MB等于多少b,1mb等于多少kb流量?1mb等于多少GB?1mb等于多少TB?1mb等于多少b...
  12. FPGA知识汇集-值得收藏的FPGA代码命名规范?
  13. python3爬取网页AJAX数据
  14. 福尔摩斯基本演绎法第一季/全集Elementary迅雷下载
  15. 【搬家】【数据库】【优化】SQL 优化学习小结——索引和语句优化
  16. Django实现收藏功能
  17. 人生最曼妙的风景,竟是内心的淡定与从容——杨绛
  18. java date 日期格式_如何将JAVA DATE类型的日期 转换成指定格式类型的 (如:YYYY-MM-DD) 的 DATE类型数据?...
  19. 46 《未来的工作》 -豆瓣评分7.0
  20. SAP中使用MM01创建物料如何选定行业领域和物料类型

热门文章

  1. 图像颜色校正的几种方法
  2. 图(Graph)详解 - 数据结构
  3. 带你入门多目标跟踪(一)领域概述
  4. 教师计算机培训心得博客,教师研修心得体会博客
  5. 查找字幕资源的网址。射手网
  6. 花千骨html+css
  7. 制作支持View,图片轮播的Banner
  8. 钉钉与wcp集成通知【原创】
  9. Android 电话系统框架介绍
  10. python leetcode 202. Happy Number