Table of contents
Effect
question
This example combines FancyScrollView to read the PPT effect. There is a lag when reading PPT. It is recommended to optimize it and preload the PPT before starting the program. Caching it in the memory will give a better experience.
illustrate
Unity version: 2021.3.6f1c1
Unity API compatibility level: Net Framework4.x
DLL library:
Aspose.Slides: Download .NET DLL to read and edit presentations | Aspose.Slides https://releases.aspose.com/slides/net/
System.Drawing: Found in 2021.3.6f1c1\Editor\Data\MonoBleedingEdge\lib\mono\unityjit
Resources and code
I have packaged the two libraries into a package for you to download.
Package name: PPT_FancyScrollView.unitypackage
Link: https://pan.baidu.com/s/1KAhXOgSty8yI-G_LkrnXhQ
Extraction code: 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);
}
}
}
}