Unity3D 加载PDF文件以及简单的切换页面
生活随笔
收集整理的這篇文章主要介紹了
Unity3D 加载PDF文件以及简单的切换页面
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
優(yōu)化:解決切換pdf操作多崩潰問題:
在?rawImage.GetComponent<RawImage>().texture賦值前先刪除之前的texture
if (?rawImage.GetComponent<RawImage>().texture != null)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? GameObject.Destroy(?rawImage.GetComponent<RawImage>().texture);
? ? ? ? ? ? }
正文:
先導(dǎo)入插件 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頁數(shù)int countOfPDFAllPage; //PDF文件總頁數(shù)//PDF網(wǎng)絡(luò)路徑 (這里填入自己的網(wǎng)絡(luò)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>/// 下載網(wǎng)絡(luò)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(){//翻頁 翻頁時關(guān)閉自動播放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; //循環(huán)播放bool autoTurnPageOver = false; //自動播放結(jié)束/// <summary>/// 自動翻頁/// </summary>/// <returns></returns>IEnumerator AutoTurnPage(){yield return new WaitForSeconds(2);curPDFPage += 1;if (curPDFPage < countOfPDFAllPage){autoTurnPageOver = false;ChangePDFPage();StartCoroutine(AutoTurnPage());}else{if (IsAutoPlayLoop) //循環(huán)自動播放{curPDFPage = 0;ChangePDFPage();StartCoroutine(AutoTurnPage());}else//非循環(huán)自動播放{if (autoTurnPageOver){curPDFPage = 0;ChangePDFPage();StartCoroutine(AutoTurnPage());}}autoTurnPageOver = true;}}總結(jié)
以上是生活随笔為你收集整理的Unity3D 加载PDF文件以及简单的切换页面的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jzoj1273 袁绍的刁难(math)
- 下一篇: 130-Vue中的监听事件——Watch