Unity3D 加载PDF文件以及简单的切换页面先导入插件

Unity3D 加载PDF文件以及简单的切换页面先导入插件 PDFRenderer 链接: https://pan.baidu.com/s/1Un-FoINPmK8iVBRLS0jkTw 提取码: z78q  然后使用以下代码就可以 using Paroxe.PdfRenderer;us

先导入插件 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">
    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">
    /// <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">
    /// <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">
    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();
            }
    }
}
</rawimage></rawimage></recttransform>

添加自动翻页功能

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;
            }
             
        }

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity PDFReader插件是一个用于Unity开发环境的插件,可以在Unity实现对PDF文件的读取和呈现功能。其主要特点如下: 1. 读取功能:Unity PDFReader插件可以读取PDF文件的内容,包括文本、图像和其他媒体元素。用户可以通过代码调用插件的接口,实现对PDF文件内容的读取。 2. 呈现功能:插件可以将PDF文件的内容以用户可见的方式呈现在Unity应用程序。用户可以选择将PDF文件呈现为只读文档、有交互的文档(支持表单和链接)或可编辑的文档。 3. 兼容性:插件兼容各种版本的Unity,同时支持多平台,包括Windows、Mac和移动设备平台。这使得开发人员可以在不同的设备上使用相同的插件代码,从而提高开发效率。 4. 自定义选项:Unity PDFReader插件还提供了一些自定义选项,使开发人员能够调整PDF文件的呈现方式。例如,用户可以选择显示页面缩略图、书签、目录和搜索功能。 5. 扩展性:插件提供了一些接口和事件,开发人员可以使用这些接口和事件来扩展插件的功能。这使开发人员能够根据自己的需求定制插件,以满足特定的应用场景。 总之,Unity PDFReader插件Unity开发者提供了一种方便快捷地实现PDF文件读取和呈现功能的解决方案。无论是用于创建教育应用、电子书阅读器还是其他需要展示PDF文件内容的应用,该插件都是一个值得考虑的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值