AppManager.Static.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 string GameLogicTypeName;
  24. internal static bool IsSessionDebug;
  25. internal static void Initialize()
  26. {
  27. Instance.Restart();
  28. }
  29. internal static void Dispose()
  30. {
  31. _instance = null;
  32. }
  33. /// <summary>
  34. /// 启动
  35. /// </summary>
  36. static public void Start()
  37. {
  38. _instance?.Start();
  39. }
  40. /// <summary>
  41. /// 关闭
  42. /// </summary>
  43. /// <returns></returns>
  44. static public IAsync Close()
  45. {
  46. AsyncGroup group = new AsyncGroup();
  47. FrameworkSchedulers.RunOnMainThread(() =>
  48. {
  49. var closeAsync = _instance?.Close();
  50. closeAsync?.Join(group);
  51. group.End();
  52. });
  53. return group;
  54. }
  55. /// <summary>
  56. /// 重启
  57. /// </summary>
  58. /// <returns></returns>
  59. static public IAsync Restart()
  60. {
  61. AsyncGroup group = new AsyncGroup();
  62. FrameworkSchedulers.RunOnMainThread(() =>
  63. {
  64. var restartAsync = _instance?.Restart();
  65. restartAsync?.Join(group);
  66. group.End();
  67. });
  68. return group;
  69. }
  70. /// <summary>
  71. /// 退出游戏
  72. /// </summary>
  73. static public void Quit()
  74. {
  75. #if UNITY_EDITOR
  76. UnityEditor.EditorApplication.isPlaying = false;
  77. #else
  78. UnityEngine.Application.Quit();
  79. #endif
  80. }
  81. }
  82. }