ProcedurePreload.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. };
  32. private Dictionary<string, bool> m_LoadedFlag = new Dictionary<string, bool>();
  33. public override bool UseNativeDialog
  34. {
  35. get
  36. {
  37. return true;
  38. }
  39. }
  40. protected override void OnEnter(ProcedureOwner procedureOwner)
  41. {
  42. base.OnEnter(procedureOwner);
  43. GameEntry.Event.Subscribe(LoadConfigSuccessEventArgs.EventId, OnLoadConfigSuccess);
  44. GameEntry.Event.Subscribe(LoadConfigFailureEventArgs.EventId, OnLoadConfigFailure);
  45. GameEntry.Event.Subscribe(LoadDataTableSuccessEventArgs.EventId, OnLoadDataTableSuccess);
  46. GameEntry.Event.Subscribe(LoadDataTableFailureEventArgs.EventId, OnLoadDataTableFailure);
  47. GameEntry.Event.Subscribe(LoadDictionarySuccessEventArgs.EventId, OnLoadDictionarySuccess);
  48. GameEntry.Event.Subscribe(LoadDictionaryFailureEventArgs.EventId, OnLoadDictionaryFailure);
  49. m_LoadedFlag.Clear();
  50. PreloadResources();
  51. }
  52. protected override void OnLeave(ProcedureOwner procedureOwner, bool isShutdown)
  53. {
  54. GameEntry.Event.Unsubscribe(LoadConfigSuccessEventArgs.EventId, OnLoadConfigSuccess);
  55. GameEntry.Event.Unsubscribe(LoadConfigFailureEventArgs.EventId, OnLoadConfigFailure);
  56. GameEntry.Event.Unsubscribe(LoadDataTableSuccessEventArgs.EventId, OnLoadDataTableSuccess);
  57. GameEntry.Event.Unsubscribe(LoadDataTableFailureEventArgs.EventId, OnLoadDataTableFailure);
  58. GameEntry.Event.Unsubscribe(LoadDictionarySuccessEventArgs.EventId, OnLoadDictionarySuccess);
  59. GameEntry.Event.Unsubscribe(LoadDictionaryFailureEventArgs.EventId, OnLoadDictionaryFailure);
  60. base.OnLeave(procedureOwner, isShutdown);
  61. }
  62. protected override void OnUpdate(ProcedureOwner procedureOwner, float elapseSeconds, float realElapseSeconds)
  63. {
  64. base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);
  65. foreach (KeyValuePair<string, bool> loadedFlag in m_LoadedFlag)
  66. {
  67. if (!loadedFlag.Value)
  68. {
  69. return;
  70. }
  71. }
  72. procedureOwner.SetData<VarInt32>("NextSceneId", GameEntry.Config.GetInt("Scene.CustomRole"));
  73. ChangeState<ProcedureChangeScene>(procedureOwner);
  74. }
  75. private void PreloadResources()
  76. {
  77. // Preload configs
  78. LoadConfig("DefaultConfig");
  79. // Preload data tables
  80. foreach (string dataTableName in DataTableNames)
  81. {
  82. LoadDataTable(dataTableName);
  83. }
  84. // Preload dictionaries
  85. LoadDictionary("Default");
  86. // Preload fonts
  87. LoadFont("MainFont");
  88. }
  89. private void LoadConfig(string configName)
  90. {
  91. string configAssetName = AssetUtility.GetConfigAsset(configName, false);
  92. m_LoadedFlag.Add(configAssetName, false);
  93. GameEntry.Config.ReadData(configAssetName, this);
  94. }
  95. private void LoadDataTable(string dataTableName)
  96. {
  97. string dataTableAssetName = AssetUtility.GetDataTableAsset(dataTableName, false);
  98. m_LoadedFlag.Add(dataTableAssetName, false);
  99. GameEntry.DataTable.LoadDataTable(dataTableName, dataTableAssetName, this);
  100. }
  101. private void LoadDictionary(string dictionaryName)
  102. {
  103. string dictionaryAssetName = AssetUtility.GetDictionaryAsset(dictionaryName, false);
  104. m_LoadedFlag.Add(dictionaryAssetName, false);
  105. GameEntry.Localization.ReadData(dictionaryAssetName, this);
  106. }
  107. private void LoadFont(string fontName)
  108. {
  109. m_LoadedFlag.Add(Utility.Text.Format("Font.{0}", fontName), false);
  110. GameEntry.Resource.LoadAsset(AssetUtility.GetFontAsset(fontName), Constant.AssetPriority.FontAsset, new LoadAssetCallbacks(
  111. (assetName, asset, duration, userData) =>
  112. {
  113. m_LoadedFlag[Utility.Text.Format("Font.{0}", fontName)] = true;
  114. UGuiForm.SetMainFont((Font)asset);
  115. Log.Info("Load font '{0}' OK.", fontName);
  116. },
  117. (assetName, status, errorMessage, userData) =>
  118. {
  119. Log.Error("Can not load font '{0}' from '{1}' with error message '{2}'.", fontName, assetName, errorMessage);
  120. }));
  121. }
  122. private void OnLoadConfigSuccess(object sender, GameEventArgs e)
  123. {
  124. LoadConfigSuccessEventArgs ne = (LoadConfigSuccessEventArgs)e;
  125. if (ne.UserData != this)
  126. {
  127. return;
  128. }
  129. m_LoadedFlag[ne.ConfigAssetName] = true;
  130. Log.Info("Load config '{0}' OK.", ne.ConfigAssetName);
  131. }
  132. private void OnLoadConfigFailure(object sender, GameEventArgs e)
  133. {
  134. LoadConfigFailureEventArgs ne = (LoadConfigFailureEventArgs)e;
  135. if (ne.UserData != this)
  136. {
  137. return;
  138. }
  139. Log.Error("Can not load config '{0}' from '{1}' with error message '{2}'.", ne.ConfigAssetName, ne.ConfigAssetName, ne.ErrorMessage);
  140. }
  141. private void OnLoadDataTableSuccess(object sender, GameEventArgs e)
  142. {
  143. LoadDataTableSuccessEventArgs ne = (LoadDataTableSuccessEventArgs)e;
  144. if (ne.UserData != this)
  145. {
  146. return;
  147. }
  148. m_LoadedFlag[ne.DataTableAssetName] = true;
  149. Log.Info("Load data table '{0}' OK.", ne.DataTableAssetName);
  150. }
  151. private void OnLoadDataTableFailure(object sender, GameEventArgs e)
  152. {
  153. LoadDataTableFailureEventArgs ne = (LoadDataTableFailureEventArgs)e;
  154. if (ne.UserData != this)
  155. {
  156. return;
  157. }
  158. Log.Error("Can not load data table '{0}' from '{1}' with error message '{2}'.", ne.DataTableAssetName, ne.DataTableAssetName, ne.ErrorMessage);
  159. }
  160. private void OnLoadDictionarySuccess(object sender, GameEventArgs e)
  161. {
  162. LoadDictionarySuccessEventArgs ne = (LoadDictionarySuccessEventArgs)e;
  163. if (ne.UserData != this)
  164. {
  165. return;
  166. }
  167. m_LoadedFlag[ne.DictionaryAssetName] = true;
  168. Log.Info("Load dictionary '{0}' OK.", ne.DictionaryAssetName);
  169. }
  170. private void OnLoadDictionaryFailure(object sender, GameEventArgs e)
  171. {
  172. LoadDictionaryFailureEventArgs ne = (LoadDictionaryFailureEventArgs)e;
  173. if (ne.UserData != this)
  174. {
  175. return;
  176. }
  177. Log.Error("Can not load dictionary '{0}' from '{1}' with error message '{2}'.", ne.DictionaryAssetName, ne.DictionaryAssetName, ne.ErrorMessage);
  178. }
  179. }
  180. }