//------------------------------------------------------------ // Game Framework // Copyright © 2013-2021 loyalsoft. All rights reserved. // Homepage: http://www.game7000.com/ // Feedback: http://www.game7000.com/ //------------------------------------------------------------ using GameFramework; using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; namespace UnityGameFramework.Runtime { /// /// 游戏入口。 /// public static class GameEntry { private static readonly GameFrameworkLinkedList s_GameFrameworkComponents = new GameFrameworkLinkedList(); /// /// 游戏框架所在的场景编号。 /// internal const int GameFrameworkSceneId = 0; /// /// 获取游戏框架组件。 /// /// 要获取的游戏框架组件类型。 /// 要获取的游戏框架组件。 public static T GetComponent() where T : GameFrameworkComponent { return (T)GetComponent(typeof(T)); } /// /// 获取游戏框架组件。 /// /// 要获取的游戏框架组件类型。 /// 要获取的游戏框架组件。 public static GameFrameworkComponent GetComponent(Type type) { LinkedListNode current = s_GameFrameworkComponents.First; while (current != null) { if (current.Value.GetType() == type) { return current.Value; } current = current.Next; } return null; } /// /// 获取游戏框架组件。 /// /// 要获取的游戏框架组件类型名称。 /// 要获取的游戏框架组件。 public static GameFrameworkComponent GetComponent(string typeName) { LinkedListNode current = s_GameFrameworkComponents.First; while (current != null) { Type type = current.Value.GetType(); if (type.FullName == typeName || type.Name == typeName) { return current.Value; } current = current.Next; } return null; } /// /// 关闭游戏框架。 /// /// 关闭游戏框架类型。 public static void Shutdown(ShutdownType shutdownType) { Log.Info("Shutdown Game Framework ({0})...", shutdownType.ToString()); BaseComponent baseComponent = GetComponent(); if (baseComponent != null) { baseComponent.Shutdown(); baseComponent = null; } s_GameFrameworkComponents.Clear(); if (shutdownType == ShutdownType.None) { return; } if (shutdownType == ShutdownType.Restart) { SceneManager.LoadScene(GameFrameworkSceneId); return; } if (shutdownType == ShutdownType.Quit) { Application.Quit(); #if UNITY_EDITOR UnityEditor.EditorApplication.isPlaying = false; #endif return; } } /// /// 注册游戏框架组件。 /// /// 要注册的游戏框架组件。 internal static void RegisterComponent(GameFrameworkComponent gameFrameworkComponent) { if (gameFrameworkComponent == null) { Log.Error("Game Framework component is invalid."); return; } Type type = gameFrameworkComponent.GetType(); LinkedListNode current = s_GameFrameworkComponents.First; while (current != null) { if (current.Value.GetType() == type) { Log.Error("Game Framework component type '{0}' is already exist.", type.FullName); return; } current = current.Next; } s_GameFrameworkComponents.AddLast(gameFrameworkComponent); } } }