/* * 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.Collections.Generic; using UnityEngine; namespace CatLib { /// /// 服务提供者引导程序 /// [ExcludeFromCodeCoverage] [Priority(20)] public sealed class BootstrapProviderRegister : IBootstrap { /// /// 服务提供者列表 /// private readonly IServiceProvider[] providers; /// /// Unity根组件 /// private readonly Component component; /// /// 构建一个服务提供者引导程序 /// /// Unity根组件 /// 服务提供者列表 public BootstrapProviderRegister(Component component, IServiceProvider[] serviceProviders = null) { providers = Arr.Merge(Providers.ServiceProviders, serviceProviders); this.component = component; } /// /// 引导程序接口 /// public void Bootstrap() { LoadUnityComponentProvider(); LoadCodeProvider(); } /// /// 加载以代码形式提供的服务提供者 /// private void LoadCodeProvider() { RegisterProviders(providers); } /// /// 加载Unity组件的服务提供者 /// private void LoadUnityComponentProvider() { if (!component) { return; } RegisterProviders(component.GetComponentsInChildren()); } /// /// 注册服务提供者 /// /// 服务提供者 private static void RegisterProviders(IEnumerable providers) { foreach (var provider in providers) { if (provider == null) { continue; } if (!App.IsRegisted(provider)) { App.Register(provider); } } } } }