/* * This file is part of the CatLib package. * * (c) CatLib * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. * * Document: http://catlib.io/ */ using System; using System.Reflection; using UnityEditor; namespace CatLib.Editor { /// /// CatLib For Unity Editor /// [InitializeOnLoad] public class EditorFramework { /// /// 编辑器层的框架实例 /// private static Application editorApplication; /// /// 创建一个新的CatLib For Unity Editor实例 /// static EditorFramework() { if (EditorApplication.isPlayingOrWillChangePlaymode) { UnityEngine.Application.quitting += Quitted; return; } ApplyEditorApplication(); } /// /// 程序需要退出 /// private static void Quitted() { ApplyEditorApplication(); } /// /// 获取编辑器框架 /// /// 编辑器框架 private static EditorFramework GetEditorFramework() { var target = typeof(EditorFramework); var assembiles = Arr.Filter(AppDomain.CurrentDomain.GetAssemblies(), TestCheckInAssembiles); foreach (var assembly in assembiles) { foreach (var type in assembly.GetTypes()) { if (type != target && target.IsAssignableFrom(type) && type.IsClass && !type.IsAbstract) { return (EditorFramework)Activator.CreateInstance(type); } } } return new EditorFramework(); } /// /// 需要检查的程序集 /// private static readonly string[] checkInAssembiles = new string[] { "*-Editor", "*.Editor", }; /// /// 测试是否处于需要检查的程序集列表 /// /// 测试程序集 /// 是否处于需要检查的程序集列表 private static bool TestCheckInAssembiles(Assembly assembly) { foreach (var pattern in checkInAssembiles) { if (Str.Is(pattern, assembly.GetName().Name)) { return true; } } return false; } /// /// 应用编辑器应用程序 /// private static void ApplyEditorApplication() { var editorFramework = GetEditorFramework(); editorApplication = editorFramework.CreateApplication(); editorFramework.BeforeBootstrap(editorApplication); editorApplication.Bootstrap(editorFramework.GetBootstraps()); editorApplication.Init(); } /// /// CatLib Unity Framework /// public IApplication Application { get { return editorApplication; } } /// /// 在引导开始之前 /// /// 应用程序 protected virtual void BeforeBootstrap(IApplication application) { application.On(ApplicationEvents.OnStartCompleted, OnStartCompleted); } /// /// 在启动完成之后 /// protected virtual void OnStartCompleted() { } /// /// 创建框架实例 /// /// 创建框架实例 protected virtual Application CreateApplication() { return new UnityApplication(null); } /// /// 获取引导程序 /// /// 引导脚本 protected virtual IBootstrap[] GetBootstraps() { return Bootstraps.GetBoostraps(null); } } }