修改加载PPT逻辑

This commit is contained in:
yanghua 2025-04-08 13:17:40 +08:00
parent c9f5e2e308
commit 6b0384849a
11 changed files with 103 additions and 104 deletions

View File

@ -17,32 +17,32 @@ public class Bootstrap : SingletonMono<Bootstrap>
base.Awake(); base.Awake();
uiManager = new UIManager(); uiManager = new UIManager();
eventCenter = new EventCenter(); eventCenter = new EventCenter();
scenesManager = new ScenesManager(); scenesManager = new ScenesManager(this);
pptFolderName = Application.streamingAssetsPath + "/PPT"; pptFolderName = Application.streamingAssetsPath + "/PPT";
LoadPPTTest.PPTFiles(pptFolderName);
ppts = LoadPPTTest.PPTName();
DontDestroyOnLoad(gameObject); DontDestroyOnLoad(gameObject);
} }
private void Start() private void Start()
{ {
eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 0.1f); uiManager.ShowPanel<UI_LoadingPanel>(this, E_UI_Layer.System, (panel) =>
scenesManager.LoadSceneAsyn(this, "MenuScene", () =>
{ {
uiManager.ShowPanel<UI_BGPanel>(this, E_UI_Layer.Bot, (panel) => eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 0.1f);
scenesManager.LoadSceneAsyn("MenuScene", () =>
{ {
uiManager.ShowPanel<UI_SelectModePanel>(this, E_UI_Layer.Top, (panel) => uiManager.ShowPanel<UI_BGPanel>(this, E_UI_Layer.Bot, (panel) =>
{ {
Debug.Log("UI_SelectModePanel"); //LoadPPTTest.PPTFiles(pptFolderName);
uiManager.ShowPanel<UI_SelectModePanel>(this, E_UI_Layer.Top, (panel) =>
{
ppts = LoadPPTTest.PPTName();
Debug.Log("UI_SelectModePanel");
});
}); });
eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 2f);
}); });
eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 0.9f);
}); });
//uiManager.ShowPanel<UI_LoadingPanel>(this, E_UI_Layer.System, (panel) =>
//{
//});
} }
// Update is called once per frame // Update is called once per frame

View File

@ -6,7 +6,11 @@ using UnityEngine.SceneManagement;
public class ScenesManager public class ScenesManager
{ {
public ScenesManager() { } private MonoBehaviour _target;
public ScenesManager(MonoBehaviour target)
{
_target = target;
}
/// <summary> /// <summary>
/// 同步加载场景 /// 同步加载场景
/// </summary> /// </summary>
@ -25,11 +29,9 @@ public class ScenesManager
/// <param name="sceneName">场景名称</param> /// <param name="sceneName">场景名称</param>
/// <param name="action">委托</param> /// <param name="action">委托</param>
/// <param name="loadSceneMode">加载场景方式</param> /// <param name="loadSceneMode">加载场景方式</param>
public void LoadSceneAsyn(MonoBehaviour taget, string sceneName, UnityAction action = null, public void LoadSceneAsyn(string sceneName, UnityAction action = null)
LoadSceneMode loadSceneMode = LoadSceneMode.Single)
{ {
Debug.Log("LoadSceneAsyn"); _target.StartCoroutine(ReallyLoadScene(sceneName, action));
taget.StartCoroutine(ReallyLoadScene(sceneName, action));
} }
/// <summary> /// <summary>
@ -43,8 +45,7 @@ public class ScenesManager
LoadSceneMode loadSceneMode = LoadSceneMode.Single) LoadSceneMode loadSceneMode = LoadSceneMode.Single)
{ {
AsyncOperation ao = SceneManager.LoadSceneAsync(sceneName, loadSceneMode); AsyncOperation ao = SceneManager.LoadSceneAsync(sceneName, loadSceneMode);
yield return ao;
action?.Invoke(); action?.Invoke();
yield return ao.isDone;
} }
} }

View File

@ -14,6 +14,7 @@ using UnityEngine.UI;
public static class LoadPPTTest public static class LoadPPTTest
{ {
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
@ -29,33 +30,26 @@ public static class LoadPPTTest
/// <returns></returns> /// <returns></returns>
public static List<Texture2D> LoadPPTItems(string pptName) public static List<Texture2D> LoadPPTItems(string pptName)
{ {
string pptPath = GetPPTPathByName(pptName); string pptPath = GetPPTPathByName(pptName);
return GetTexture2DsByName(pptName, pptPath); return GetTexture2DsByName(pptName, pptPath);
//target.StartCoroutine(LoadAllPPTFiles(pptPath)); //target.StartCoroutine(LoadAllPPTFiles(pptPath));
} }
public static string[] PPTName() public static string[] PPTName()
{ {
///切割pptsPath ///切割pptsPath
return pptsPath; return pptsPath;
} }
private static List<Texture2D> GetTexture2DsByName(string pptName, string pptPath) private static List<Texture2D> GetTexture2DsByName(string pptName, string pptPath)
{ {
if (!texturesDic.ContainsKey(pptName)) foreach (var item in texturesDic.Keys)
{ {
texturesDic.Add(pptName, LoadAllPPTFiles(pptPath)); if (item.Equals(pptName))
return LoadAllPPTFiles(pptPath); return texturesDic[item];
}
else
{
foreach (var item in texturesDic.Keys)
{
if (item.Equals(pptName))
return texturesDic[item];
}
} }
return null; return null;
} }
@ -78,42 +72,75 @@ public static class LoadPPTTest
/// 获取文件夹下所有.pptx文件 /// 获取文件夹下所有.pptx文件
/// </summary> /// </summary>
/// <param name="folderPath"></param> /// <param name="folderPath"></param>
public static void PPTFiles(string folderPath) public static async void PPTFiles(string folderPath)
{ {
string fullPPTFolderPath = Path.Combine(Application.streamingAssetsPath, folderPath); string fullPPTFolderPath = Path.Combine(Application.streamingAssetsPath, folderPath);
pptsPath = Directory.GetFiles(fullPPTFolderPath, "*.pptx"); pptsPath = Directory.GetFiles(fullPPTFolderPath, "*.pptx");
for (int i = 0; i < pptsPath.Length; i++)
{
string pptName = PathName(pptsPath[i]);
await LoadAllPPTFiles(pptName, pptsPath[i]);
}
} }
public static List<Texture2D> LoadAllPPTFiles(string folderPath)
public static async UniTask LoadAllPPTFiles(string pptName, string folderPath)
{ {
List<Texture2D> texture2Ds = new List<Texture2D>(); List<Texture2D> texture2Ds = new List<Texture2D>();
Presentation presentation = new Aspose.Slides.Presentation(folderPath); Presentation presentation = new Presentation(folderPath);
// 遍历文档(只做示例使用自己根据需求拓展) // 遍历文档(只做示例使用自己根据需求拓展)
for (int i = 0; i < presentation.Slides.Count; i++) for (int i = 0; i < presentation.Slides.Count; i++)
{ {
ISlide slide = presentation.Slides[i]; ISlide slide = presentation.Slides[i];
var bitmap = slide.GetThumbnail(1f, 1f); var bitmap = slide.GetThumbnail(1f, 1f);
// 声明内存流将图片转换为内存流再由流转换为byte数组然后用texture2d加载byte数组 // 声明内存流将图片转换为内存流再由流转换为byte数组然后用texture2d加载byte数组
using (MemoryStream ms = new MemoryStream()) using (MemoryStream ms = new MemoryStream())
{ {
bitmap.Save(ms, ImageFormat.Jpeg); bitmap.Save(ms, ImageFormat.Jpeg);
byte[] buff = new byte[ms.Length]; byte[] buff = new byte[ms.Length];
ms.Seek(0, SeekOrigin.Begin); ms.Seek(0, SeekOrigin.Begin);
ms.Read(buff, 0, (int)ms.Length); await ms.ReadAsync(buff, 0, (int)ms.Length);
// 注意这个image的命名空间为system.drawing不是unity.ui,这个图片的目的是提供图片的宽高 // 注意这个image的命名空间为system.drawing不是unity.ui,这个图片的目的是提供图片的宽高
System.Drawing.Image sizeImage = System.Drawing.Image.FromStream(ms); System.Drawing.Image sizeImage = System.Drawing.Image.FromStream(ms);
Texture2D texture2D = new Texture2D(sizeImage.Width, sizeImage.Height); Texture2D texture2D = new Texture2D(sizeImage.Width, sizeImage.Height);
texture2D.LoadImage(buff); texture2D.LoadImage(buff);
texture2Ds.Add(texture2D); texture2Ds.Add(texture2D);
} }
} }
texturesDic.Add(pptName, texture2Ds);
Debug.Log("所有PPT加载完成"); Debug.Log("所有PPT加载完成");
return texture2Ds;
} }
//public static List<Texture2D> LoadAllPPTFiles(string folderPath)
//{
// List<Texture2D> texture2Ds = new List<Texture2D>();
// Presentation presentation = new Presentation(folderPath);
// // 遍历文档(只做示例使用自己根据需求拓展)
// for (int i = 0; i < presentation.Slides.Count; i++)
// {
// ISlide slide = presentation.Slides[i];
// var bitmap = slide.GetThumbnail(1f, 1f);
// // 声明内存流将图片转换为内存流再由流转换为byte数组然后用texture2d加载byte数组
// using (MemoryStream ms = new MemoryStream())
// {
// bitmap.Save(ms, ImageFormat.Jpeg);
// byte[] buff = new byte[ms.Length];
// ms.Seek(0, SeekOrigin.Begin);
// ms.Read(buff, 0, (int)ms.Length);
// // 注意这个image的命名空间为system.drawing不是unity.ui,这个图片的目的是提供图片的宽高
// System.Drawing.Image sizeImage = System.Drawing.Image.FromStream(ms);
// Texture2D texture2D = new Texture2D(sizeImage.Width, sizeImage.Height);
// texture2D.LoadImage(buff);
// texture2Ds.Add(texture2D);
// }
// }
// Debug.Log("所有PPT加载完成");
// return texture2Ds;
//}
// 逐个加载每个PPT文件 // 逐个加载每个PPT文件
/// <summary> /// <summary>
@ -137,7 +164,7 @@ public static class LoadPPTTest
/// <summary> /// <summary>
/// 从文件名中提取开头的数字(如 "1-第一章" -> 返回 1 /// 从文件名中提取开头的数字(如 "1-第一章" -> 返回 1
/// </summary> /// </summary>
public static int ExtractLeadingNumber(string fileName) public static int ExtractLeadingNumber(string fileName)
{ {
if (string.IsNullOrEmpty(fileName)) return 0; if (string.IsNullOrEmpty(fileName)) return 0;

View File

@ -19,7 +19,7 @@ public class TestScene1Manager : MonoBehaviour
Bootstrap.Instance.uiManager.ShowPanel<UI_LoadingPanel>(this, E_UI_Layer.System, (panel) => Bootstrap.Instance.uiManager.ShowPanel<UI_LoadingPanel>(this, E_UI_Layer.System, (panel) =>
{ {
Bootstrap.Instance.eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 0.1f); Bootstrap.Instance.eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 0.1f);
Bootstrap.Instance.scenesManager.LoadSceneAsyn(this, "TestScene", () => Bootstrap.Instance.scenesManager.LoadSceneAsyn("TestScene", () =>
{ {
Debug.Log("加载场景成功"); Debug.Log("加载场景成功");
Bootstrap.Instance.eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 0.9f); Bootstrap.Instance.eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 0.9f);

View File

@ -20,7 +20,7 @@ public class TestSceneManager : MonoBehaviour
Bootstrap.Instance.uiManager.ShowPanel<UI_LoadingPanel>(this, E_UI_Layer.System, (panel) => Bootstrap.Instance.uiManager.ShowPanel<UI_LoadingPanel>(this, E_UI_Layer.System, (panel) =>
{ {
Bootstrap.Instance.eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 0.1f); Bootstrap.Instance.eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 0.1f);
Bootstrap.Instance.scenesManager.LoadSceneAsyn(this, "TestScene1", () => Bootstrap.Instance.scenesManager.LoadSceneAsyn("TestScene1", () =>
{ {
Debug.Log("加载场景成功"); Debug.Log("加载场景成功");
Bootstrap.Instance.eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 0.9f); Bootstrap.Instance.eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 0.9f);

View File

@ -170,7 +170,7 @@ public class UI_ExamPanel : BasePanel
switch (btnName) switch (btnName)
{ {
case "retrun_Btn": case "retrun_Btn":
Bootstrap.Instance.scenesManager.LoadSceneAsyn(this, "MenuScene", () => Bootstrap.Instance.scenesManager.LoadSceneAsyn( "MenuScene", () =>
{ {
Bootstrap.Instance.uiManager.ShowPanel<UI_SelectModePanel>(this, E_UI_Layer.Top, (panel) => Bootstrap.Instance.uiManager.ShowPanel<UI_SelectModePanel>(this, E_UI_Layer.Top, (panel) =>
{ {

View File

@ -1,6 +1,5 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using TMPro;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
@ -9,34 +8,17 @@ public class UI_LoadingPanel : BasePanel
public Slider loadSlider; public Slider loadSlider;
private float currentProgress = 0; private float currentProgress = 0;
public float targetProgress; public float speed;
public bool isLoading = false; public bool isLoading = false;
public TextMeshProUGUI Slider_Text;
public Sprite[] frames; // 将每一帧的Sprite图片依次拖入这个数组中
public float frameRate = 30f; // 每秒播放的帧数
private Image image;
protected override void Awake() protected override void Awake()
{ {
base.Awake(); base.Awake();
loadSlider = GetControl<Slider>("loadSlider"); loadSlider = GetControl<Slider>("loadSlider");
Slider_Text = GetControl<TextMeshProUGUI>("%");
image = GetControl<Image>("middleImage");
if (image == null)
{
Debug.LogError("Image component not found!");
}
StartCoroutine(Animate());
} }
public override void ShowMe() public override void ShowMe()
{ {
base.ShowMe(); base.ShowMe();
loadSlider.value = 0;
Slider_Text.text = "0%";
Debug.Log("UI_LoadingPanel ShowMe");
Bootstrap.Instance.eventCenter.AddEventListener<float>(Enum_EventType.UpdateProgress, UpdateProgress); Bootstrap.Instance.eventCenter.AddEventListener<float>(Enum_EventType.UpdateProgress, UpdateProgress);
} }
@ -44,7 +26,9 @@ public class UI_LoadingPanel : BasePanel
public override void HideMe() public override void HideMe()
{ {
base.HideMe(); base.HideMe();
Debug.Log("UI_LoadingPanel HideMe"); loadSlider.value = 0;
currentProgress = 0;
speed = 0;
Bootstrap.Instance.eventCenter.RemoveEventListener<float>(Enum_EventType.UpdateProgress, UpdateProgress); Bootstrap.Instance.eventCenter.RemoveEventListener<float>(Enum_EventType.UpdateProgress, UpdateProgress);
} }
@ -53,38 +37,23 @@ public class UI_LoadingPanel : BasePanel
{ {
if (isLoading) if (isLoading)
{ {
if (currentProgress < targetProgress) if (currentProgress < 0.98f)
{ {
currentProgress += Time.deltaTime; currentProgress += Time.deltaTime * speed;
if (currentProgress >= targetProgress)
currentProgress = targetProgress;
loadSlider.value = currentProgress; loadSlider.value = currentProgress;
// 将Slider的value0~1转为百分比0%~100%
float percent = loadSlider.value * 100f;
Slider_Text.text = Mathf.RoundToInt(percent) + "%";
} }
else else
{ {
isLoading = false; isLoading = false;
Bootstrap.Instance.uiManager.HidePanel<UI_LoadingPanel>(); HideMe();
} }
} }
} }
private void UpdateProgress(float progress) private void UpdateProgress(float speed)
{ {
isLoading = true; isLoading = true;
targetProgress += progress; this.speed = speed;
}
private IEnumerator Animate()
{
for (int i = 0; i < frames.Length; i++)
{
image.sprite = frames[i];
yield return new WaitForSeconds(1f / frameRate);
}
// 你可以在这里添加循环播放的代码,或者在特定条件下停止动画
StartCoroutine(Animate()); // 循环播放
} }
} }

View File

@ -36,7 +36,7 @@ public class UI_SelectModePanel : BasePanel
Bootstrap.Instance.eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 0.1f); Bootstrap.Instance.eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 0.1f);
Bootstrap.Instance.uiManager.ShowPanel<UI_LoadingPanel>(this, E_UI_Layer.System, (panel) => Bootstrap.Instance.uiManager.ShowPanel<UI_LoadingPanel>(this, E_UI_Layer.System, (panel) =>
{ {
Bootstrap.Instance.scenesManager.LoadSceneAsyn(this, "理论考核", () => Bootstrap.Instance.scenesManager.LoadSceneAsyn("理论考核", () =>
{ {
Bootstrap.Instance.uiManager.ShowPanel<UI_MainTitlePanel>(this, E_UI_Layer.Bot, (panel) => Bootstrap.Instance.uiManager.ShowPanel<UI_MainTitlePanel>(this, E_UI_Layer.Bot, (panel) =>
{ {
@ -57,7 +57,7 @@ public class UI_SelectModePanel : BasePanel
Bootstrap.Instance.eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 0.1f); Bootstrap.Instance.eventCenter.EventTrigger(Enum_EventType.UpdateProgress, 0.1f);
Bootstrap.Instance.uiManager.ShowPanel<UI_LoadingPanel>(this, E_UI_Layer.System, (panel) => Bootstrap.Instance.uiManager.ShowPanel<UI_LoadingPanel>(this, E_UI_Layer.System, (panel) =>
{ {
Bootstrap.Instance.scenesManager.LoadSceneAsyn(this, "LiveScene", () => Bootstrap.Instance.scenesManager.LoadSceneAsyn("LiveScene", () =>
{ {
Bootstrap.Instance.uiManager.ShowPanel<UI_MainTitlePanel>(this, E_UI_Layer.Bot, (panel) => Bootstrap.Instance.uiManager.ShowPanel<UI_MainTitlePanel>(this, E_UI_Layer.Bot, (panel) =>
{ {

View File

@ -75,7 +75,7 @@ public class UI_StepsPanel : BasePanel
switch (btnName) switch (btnName)
{ {
case "retrun_Btn": case "retrun_Btn":
Bootstrap.Instance.scenesManager.LoadSceneAsyn(this, "MenuScene", () => Bootstrap.Instance.scenesManager.LoadSceneAsyn( "MenuScene", () =>
{ {
Bootstrap.Instance.uiManager.ShowPanel<UI_SelectModePanel>(this, E_UI_Layer.Top, (panel) => Bootstrap.Instance.uiManager.ShowPanel<UI_SelectModePanel>(this, E_UI_Layer.Top, (panel) =>
{ {

View File

@ -49,6 +49,8 @@ public class UI_TipsForPracticePanel : BasePanel
public override void ShowMe() public override void ShowMe()
{ {
base.ShowMe(); base.ShowMe();
string pptBtnName = Left_content.GetChild(0).GetComponent<UI_TipsForPracticePanelPPTNameItem>().PPTName_Btn.gameObject.name;
OnClick(pptBtnName);
} }
public override void HideMe() public override void HideMe()
{ {

View File

@ -6,28 +6,28 @@ EditorUserSettings:
serializedVersion: 4 serializedVersion: 4
m_ConfigSettings: m_ConfigSettings:
RecentlyUsedSceneGuid-0: RecentlyUsedSceneGuid-0:
value: 5452075006075b5f540c5c77497706444f4e4d2b2d7e27677d2b4b30b5b8316d
flags: 0
RecentlyUsedSceneGuid-1:
value: 5006505e5c0c0a5e0e0b5a2746220b44174e1d292d7b74617c2b196be0b66168 value: 5006505e5c0c0a5e0e0b5a2746220b44174e1d292d7b74617c2b196be0b66168
flags: 0 flags: 0
RecentlyUsedSceneGuid-2: RecentlyUsedSceneGuid-1:
value: 5754060550000c5e59580a2348270d44434e1b782a7b7633747e1e30e4b5616b value: 5754060550000c5e59580a2348270d44434e1b782a7b7633747e1e30e4b5616b
flags: 0 flags: 0
RecentlyUsedSceneGuid-3: RecentlyUsedSceneGuid-2:
value: 0609045307065a5d5a0d0874407b5944124f1e72787b2465287e1f36b0b8316f value: 0609045307065a5d5a0d0874407b5944124f1e72787b2465287e1f36b0b8316f
flags: 0 flags: 0
RecentlyUsedSceneGuid-4: RecentlyUsedSceneGuid-3:
value: 55085253045108025c56097646265e44124f4f7b7e7a76312c2f4960e6b1363d value: 55085253045108025c56097646265e44124f4f7b7e7a76312c2f4960e6b1363d
flags: 0 flags: 0
RecentlyUsedSceneGuid-5: RecentlyUsedSceneGuid-4:
value: 0004025352565f0d0c0d5827167b09444f4f1c282a7877677b704536b6b36c6d value: 0004025352565f0d0c0d5827167b09444f4f1c282a7877677b704536b6b36c6d
flags: 0 flags: 0
RecentlyUsedSceneGuid-5:
value: 5400065054540b095f0b5c7511255b4447151e787a2b253628794c37e7b6676e
flags: 0
RecentlyUsedSceneGuid-6: RecentlyUsedSceneGuid-6:
value: 5a5757560101590a5d0c0e24427b5d44434e4c7a7b7a23677f2b4565b7b5353a value: 5452075006075b5f540c5c77497706444f4e4d2b2d7e27677d2b4b30b5b8316d
flags: 0 flags: 0
RecentlyUsedSceneGuid-7: RecentlyUsedSceneGuid-7:
value: 5400065054540b095f0b5c7511255b4447151e787a2b253628794c37e7b6676e value: 5a5757560101590a5d0c0e24427b5d44434e4c7a7b7a23677f2b4565b7b5353a
flags: 0 flags: 0
vcSharedLogLevel: vcSharedLogLevel:
value: 0d5e400f0650 value: 0d5e400f0650