ProcedurePreload.cs 7.9 KB

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