当前位置:   article > 正文

GameFrameWork框架(Unity3D)使用笔记(八) 实现场景加载进度条_gamefromwork框架

gamefromwork框架

前言:

        游戏在转换场景的时候,需要花费时间来加载相关的资源。而这个过程往往因为游戏场景的规模和复杂度以及玩家电脑配置的原因花费一小段时间(虽然这个项目里用不到)。

        所以,如果这一小段时间,画面就卡在这里,啥也做不了,玩家也不知道啥时候能加载好。这个等待的时间实际上非常地影响玩家的使用体验。

        目前大多数游戏在转换关卡这种时候都会有个加载界面,显示加载进度。这样玩家可以对啥时候能加载好有个心理预估(判断要不要因为加载太久浪费时间不如卸载游戏(开个玩笑))。

        一般加载场景显示进度条的方法搜搜就有了,就是利用Unity自带的异步加载函数SceneManager.LoadSceneAsync()加载场景,并且通过AsyncOperation跟踪加载进度,从而设置进度条之类的。

        不过,在GameFramework框架下,加载场景的模块被进一步封装,那怎么在UGF下实现加载的进度条就是本篇的主要内容。


一、实现过程讲解

        我看过一些非GF的加载场景的方案,大多数都是:对于从场景a-->场景b的过程,将其变为从场景a-->场景c-->场景b

        其中,场景c里面主要就只有一个加载界面,主要用来显示进度条等内容。这样的话,从a->c可以非常快速地跳转(因为c中就只有个UI所以即便配置不高也能很快跳转)然后玩家在c中观看进度条的时候,后台异步加载场景b,加载完毕后立刻转到场景b。

        但是,在GF框架下,有个不同的地方。就是GF框架预制体所在的Launcher场景是一直存在的,并且框架的UI统一在这个场景里管理。

        所以在GF里实现进度条的功能,我的方案是直接在ProcedureChangeScene流程里面加载新场景的同时显示进度条UI,并且在加载完成后关闭UI。

        此外,由于一些原因(参考这一篇:http://t.csdn.cn/65FDe),想要测试这个进度条的真正效果,需要把当前工程Build再运行测试。


一、拼制UI

 在场景里面拼个进度条UI:

然后写UI的逻辑脚本,这里实现控制UI进度条的函数方便之后调用:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace ShadowU
  6. {
  7. public class LoadingForm: UGuiForm
  8. {
  9. public GameObject mask; //进度条的遮罩
  10. public Text loadingText; //加载中的文字
  11. protected override void OnClose(bool isShutdown, object userData)
  12. {
  13. base.OnClose(isShutdown, userData);
  14. }
  15. protected override void OnInit(object userData)
  16. {
  17. base.OnInit(userData);
  18. }
  19. protected override void OnOpen(object userData)
  20. {
  21. base.OnOpen(userData);
  22. loadingText.text = "加载中. . . . . . ";
  23. }
  24. protected override void OnUpdate(float elapseSeconds, float realElapseSeconds)
  25. {
  26. base.OnUpdate(elapseSeconds, realElapseSeconds);
  27. //控制加载中文字后的句号浮动
  28. string temp = loadingText.text;
  29. temp.Insert(3, temp[temp.Length - 1].ToString()).Remove(temp.Length - 1);
  30. loadingText.text = temp;
  31. }
  32. void SetProcess(float a)
  33. {
  34. a = Mathf.Clamp(a,0f,1.0f);//将数值控制在0-1
  35. mask.transform.SetLocalScaleX(a);
  36. }
  37. }
  38. }

接上脚本,设置好public参数,做成预制体:


二、修改ProcedureChangeScene流程代码

         ProcedureChangeScene开始的时候,加载出Loading界面,同时开始异步加载新场景。

        通过LoadSceneUpdateEvent事件来更新加载界面进度条的进度。

        最后,在场景加载完成后关闭Loading界面即可。

        新的ProcedureChangeScene代码如下:

  1. using GameFramework.Fsm;
  2. using GameFramework.Event;
  3. using GameFramework.DataTable;
  4. using UnityGameFramework.Runtime;
  5. using UnityEngine;
  6. using UnityEngine.SceneManagement;
  7. using System;
  8. namespace ShadowU
  9. {
  10. public class ProcedureChangeScene : ProcedureBase
  11. {
  12. private const int MenuSceneId = 1; //菜单场景的ID
  13. private bool m_ChangeToMenu = false; //是否转换为菜单
  14. private bool m_IsChangeSceneComplete = false; //转换场景是否结束
  15. private LoadingForm m_LoadingForm; //加载界面
  16. public override bool UseNativeDialog
  17. {
  18. get
  19. {
  20. return true;
  21. }
  22. }
  23. protected override void OnEnter(IFsm<GameFramework.Procedure.IProcedureManager> procedureOwner)
  24. {
  25. base.OnEnter(procedureOwner);
  26. Debug.Log("ChangeScene!");
  27. m_IsChangeSceneComplete = false; //如果为true的话 则在update中会切换流程
  28. //打开加载界面
  29. GameEntry.UI.OpenUIForm(AssetUtility.GetUIFormAsset("LoadingForm"), "Default", 1, this);
  30. //注册事件
  31. GameEntry.Event.Subscribe(LoadSceneSuccessEventArgs.EventId,OnLoadSceneSuccess);
  32. GameEntry.Event.Subscribe(LoadSceneFailureEventArgs.EventId, OnLoadSceneFailure);
  33. GameEntry.Event.Subscribe(LoadSceneUpdateEventArgs.EventId, OnLoadSceneUpdate);
  34. GameEntry.Event.Subscribe(LoadSceneDependencyAssetEventArgs.EventId, OnLoadSceneDependencyAsset);
  35. GameEntry.Event.Subscribe(OpenUIFormSuccessEventArgs.EventId,OnOpenUIFormSuccess);
  36. //隐藏所有实体
  37. GameEntry.Entity.HideAllLoadingEntities();
  38. GameEntry.Entity.HideAllLoadedEntities();
  39. //卸载所有场景
  40. string[] loadedSceneAssetNames = GameEntry.Scene.GetLoadedSceneAssetNames();
  41. foreach (string sn in loadedSceneAssetNames)
  42. GameEntry.Scene.UnloadScene(sn);
  43. //还原游戏速度
  44. GameEntry.Base.ResetNormalGameSpeed();
  45. //获取下一个场景的ID
  46. int sceneId = procedureOwner.GetData<VarInt32>("NextSceneId");
  47. m_ChangeToMenu = sceneId == MenuSceneId; //判断是否是转到菜单
  48. IDataTable<DRScene> dtScene = GameEntry.DataTable.GetDataTable<DRScene>();
  49. DRScene drScene = dtScene.GetDataRow(sceneId); //获取数据表行
  50. if(drScene == null)
  51. {
  52. Log.Warning("Can not load scene '{0}' from data table.", sceneId.ToString());
  53. return;
  54. }
  55. GameEntry.Scene.LoadScene(AssetUtility.GetSceneAsset(drScene.AssetName),Constant.AssetPriority.SceneAsset,this);
  56. }
  57. protected override void OnLeave(IFsm<GameFramework.Procedure.IProcedureManager> procedureOwner, bool isShutdown)
  58. {
  59. base.OnLeave(procedureOwner, isShutdown);
  60. GameEntry.Event.Unsubscribe(LoadSceneSuccessEventArgs.EventId, OnLoadSceneSuccess);
  61. GameEntry.Event.Unsubscribe(LoadSceneFailureEventArgs.EventId, OnLoadSceneFailure);
  62. GameEntry.Event.Unsubscribe(LoadSceneUpdateEventArgs.EventId, OnLoadSceneUpdate);
  63. GameEntry.Event.Unsubscribe(LoadSceneDependencyAssetEventArgs.EventId, OnLoadSceneDependencyAsset);
  64. GameEntry.Event.Unsubscribe(OpenUIFormSuccessEventArgs.EventId,OnOpenUIFormSuccess);
  65. //关闭UI
  66. if (m_LoadingForm != null)
  67. {
  68. m_LoadingForm.Close(isShutdown);
  69. m_LoadingForm = null;
  70. }
  71. }
  72. protected override void OnUpdate(IFsm<GameFramework.Procedure.IProcedureManager> procedureOwner, float elapseSeconds, float realElapseSeconds)
  73. {
  74. base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);
  75. if (!m_IsChangeSceneComplete)
  76. {
  77. return; //还没完成场景切换
  78. }
  79. if (m_ChangeToMenu)
  80. {
  81. ChangeState<ProcedureMenu>(procedureOwner); //菜单
  82. }else
  83. {
  84. ChangeState<ProcedureMain>(procedureOwner); //游戏运行流程
  85. }
  86. }
  87. private void OnLoadSceneSuccess(object sender,GameEventArgs e)
  88. {
  89. LoadSceneSuccessEventArgs ne = (LoadSceneSuccessEventArgs)e;
  90. if(ne.UserData != this)
  91. {
  92. return;
  93. }
  94. Log.Info("Load scene '{0}' OK.", ne.SceneAssetName);
  95. m_IsChangeSceneComplete = true;
  96. }
  97. private void OnLoadSceneFailure(object sender, GameEventArgs e)
  98. {
  99. LoadSceneFailureEventArgs ne = (LoadSceneFailureEventArgs)e;
  100. if (ne.UserData != this)
  101. {
  102. return;
  103. }
  104. Log.Error("Load scene '{0}' failure, error message '{1}'.", ne.SceneAssetName, ne.ErrorMessage);
  105. }
  106. private void OnLoadSceneUpdate(object sender, GameEventArgs e)
  107. {
  108. LoadSceneUpdateEventArgs ne = (LoadSceneUpdateEventArgs)e;
  109. if (ne.UserData != this)
  110. {
  111. return;
  112. }
  113. Log.Info("Load scene '{0}' update, progress '{1}'.", ne.SceneAssetName, ne.Progress.ToString("P2"));
  114. //更新加载界面的进度条
  115. if(m_LoadingForm != null)
  116. m_LoadingForm.SetProcess(ne.Progress);
  117. }
  118. private void OnLoadSceneDependencyAsset(object sender, GameEventArgs e)
  119. {
  120. LoadSceneDependencyAssetEventArgs ne = (LoadSceneDependencyAssetEventArgs)e;
  121. if (ne.UserData != this)
  122. {
  123. return;
  124. }
  125. Log.Info("Load scene '{0}' dependency asset '{1}', count '{2}/{3}'.", ne.SceneAssetName, ne.DependencyAssetName, ne.LoadedCount.ToString(), ne.TotalCount.ToString());
  126. }
  127. private void OnOpenUIFormSuccess(object sender, GameEventArgs e)
  128. {
  129. OpenUIFormSuccessEventArgs ne = (OpenUIFormSuccessEventArgs)e;
  130. if (ne.UserData != this)
  131. {
  132. return;
  133. }
  134. m_LoadingForm = (LoadingForm)ne.UIForm.Logic;
  135. }
  136. }
  137. }

 三、测试效果

        直接编辑器模式运行的话看场景加载并不是异步的,所以要测试这个进度条的真正效果需要打包运行。

        关于打包的过程之后的文章里会讲,这里就先跳过了。

        测试:

 进度条的功能是完成了!

但是。。。又出了新的问题:菜单界面在加载界面之上。

不过编辑器模式运行并没有这个问题:

    

但是没有问题只是看上去的。。。如果我立即暂停后观察,其实这个模式下也会两个UI同屏,只不过相对来说时间比较短暂。我经过一段时间分析后,想到UGuiForm里面在关闭和打开界面的时候都有淡入淡出的逻辑,于是作出如下假设:

加载界面看到菜单界面并不是渲染层级先后的问题,而是它们都在淡入或淡出过程中。所以说,他们都处于半透明状态所以能都看得见。

(因为基于这个假设能解决问题那我就不去证明我这个假设到底对不对了嘿嘿~) 

那解决办法就很灵活了。比如可以判断取消打开加载界面的淡入效果。不过我觉得这样改结构会很不美观。

于是我打算这样处理:让加载界面至少停留一秒。同时减小淡入淡出的时间。

在UGuiForm.cs里面修改淡入淡出时间

修改一下ProcedureChangeScene的代码(增加了和loadingTimer变量有关的部分):

  1. using GameFramework.Fsm;
  2. using GameFramework.Event;
  3. using GameFramework.DataTable;
  4. using UnityGameFramework.Runtime;
  5. using UnityEngine;
  6. using UnityEngine.SceneManagement;
  7. using System;
  8. namespace ShadowU
  9. {
  10. public class ProcedureChangeScene : ProcedureBase
  11. {
  12. private const int MenuSceneId = 1; //菜单场景的ID
  13. private bool m_ChangeToMenu = false; //是否转换为菜单
  14. private bool m_IsChangeSceneComplete = false; //转换场景是否结束
  15. private LoadingForm m_LoadingForm; //加载界面
  16. private float loadingTimer; //加载界面停留的计时器
  17. public override bool UseNativeDialog
  18. {
  19. get
  20. {
  21. return true;
  22. }
  23. }
  24. protected override void OnEnter(IFsm<GameFramework.Procedure.IProcedureManager> procedureOwner)
  25. {
  26. base.OnEnter(procedureOwner);
  27. Debug.Log("ChangeScene!");
  28. m_IsChangeSceneComplete = false; //如果为true的话 则在update中会切换流程
  29. //打开加载界面
  30. GameEntry.UI.OpenUIForm(AssetUtility.GetUIFormAsset("LoadingForm"), "Default", 1, this);
  31. loadingTimer = 1.0f; //加载界面至少停留一秒
  32. //注册事件
  33. GameEntry.Event.Subscribe(LoadSceneSuccessEventArgs.EventId,OnLoadSceneSuccess);
  34. GameEntry.Event.Subscribe(LoadSceneFailureEventArgs.EventId, OnLoadSceneFailure);
  35. GameEntry.Event.Subscribe(LoadSceneUpdateEventArgs.EventId, OnLoadSceneUpdate);
  36. GameEntry.Event.Subscribe(LoadSceneDependencyAssetEventArgs.EventId, OnLoadSceneDependencyAsset);
  37. GameEntry.Event.Subscribe(OpenUIFormSuccessEventArgs.EventId,OnOpenUIFormSuccess);
  38. //隐藏所有实体
  39. GameEntry.Entity.HideAllLoadingEntities();
  40. GameEntry.Entity.HideAllLoadedEntities();
  41. //卸载所有场景
  42. string[] loadedSceneAssetNames = GameEntry.Scene.GetLoadedSceneAssetNames();
  43. foreach (string sn in loadedSceneAssetNames)
  44. GameEntry.Scene.UnloadScene(sn);
  45. //还原游戏速度
  46. GameEntry.Base.ResetNormalGameSpeed();
  47. //获取下一个场景的ID
  48. int sceneId = procedureOwner.GetData<VarInt32>("NextSceneId");
  49. m_ChangeToMenu = sceneId == MenuSceneId; //判断是否是转到菜单
  50. IDataTable<DRScene> dtScene = GameEntry.DataTable.GetDataTable<DRScene>();
  51. DRScene drScene = dtScene.GetDataRow(sceneId); //获取数据表行
  52. if(drScene == null)
  53. {
  54. Log.Warning("Can not load scene '{0}' from data table.", sceneId.ToString());
  55. return;
  56. }
  57. GameEntry.Scene.LoadScene(AssetUtility.GetSceneAsset(drScene.AssetName),Constant.AssetPriority.SceneAsset,this);
  58. }
  59. protected override void OnLeave(IFsm<GameFramework.Procedure.IProcedureManager> procedureOwner, bool isShutdown)
  60. {
  61. base.OnLeave(procedureOwner, isShutdown);
  62. GameEntry.Event.Unsubscribe(LoadSceneSuccessEventArgs.EventId, OnLoadSceneSuccess);
  63. GameEntry.Event.Unsubscribe(LoadSceneFailureEventArgs.EventId, OnLoadSceneFailure);
  64. GameEntry.Event.Unsubscribe(LoadSceneUpdateEventArgs.EventId, OnLoadSceneUpdate);
  65. GameEntry.Event.Unsubscribe(LoadSceneDependencyAssetEventArgs.EventId, OnLoadSceneDependencyAsset);
  66. GameEntry.Event.Unsubscribe(OpenUIFormSuccessEventArgs.EventId,OnOpenUIFormSuccess);
  67. //关闭UI
  68. if (m_LoadingForm != null)
  69. {
  70. m_LoadingForm.Close(isShutdown);
  71. m_LoadingForm = null;
  72. }
  73. }
  74. protected override void OnUpdate(IFsm<GameFramework.Procedure.IProcedureManager> procedureOwner, float elapseSeconds, float realElapseSeconds)
  75. {
  76. base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);
  77. if (!m_IsChangeSceneComplete)
  78. {
  79. return; //还没完成场景切换
  80. }
  81. m_LoadingForm.SetProcess(1);
  82. if(loadingTimer > 0)
  83. {
  84. loadingTimer -= elapseSeconds;//更新计时器
  85. return;
  86. }
  87. if (m_ChangeToMenu)
  88. {
  89. ChangeState<ProcedureMenu>(procedureOwner); //菜单
  90. }else
  91. {
  92. ChangeState<ProcedureMain>(procedureOwner); //游戏运行流程
  93. }
  94. }
  95. private void OnLoadSceneSuccess(object sender,GameEventArgs e)
  96. {
  97. LoadSceneSuccessEventArgs ne = (LoadSceneSuccessEventArgs)e;
  98. if(ne.UserData != this)
  99. {
  100. return;
  101. }
  102. Log.Info("Load scene '{0}' OK.", ne.SceneAssetName);
  103. m_IsChangeSceneComplete = true;
  104. }
  105. private void OnLoadSceneFailure(object sender, GameEventArgs e)
  106. {
  107. LoadSceneFailureEventArgs ne = (LoadSceneFailureEventArgs)e;
  108. if (ne.UserData != this)
  109. {
  110. return;
  111. }
  112. Log.Error("Load scene '{0}' failure, error message '{1}'.", ne.SceneAssetName, ne.ErrorMessage);
  113. }
  114. private void OnLoadSceneUpdate(object sender, GameEventArgs e)
  115. {
  116. LoadSceneUpdateEventArgs ne = (LoadSceneUpdateEventArgs)e;
  117. if (ne.UserData != this)
  118. {
  119. return;
  120. }
  121. Log.Info("Load scene '{0}' update, progress '{1}'.", ne.SceneAssetName, ne.Progress.ToString("P2"));
  122. //更新加载界面的进度条
  123. if(m_LoadingForm != null)
  124. m_LoadingForm.SetProcess(ne.Progress);
  125. }
  126. private void OnLoadSceneDependencyAsset(object sender, GameEventArgs e)
  127. {
  128. LoadSceneDependencyAssetEventArgs ne = (LoadSceneDependencyAssetEventArgs)e;
  129. if (ne.UserData != this)
  130. {
  131. return;
  132. }
  133. Log.Info("Load scene '{0}' dependency asset '{1}', count '{2}/{3}'.", ne.SceneAssetName, ne.DependencyAssetName, ne.LoadedCount.ToString(), ne.TotalCount.ToString());
  134. }
  135. private void OnOpenUIFormSuccess(object sender, GameEventArgs e)
  136. {
  137. OpenUIFormSuccessEventArgs ne = (OpenUIFormSuccessEventArgs)e;
  138. if (ne.UserData != this)
  139. {
  140. return;
  141. }
  142. m_LoadingForm = (LoadingForm)ne.UIForm.Logic;
  143. }
  144. }
  145. }

此外还有个细节:就是进度条到90就不动了。其实场景加载到90%的时候就已经加载完了,最后的10%就在于有没有把场景显示出来。那为了看起来不膈应,我上面的代码里在加载完场景后手动把进度条设置为100:

打包看下最终效果:

 效果挺理想的,那就这样了!

        

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/102627?site
推荐阅读
相关标签
  

闽ICP备14008679号