AppManager.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using UnityEngine;
  2. using XGame.Framework.Asset;
  3. using XGame.Framework.Asyncs;
  4. namespace XGame.Framework
  5. {
  6. public partial class AppManager : IAppManager
  7. {
  8. private GameObject _go;
  9. private Launch _launch;
  10. private AppStatus _status = AppStatus.Closed;
  11. Transform IAppManager.Root => _go.transform;
  12. /// <summary>
  13. /// 启动
  14. /// </summary>
  15. void IAppManager.Start()
  16. {
  17. switch (_status)
  18. {
  19. case AppStatus.Started:
  20. {
  21. Log.Error("AppManager::Start App已启动...");
  22. break;
  23. }
  24. case AppStatus.Restarting:
  25. {
  26. Log.Error("AppManager::Start App正在重启中...");
  27. break;
  28. }
  29. case AppStatus.Closing:
  30. {
  31. Log.Error("AppManager::Start App正在关闭中...");
  32. break;
  33. }
  34. case AppStatus.Closed:
  35. {
  36. DoStart();
  37. _status = AppStatus.Started;
  38. break;
  39. }
  40. default:
  41. break;
  42. }
  43. }
  44. /// <summary>
  45. /// 重启
  46. /// </summary>
  47. IAsync IAppManager.Restart()
  48. {
  49. switch (_status)
  50. {
  51. case AppStatus.Started:
  52. {
  53. _status = AppStatus.Restarting;
  54. IAsync assetAsync = Shutdown();
  55. if (assetAsync == null)
  56. {
  57. DoStart();
  58. _status = AppStatus.Started;
  59. }
  60. else
  61. {
  62. assetAsync.Then(a =>
  63. {
  64. DoStart();
  65. _status = AppStatus.Started;
  66. });
  67. }
  68. return assetAsync;
  69. }
  70. case AppStatus.Restarting:
  71. {
  72. Log.Warn("AppManager::Restart App正在重启中....");
  73. return null;
  74. }
  75. case AppStatus.Closing:
  76. {
  77. Log.Warn("AppManager::Restart App正在关闭中....");
  78. return null;
  79. }
  80. case AppStatus.Closed:
  81. {
  82. DoStart();
  83. _status = AppStatus.Started;
  84. return null;
  85. }
  86. default:
  87. return null;
  88. }
  89. }
  90. /// <summary>
  91. /// 关闭
  92. /// </summary>
  93. IAsync IAppManager.Close()
  94. {
  95. switch (_status)
  96. {
  97. case AppStatus.Started:
  98. {
  99. _status = AppStatus.Closing;
  100. IAsync assetAsync = Shutdown();
  101. if (assetAsync == null)
  102. _status = AppStatus.Closed;
  103. else
  104. assetAsync.Then(a => _status = AppStatus.Closed);
  105. return assetAsync;
  106. }
  107. case AppStatus.Restarting:
  108. {
  109. Log.Error("AppManager::Close App正在关闭中");
  110. return null;
  111. }
  112. case AppStatus.Closing:
  113. {
  114. Log.Error("AppManager::Close App正在关闭中");
  115. return null;
  116. }
  117. case AppStatus.Closed:
  118. {
  119. Log.Error("AppManager::Close App还未启动...");
  120. return null;
  121. }
  122. default:
  123. return null;
  124. }
  125. }
  126. private void DoStart()
  127. {
  128. _go = new GameObject("Root");
  129. _launch = _go.AddComponent<Launch>();
  130. Object.DontDestroyOnLoad(_go);
  131. }
  132. private IAsync Shutdown()
  133. {
  134. _status = AppStatus.Closing;
  135. OnShutdown();
  136. return AssetManager.Dispose();
  137. }
  138. private void OnShutdown()
  139. {
  140. _launch?.Dispose();
  141. _launch = null;
  142. if (_go)
  143. {
  144. Object.Destroy(_go);
  145. _go = null;
  146. }
  147. System.GC.Collect();
  148. }
  149. }
  150. }