ProcedurePreload.cs 7.8 KB

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