ProcedurePreload.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 Jiang Yin. All rights reserved.
  4. // Homepage: https://gameframework.cn/
  5. // Feedback: mailto:ellan@gameframework.cn
  6. //------------------------------------------------------------
  7. using GameFramework;
  8. using GameFramework.Event;
  9. using GameFramework.Resource;
  10. using System.Collections.Generic;
  11. using UnityEngine;
  12. using UnityGameFramework.Runtime;
  13. using ProcedureOwner = GameFramework.Fsm.IFsm<GameFramework.Procedure.IProcedureManager>;
  14. namespace MetaClient
  15. {
  16. public class ProcedurePreload : ProcedureBase
  17. {
  18. public static readonly string[] DataTableNames = new string[]
  19. {
  20. "Aircraft",
  21. "Armor",
  22. "Asteroid",
  23. "Entity",
  24. "Music",
  25. "Scene",
  26. "Sound",
  27. "Thruster",
  28. "UIForm",
  29. "UISound",
  30. "Weapon",
  31. "CustomBody",
  32. "CustomFace"
  33. };
  34. private Dictionary<string, bool> m_LoadedFlag = new Dictionary<string, bool>();
  35. public override bool UseNativeDialog
  36. {
  37. get
  38. {
  39. return true;
  40. }
  41. }
  42. protected override void OnEnter(ProcedureOwner procedureOwner)
  43. {
  44. base.OnEnter(procedureOwner);
  45. GameEntry.Event.Subscribe(LoadConfigSuccessEventArgs.EventId, OnLoadConfigSuccess);
  46. GameEntry.Event.Subscribe(LoadConfigFailureEventArgs.EventId, OnLoadConfigFailure);
  47. GameEntry.Event.Subscribe(LoadDataTableSuccessEventArgs.EventId, OnLoadDataTableSuccess);
  48. GameEntry.Event.Subscribe(LoadDataTableFailureEventArgs.EventId, OnLoadDataTableFailure);
  49. GameEntry.Event.Subscribe(LoadDictionarySuccessEventArgs.EventId, OnLoadDictionarySuccess);
  50. GameEntry.Event.Subscribe(LoadDictionaryFailureEventArgs.EventId, OnLoadDictionaryFailure);
  51. m_LoadedFlag.Clear();
  52. PreloadResources();
  53. }
  54. protected override void OnLeave(ProcedureOwner procedureOwner, bool isShutdown)
  55. {
  56. GameEntry.Event.Unsubscribe(LoadConfigSuccessEventArgs.EventId, OnLoadConfigSuccess);
  57. GameEntry.Event.Unsubscribe(LoadConfigFailureEventArgs.EventId, OnLoadConfigFailure);
  58. GameEntry.Event.Unsubscribe(LoadDataTableSuccessEventArgs.EventId, OnLoadDataTableSuccess);
  59. GameEntry.Event.Unsubscribe(LoadDataTableFailureEventArgs.EventId, OnLoadDataTableFailure);
  60. GameEntry.Event.Unsubscribe(LoadDictionarySuccessEventArgs.EventId, OnLoadDictionarySuccess);
  61. GameEntry.Event.Unsubscribe(LoadDictionaryFailureEventArgs.EventId, OnLoadDictionaryFailure);
  62. base.OnLeave(procedureOwner, isShutdown);
  63. }
  64. protected override void OnUpdate(ProcedureOwner procedureOwner, float elapseSeconds, float realElapseSeconds)
  65. {
  66. base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);
  67. foreach (KeyValuePair<string, bool> loadedFlag in m_LoadedFlag)
  68. {
  69. if (!loadedFlag.Value)
  70. {
  71. return;
  72. }
  73. }
  74. procedureOwner.SetData<VarInt32>("NextSceneId", GameEntry.Config.GetInt("Scene.CustomRole"));
  75. ChangeState<ProcedureChangeScene>(procedureOwner);
  76. }
  77. private void PreloadResources()
  78. {
  79. // Preload configs
  80. LoadConfig("DefaultConfig");
  81. // Preload data tables
  82. foreach (string dataTableName in DataTableNames)
  83. {
  84. LoadDataTable(dataTableName);
  85. }
  86. // Preload dictionaries
  87. LoadDictionary("Default");
  88. // Preload fonts
  89. LoadFont("MainFont");
  90. }
  91. private void LoadConfig(string configName)
  92. {
  93. string configAssetName = AssetUtility.GetConfigAsset(configName, false);
  94. m_LoadedFlag.Add(configAssetName, false);
  95. GameEntry.Config.ReadData(configAssetName, this);
  96. }
  97. private void LoadDataTable(string dataTableName)
  98. {
  99. string dataTableAssetName = AssetUtility.GetDataTableAsset(dataTableName, false);
  100. m_LoadedFlag.Add(dataTableAssetName, false);
  101. GameEntry.DataTable.LoadDataTable(dataTableName, dataTableAssetName, this);
  102. }
  103. private void LoadDictionary(string dictionaryName)
  104. {
  105. string dictionaryAssetName = AssetUtility.GetDictionaryAsset(dictionaryName, false);
  106. m_LoadedFlag.Add(dictionaryAssetName, false);
  107. GameEntry.Localization.ReadData(dictionaryAssetName, this);
  108. }
  109. private void LoadFont(string fontName)
  110. {
  111. m_LoadedFlag.Add(Utility.Text.Format("Font.{0}", fontName), false);
  112. GameEntry.Resource.LoadAsset(AssetUtility.GetFontAsset(fontName), Constant.AssetPriority.FontAsset, new LoadAssetCallbacks(
  113. (assetName, asset, duration, userData) =>
  114. {
  115. m_LoadedFlag[Utility.Text.Format("Font.{0}", fontName)] = true;
  116. UGuiForm.SetMainFont((Font)asset);
  117. Log.Info("Load font '{0}' OK.", fontName);
  118. },
  119. (assetName, status, errorMessage, userData) =>
  120. {
  121. Log.Error("Can not load font '{0}' from '{1}' with error message '{2}'.", fontName, assetName, errorMessage);
  122. }));
  123. }
  124. private void OnLoadConfigSuccess(object sender, GameEventArgs e)
  125. {
  126. LoadConfigSuccessEventArgs ne = (LoadConfigSuccessEventArgs)e;
  127. if (ne.UserData != this)
  128. {
  129. return;
  130. }
  131. m_LoadedFlag[ne.ConfigAssetName] = true;
  132. Log.Info("Load config '{0}' OK.", ne.ConfigAssetName);
  133. }
  134. private void OnLoadConfigFailure(object sender, GameEventArgs e)
  135. {
  136. LoadConfigFailureEventArgs ne = (LoadConfigFailureEventArgs)e;
  137. if (ne.UserData != this)
  138. {
  139. return;
  140. }
  141. Log.Error("Can not load config '{0}' from '{1}' with error message '{2}'.", ne.ConfigAssetName, ne.ConfigAssetName, ne.ErrorMessage);
  142. }
  143. private void OnLoadDataTableSuccess(object sender, GameEventArgs e)
  144. {
  145. LoadDataTableSuccessEventArgs ne = (LoadDataTableSuccessEventArgs)e;
  146. if (ne.UserData != this)
  147. {
  148. return;
  149. }
  150. m_LoadedFlag[ne.DataTableAssetName] = true;
  151. Log.Info("Load data table '{0}' OK.", ne.DataTableAssetName);
  152. }
  153. private void OnLoadDataTableFailure(object sender, GameEventArgs e)
  154. {
  155. LoadDataTableFailureEventArgs ne = (LoadDataTableFailureEventArgs)e;
  156. if (ne.UserData != this)
  157. {
  158. return;
  159. }
  160. Log.Error("Can not load data table '{0}' from '{1}' with error message '{2}'.", ne.DataTableAssetName, ne.DataTableAssetName, ne.ErrorMessage);
  161. }
  162. private void OnLoadDictionarySuccess(object sender, GameEventArgs e)
  163. {
  164. LoadDictionarySuccessEventArgs ne = (LoadDictionarySuccessEventArgs)e;
  165. if (ne.UserData != this)
  166. {
  167. return;
  168. }
  169. m_LoadedFlag[ne.DictionaryAssetName] = true;
  170. Log.Info("Load dictionary '{0}' OK.", ne.DictionaryAssetName);
  171. }
  172. private void OnLoadDictionaryFailure(object sender, GameEventArgs e)
  173. {
  174. LoadDictionaryFailureEventArgs ne = (LoadDictionaryFailureEventArgs)e;
  175. if (ne.UserData != this)
  176. {
  177. return;
  178. }
  179. Log.Error("Can not load dictionary '{0}' from '{1}' with error message '{2}'.", ne.DictionaryAssetName, ne.DictionaryAssetName, ne.ErrorMessage);
  180. }
  181. }
  182. }