unity加载ppt

1、效果图

 2、unity界面

脚本挂载照相机或者canvas上都行,然后content是canvas下面的content,perfab是一个图片的预制体,各种按钮就是图中的那几个按钮(做的非常简单)

3、脚本

using System;
using System.Collections;
using System.Drawing.Imaging;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

//※※※※ 别忘了在PlayerSetting中改成.net4.x的api ※※※※
public class LoadPPT : MonoBehaviour
{
    public Transform content;
    [SerializeField] Image prefab;

    [SerializeField] Button firstPageBtn;   //第一个按钮
    [SerializeField] Button lastPageBtn;   //第一个按钮
    [SerializeField] Button toLastPageBtn;   //第一个按钮
    [SerializeField] Button toNestPageBtn;   //第一个按钮
    [SerializeField] Button autoPlayBtn;   //第一个按钮
    [SerializeField] Button StopAutoPlayBtn;   //第一个按钮
    private void Start ()
    {
        string pptPath = Application.streamingAssetsPath + "/Test.pptx";
       
        Load(pptPath);
        FirstPage();

        //按钮监听
        firstPageBtn.onClick.AddListener(FirstPage);
        lastPageBtn.onClick.AddListener(LastPage);
        toLastPageBtn.onClick.AddListener(ToLastPage);
        toNestPageBtn.onClick.AddListener(ToNextPage);
        StopAutoPlayBtn.onClick.AddListener(StopAutoPlay);
        autoPlayBtn.onClick.AddListener(delegate {InvokeRepeating("AutoPlayImage", 3,3); });
    }
    public void Load (string pptPath)
    {
        //读取ppt文件
        var presentation = new Aspose.Slides.Presentation(pptPath);
        //遍历文档(只做示例使用自己根据需求拓展)
        for (int i = 0; i < presentation.Slides.Count; i++)
        {
            Instantiate(prefab, content);
            if (i < content.childCount)
            {
                var slide = presentation.Slides[i];
                var bitmap = slide.GetThumbnail(1f, 1f);
                byte[] bytes = Bitmap2Byte(bitmap);

                var showImage = content.GetChild(i).GetComponent<UnityEngine.UI.Image>();
                int width = 960, height = 540;
                Texture2D texture2D = new Texture2D(width, height);
                texture2D.LoadImage(bytes);
                Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, width, height), Vector2.zero);
                showImage.sprite = sprite;
            }
        }
    }

    //将ppt转为图片数据
    public byte[] Bitmap2Byte (System.Drawing.Bitmap bitmap)
    {
        //方式2:
        using (MemoryStream stream = new MemoryStream())
        {
            bitmap.Save(stream, ImageFormat.Png);
            byte[] data = new byte[stream.Length];
            stream.Seek(0, SeekOrigin.Begin);
            stream.Read(data, 0, Convert.ToInt32(stream.Length));
            return data;
        }
    }

    #region 首尾页

    //显示首页
    void FirstPage()
    {
        if (IsInvoking("AutoPlayImage"))
            CancelInvoke("AutoPlayImage");
        for (int i = 0; i < content.childCount; i++)
        {
            content.GetChild(0).gameObject.SetActive(true);
            if (i!=0)
            {
                content.GetChild(i).gameObject.SetActive(false);
            }
        }
    }

    //显示尾页
    void LastPage()
    {
        if (IsInvoking("AutoPlayImage"))
            CancelInvoke("AutoPlayImage");
        for (int i = 0; i < content.childCount; i++)
        {
            content.GetChild(content.childCount-1).gameObject.SetActive(true);
            if (i != content.childCount - 1)
            {
                content.GetChild(i).gameObject.SetActive(false);
            }
        }
    }

    #endregion

    #region 上一张,下一张

    int index = 0;    //计数
    //上一张
    void ToLastPage()
    {
        if (IsInvoking("AutoPlayImage"))
            CancelInvoke("AutoPlayImage");
        index--;
        if (index <= 0)
            index = 0;
        for (int i = 0; i < content.childCount; i++)
        {
            content.GetChild(index).gameObject.SetActive(true);
            if (i !=index)
            {
                content.GetChild(i).gameObject.SetActive(false);
            }
        }
    }

    //下一张
    void ToNextPage()
    {
        if (IsInvoking("AutoPlayImage"))
            CancelInvoke("AutoPlayImage");
        index++;
        if (index >= content.childCount - 1)
            index = content.childCount-1;
        for (int i = 0; i < content.childCount; i++)
        {
            content.GetChild(index).gameObject.SetActive(true);
            if (i != index)
            {
                content.GetChild(i).gameObject.SetActive(false);
            }
        }
    }

    #endregion

    #region 是否自动播放
    //自动播放
    void AutoPlayImage()
    {
        Debug.Log(1321432);
        index++;
        if (index > content.childCount - 1)
            index = 0;
        for (int i = 0; i < content.childCount; i++)
        {
            if (i != index)
            {
                content.GetChild(i).gameObject.SetActive(false);
            }
        }
        content.GetChild(index).gameObject.SetActive(true);
    }

    //停止自动播放
    void StopAutoPlay()
    {
        if (IsInvoking("AutoPlayImage"))
            CancelInvoke("AutoPlayImage");
    }
    #endregion

}

注意:

1、文件位置,是在StreamingAssets中新建一个Test的ppt文件,没有文件的话,是读不到ppt内容的。

2、修改File——Build Setting——Player Setting——Player中找到这个Api Compatibility Level改成4x。

 4、程序包

链接:https://pan.baidu.com/s/1ahgtyG3PmA32jCyIcvPHgg?pwd=jl98 
提取码:jl98

  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值