ApplicationManager.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Collections.Generic;
  5. using FrameWork.SDKManager;
  6. public class ApplicationManager : MonoBehaviour
  7. {
  8. private static ApplicationManager instance;
  9. public static ApplicationManager Instance
  10. {
  11. get {
  12. if (instance == null)
  13. {
  14. instance = FindObjectOfType<ApplicationManager>();
  15. }
  16. return ApplicationManager.instance; }
  17. set { ApplicationManager.instance = value; }
  18. }
  19. public AppMode m_AppMode = AppMode.Developing;
  20. public bool m_useAssetsBundle = false;
  21. public static AppMode AppMode
  22. {
  23. get
  24. {
  25. #if APPMODE_DEV
  26. return AppMode.Developing;
  27. #elif APPMODE_QA
  28. return AppMode.QA;
  29. #elif APPMODE_REL
  30. return AppMode.Release;
  31. #else
  32. return instance.m_AppMode;
  33. #endif
  34. }
  35. }
  36. public bool UseAssetsBundle
  37. {
  38. get
  39. {
  40. #if USE_BUNDLE
  41. return true;
  42. #else
  43. return m_useAssetsBundle;
  44. #endif
  45. }
  46. }
  47. public static string Version
  48. {
  49. get
  50. {
  51. return Application.version + "." + HotUpdateManager.GetHotUpdateVersion();
  52. }
  53. }
  54. public static SystemLanguage Langguage
  55. {
  56. get
  57. {
  58. if (Application.isPlaying)
  59. return instance.langguage;
  60. else
  61. return Application.systemLanguage;
  62. }
  63. set
  64. {
  65. instance.langguage = value;
  66. }
  67. }
  68. [Tooltip("是否记录输入到本地")]
  69. public bool m_recordInput = true;
  70. //快速启动
  71. public bool m_quickLunch = true;
  72. [HideInInspector]
  73. public string m_Status = "";
  74. [HideInInspector]
  75. public List<string> m_globalLogic;
  76. [HideInInspector]
  77. public string currentStatus;
  78. /// <summary>
  79. /// 语言
  80. /// </summary>
  81. public SystemLanguage langguage = SystemLanguage.ChineseSimplified;
  82. /// <summary>
  83. /// 显示括号标识多语言转换的字段
  84. /// </summary>
  85. public bool showLanguageValue = false;
  86. public void Awake()
  87. {
  88. instance = this;
  89. AppLaunch();
  90. }
  91. /// <summary>
  92. /// 程序启动
  93. /// </summary>
  94. public void AppLaunch()
  95. {
  96. DontDestroyOnLoad(gameObject);
  97. Application.runInBackground = true;
  98. Screen.sleepTimeout = SleepTimeout.NeverSleep;
  99. SetResourceLoadType(); //设置资源加载类型
  100. AudioPlayManager.Init();
  101. MemoryManager.Init(); //内存管理初始化
  102. Timer.Init(); //计时器启动
  103. InputManager.Init(); //输入管理器启动
  104. #if !UNITY_WEBGL
  105. UIManager.Init(); //UIManager启动
  106. #else
  107. UIManager.InitAsync(); //异步加载UIManager
  108. #endif
  109. ApplicationStatusManager.Init(); //游戏流程状态机初始化
  110. GlobalLogicManager.Init(); //初始化全局逻辑
  111. SDKManager.Init(); //初始化SDKManger
  112. if (AppMode != AppMode.Release)
  113. {
  114. GUIConsole.Init(); //运行时Console
  115. DevelopReplayManager.OnLunchCallBack += () =>
  116. {
  117. #if USE_LUA
  118. LuaManager.Init();
  119. #endif
  120. InitGlobalLogic(); //全局逻辑
  121. ApplicationStatusManager.EnterTestModel(m_Status);//可以从此处进入测试流程
  122. };
  123. DevelopReplayManager.Init(m_quickLunch); //开发者复盘管理器
  124. }
  125. else
  126. {
  127. Log.Init(false); //关闭 Debug
  128. #if USE_LUA
  129. LuaManager.Init();
  130. #endif
  131. InitGlobalLogic(); //全局逻辑
  132. ApplicationStatusManager.EnterStatus(m_Status);//游戏流程状态机,开始第一个状态
  133. }
  134. }
  135. #region 程序生命周期事件派发
  136. public static ApplicationVoidCallback s_OnApplicationQuit = null;
  137. public static ApplicationBoolCallback s_OnApplicationPause = null;
  138. public static ApplicationBoolCallback s_OnApplicationFocus = null;
  139. public static ApplicationVoidCallback s_OnApplicationUpdate = null;
  140. public static ApplicationVoidCallback s_OnApplicationFixedUpdate = null;
  141. public static ApplicationVoidCallback s_OnApplicationOnGUI = null;
  142. public static ApplicationVoidCallback s_OnApplicationOnDrawGizmos = null;
  143. public static ApplicationVoidCallback s_OnApplicationLateUpdate = null;
  144. void OnApplicationQuit()
  145. {
  146. if (s_OnApplicationQuit != null)
  147. {
  148. try
  149. {
  150. s_OnApplicationQuit();
  151. }
  152. catch (Exception e)
  153. {
  154. Debug.LogError(e.ToString());
  155. }
  156. }
  157. }
  158. /*
  159. * 强制暂停时,先 OnApplicationPause,后 OnApplicationFocus
  160. * 重新“启动”游戏时,先OnApplicationFocus,后 OnApplicationPause
  161. */
  162. void OnApplicationPause(bool pauseStatus)
  163. {
  164. if (s_OnApplicationPause != null)
  165. {
  166. try
  167. {
  168. s_OnApplicationPause(pauseStatus);
  169. }
  170. catch (Exception e)
  171. {
  172. Debug.LogError(e.ToString());
  173. }
  174. }
  175. }
  176. void OnApplicationFocus(bool focusStatus)
  177. {
  178. if (s_OnApplicationFocus != null)
  179. {
  180. try
  181. {
  182. s_OnApplicationFocus(focusStatus);
  183. }
  184. catch (Exception e)
  185. {
  186. Debug.LogError(e.ToString());
  187. }
  188. }
  189. }
  190. void Update()
  191. {
  192. if (s_OnApplicationUpdate != null)
  193. s_OnApplicationUpdate();
  194. }
  195. private void LateUpdate()
  196. {
  197. if(s_OnApplicationLateUpdate != null)
  198. {
  199. s_OnApplicationLateUpdate();
  200. }
  201. }
  202. private void FixedUpdate()
  203. {
  204. if (s_OnApplicationFixedUpdate != null)
  205. s_OnApplicationFixedUpdate();
  206. }
  207. void OnGUI()
  208. {
  209. if (s_OnApplicationOnGUI != null)
  210. s_OnApplicationOnGUI();
  211. }
  212. private void OnDrawGizmos()
  213. {
  214. if (s_OnApplicationOnDrawGizmos != null)
  215. s_OnApplicationOnDrawGizmos();
  216. }
  217. #endregion
  218. #region 程序启动细节
  219. /// <summary>
  220. /// 设置资源加载方式
  221. /// </summary>
  222. void SetResourceLoadType()
  223. {
  224. if (UseAssetsBundle)
  225. {
  226. ResourceManager.m_gameLoadType = ResLoadLocation.Streaming;
  227. }
  228. else
  229. {
  230. ResourceManager.m_gameLoadType = ResLoadLocation.Resource;
  231. }
  232. }
  233. /// <summary>
  234. /// 初始化全局逻辑
  235. /// </summary>
  236. void InitGlobalLogic()
  237. {
  238. for (int i = 0; i < m_globalLogic.Count; i++)
  239. {
  240. GlobalLogicManager.InitLogic(m_globalLogic[i]);
  241. }
  242. }
  243. #endregion
  244. }
  245. public enum AppMode
  246. {
  247. Developing,
  248. QA,
  249. Release
  250. }
  251. public delegate void ApplicationBoolCallback(bool status);
  252. public delegate void ApplicationVoidCallback();