先匯入插件 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的UI
PDFDocument 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 update
void 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 frame
void 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;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/397487.html
標籤:其他
上一篇:用C語言實作推箱子游戲
