AppManager.Static.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.Threading;
  2. using UnityEngine;
  3. using XGame.Framework.Asyncs;
  4. using XGame.Framework.ThreadScheduler;
  5. namespace XGame.Framework
  6. {
  7. public partial class AppManager
  8. {
  9. private static IAppManager _instance;
  10. private static IAppManager Instance => _instance ?? (_instance = new AppManager());
  11. /// <summary>
  12. /// 客户端当前帧数(非线程安全) 注意: 热重启不会归零
  13. /// </summary>
  14. public static long FrameCount { get; internal set; }
  15. /// <summary>
  16. /// 主线程ID
  17. /// </summary>
  18. public static readonly int MainThreadId = Thread.CurrentThread.ManagedThreadId;
  19. /// <summary>
  20. /// 根节点
  21. /// </summary>
  22. public static Transform Root => _instance?.Root;
  23. internal static void Initialize()
  24. {
  25. Instance.Restart();
  26. }
  27. /// <summary>
  28. /// 启动
  29. /// </summary>
  30. static public void Start()
  31. {
  32. _instance?.Start();
  33. }
  34. /// <summary>
  35. /// 关闭
  36. /// </summary>
  37. /// <returns></returns>
  38. static public IAsync Close()
  39. {
  40. AsyncGroup group = new AsyncGroup();
  41. FrameworkSchedulers.RunOnMainThread(() =>
  42. {
  43. var closeAsync = _instance?.Close();
  44. closeAsync?.Join(group);
  45. group.End();
  46. });
  47. return group;
  48. }
  49. /// <summary>
  50. /// 重启
  51. /// </summary>
  52. /// <returns></returns>
  53. static public IAsync Restart()
  54. {
  55. AsyncGroup group = new AsyncGroup();
  56. FrameworkSchedulers.RunOnMainThread(() =>
  57. {
  58. var restartAsync = _instance?.Restart();
  59. restartAsync?.Join(group);
  60. group.End();
  61. });
  62. return group;
  63. }
  64. /// <summary>
  65. /// 退出游戏
  66. /// </summary>
  67. static public void Quit()
  68. {
  69. #if UNITY_EDITOR
  70. UnityEditor.EditorApplication.isPlaying = false;
  71. #else
  72. UnityEngine.Application.Quit();
  73. #endif
  74. }
  75. }
  76. }