GameEntry.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using GameFramework;
  8. using System;
  9. using System.Collections.Generic;
  10. using UnityEngine;
  11. using UnityEngine.SceneManagement;
  12. namespace UnityGameFramework.Runtime
  13. {
  14. /// <summary>
  15. /// 游戏入口。
  16. /// </summary>
  17. public static class GameEntry
  18. {
  19. private static readonly GameFrameworkLinkedList<GameFrameworkComponent> s_GameFrameworkComponents = new GameFrameworkLinkedList<GameFrameworkComponent>();
  20. /// <summary>
  21. /// 游戏框架所在的场景编号。
  22. /// </summary>
  23. internal const int GameFrameworkSceneId = 0;
  24. /// <summary>
  25. /// 获取游戏框架组件。
  26. /// </summary>
  27. /// <typeparam name="T">要获取的游戏框架组件类型。</typeparam>
  28. /// <returns>要获取的游戏框架组件。</returns>
  29. public static T GetComponent<T>() where T : GameFrameworkComponent
  30. {
  31. return (T)GetComponent(typeof(T));
  32. }
  33. /// <summary>
  34. /// 获取游戏框架组件。
  35. /// </summary>
  36. /// <param name="type">要获取的游戏框架组件类型。</param>
  37. /// <returns>要获取的游戏框架组件。</returns>
  38. public static GameFrameworkComponent GetComponent(Type type)
  39. {
  40. LinkedListNode<GameFrameworkComponent> current = s_GameFrameworkComponents.First;
  41. while (current != null)
  42. {
  43. if (current.Value.GetType() == type)
  44. {
  45. return current.Value;
  46. }
  47. current = current.Next;
  48. }
  49. return null;
  50. }
  51. /// <summary>
  52. /// 获取游戏框架组件。
  53. /// </summary>
  54. /// <param name="typeName">要获取的游戏框架组件类型名称。</param>
  55. /// <returns>要获取的游戏框架组件。</returns>
  56. public static GameFrameworkComponent GetComponent(string typeName)
  57. {
  58. LinkedListNode<GameFrameworkComponent> current = s_GameFrameworkComponents.First;
  59. while (current != null)
  60. {
  61. Type type = current.Value.GetType();
  62. if (type.FullName == typeName || type.Name == typeName)
  63. {
  64. return current.Value;
  65. }
  66. current = current.Next;
  67. }
  68. return null;
  69. }
  70. /// <summary>
  71. /// 关闭游戏框架。
  72. /// </summary>
  73. /// <param name="shutdownType">关闭游戏框架类型。</param>
  74. public static void Shutdown(ShutdownType shutdownType)
  75. {
  76. Log.Info("Shutdown Game Framework ({0})...", shutdownType.ToString());
  77. BaseComponent baseComponent = GetComponent<BaseComponent>();
  78. if (baseComponent != null)
  79. {
  80. baseComponent.Shutdown();
  81. baseComponent = null;
  82. }
  83. s_GameFrameworkComponents.Clear();
  84. if (shutdownType == ShutdownType.None)
  85. {
  86. return;
  87. }
  88. if (shutdownType == ShutdownType.Restart)
  89. {
  90. SceneManager.LoadScene(GameFrameworkSceneId);
  91. return;
  92. }
  93. if (shutdownType == ShutdownType.Quit)
  94. {
  95. Application.Quit();
  96. #if UNITY_EDITOR
  97. UnityEditor.EditorApplication.isPlaying = false;
  98. #endif
  99. return;
  100. }
  101. }
  102. /// <summary>
  103. /// 注册游戏框架组件。
  104. /// </summary>
  105. /// <param name="gameFrameworkComponent">要注册的游戏框架组件。</param>
  106. internal static void RegisterComponent(GameFrameworkComponent gameFrameworkComponent)
  107. {
  108. if (gameFrameworkComponent == null)
  109. {
  110. Log.Error("Game Framework component is invalid.");
  111. return;
  112. }
  113. Type type = gameFrameworkComponent.GetType();
  114. LinkedListNode<GameFrameworkComponent> current = s_GameFrameworkComponents.First;
  115. while (current != null)
  116. {
  117. if (current.Value.GetType() == type)
  118. {
  119. Log.Error("Game Framework component type '{0}' is already exist.", type.FullName);
  120. return;
  121. }
  122. current = current.Next;
  123. }
  124. s_GameFrameworkComponents.AddLast(gameFrameworkComponent);
  125. }
  126. }
  127. }