123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- using UnityEngine;
- using XGame.Framework.Asset;
- using XGame.Framework.Asyncs;
- namespace XGame.Framework
- {
- public partial class AppManager : IAppManager
- {
- private GameObject _go;
- private Launch _launch;
- private AppStatus _status = AppStatus.Closed;
- Transform IAppManager.Root => _go.transform;
- /// <summary>
- /// 启动
- /// </summary>
- void IAppManager.Start()
- {
- switch (_status)
- {
- case AppStatus.Started:
- {
- Log.Error("AppManager::Start App已启动...");
- break;
- }
- case AppStatus.Restarting:
- {
- Log.Error("AppManager::Start App正在重启中...");
- break;
- }
- case AppStatus.Closing:
- {
- Log.Error("AppManager::Start App正在关闭中...");
- break;
- }
- case AppStatus.Closed:
- {
- DoStart();
- _status = AppStatus.Started;
- break;
- }
- default:
- break;
- }
- }
- /// <summary>
- /// 重启
- /// </summary>
- IAsync IAppManager.Restart()
- {
- switch (_status)
- {
- case AppStatus.Started:
- {
- _status = AppStatus.Restarting;
- IAsync assetAsync = Shutdown();
- if (assetAsync == null)
- {
- DoStart();
- _status = AppStatus.Started;
- }
- else
- {
- assetAsync.Then(a =>
- {
- DoStart();
- _status = AppStatus.Started;
- });
- }
- return assetAsync;
- }
- case AppStatus.Restarting:
- {
- Log.Warn("AppManager::Restart App正在重启中....");
- return null;
- }
- case AppStatus.Closing:
- {
- Log.Warn("AppManager::Restart App正在关闭中....");
- return null;
- }
- case AppStatus.Closed:
- {
- DoStart();
- _status = AppStatus.Started;
- return null;
- }
- default:
- return null;
- }
- }
- /// <summary>
- /// 关闭
- /// </summary>
- IAsync IAppManager.Close()
- {
- switch (_status)
- {
- case AppStatus.Started:
- {
- _status = AppStatus.Closing;
- IAsync assetAsync = Shutdown();
- if (assetAsync == null)
- _status = AppStatus.Closed;
- else
- assetAsync.Then(a => _status = AppStatus.Closed);
- return assetAsync;
- }
- case AppStatus.Restarting:
- {
- Log.Error("AppManager::Close App正在关闭中");
- return null;
- }
- case AppStatus.Closing:
- {
- Log.Error("AppManager::Close App正在关闭中");
- return null;
- }
- case AppStatus.Closed:
- {
- Log.Error("AppManager::Close App还未启动...");
- return null;
- }
- default:
- return null;
- }
- }
- private void DoStart()
- {
- _go = new GameObject("Root");
- _launch = _go.AddComponent<Launch>();
- Object.DontDestroyOnLoad(_go);
- }
- private IAsync Shutdown()
- {
- _status = AppStatus.Closing;
- OnShutdown();
- return AssetManager.ReStart();
- }
- private void OnShutdown()
- {
- _launch?.Dispose();
- _launch = null;
- if (_go)
- {
- Object.Destroy(_go);
- _go = null;
- }
- System.GC.Collect();
- }
- }
- }
|