Unity 加载读取PPT

目录

效果

问题

说明

资源与代码


效果

问题

本例子结合FancyScrollView,读取PPT效果。PPT读取的时候有点卡顿,建议优化一下,在程序启动前预加载一下PPT,缓存在内存里,体感会更好。

说明

Unity版本:2021.3.6f1c1

Unity API兼容等级:Net Framework4.x

DLL库:

Aspose.Slides:下载 .NET DLL 以读取编辑演示文稿|Aspose.Slideshttps://releases.aspose.com/slides/net/

System.Drawing:2021.3.6f1c1\Editor\Data\MonoBleedingEdge\lib\mono\unityjit中找

资源与代码

两个库我已经给大家封装在一个包里了,大家下载即可。 

包名:PPT_FancyScrollView.unitypackage

链接:https://pan.baidu.com/s/1KAhXOgSty8yI-G_LkrnXhQ
提取码:n335

// 核心代码

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using UnityEngine;
using System.Threading.Tasks;
using Aspose.Slides;

namespace FancyScrollView.PPT
{
    public class PPTDManager : MonoBehaviour
    {
        private static PPTDManager instance;

        public static PPTDManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new GameObject().AddComponent<PPTDManager>();

                    // instance = new PPTDManager();
                }

                return instance;
            }
        }

        public Presentation LoadPPT(string path)
        {
            if (File.Exists(path) == false) return null;

            return new Presentation(path);
        }

        private Dictionary<string, Sprite> itemDatasCash = new Dictionary<string, Sprite>();

        public void LoadPPTByIndex(ItemData itemData, Action<Sprite> completion)
        {
            Sprite sprite;

            var key = itemData.path + itemData.pageIndex;

            if (itemDatasCash.TryGetValue(key, out sprite))
            {
                completion?.Invoke(sprite);
            }
            else
            {
                Presentation presentation = itemData.presentation;
                int index = itemData.pageIndex;
                var slide = presentation.Slides[index];
                var bitmap = slide.GetThumbnail(1, 1);

                Bitmap2Byte(bitmap, (bytes) =>
                {
                    // PPT的尺寸
                    // int width = 960;
                    // int height = 540;
                    int width = Convert.ToInt16(presentation.SlideSize.Size.Width);
                    int height = Convert.ToUInt16(presentation.SlideSize.Size.Height);

                    Texture2D texture2D = new Texture2D(width, height);
                    texture2D.LoadImage(bytes);
                    sprite = Sprite.Create(texture2D, new Rect(0, 0, width, height), Vector2.zero);
                    if (itemDatasCash.ContainsKey(key) == false)
                    {
                        itemDatasCash.Add(key, sprite);
                    }

                    completion?.Invoke(sprite);
                });
            }
        }


        private async Task Bitmap2Byte(Bitmap bitmap, Action<byte[]> completion)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                bitmap.Save(stream, ImageFormat.Png);
                byte[] data = new byte[stream.Length];
                stream.Seek(0, SeekOrigin.Begin);
                await stream.ReadAsync(data, 0, Convert.ToInt32(stream.Length));
                completion?.Invoke(data);
            }
        }
    }
}
  • 9
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值