Ejemplo n.º 1
0
        /// <summary>
        /// Initializes engine behaviour and services.
        /// Services will be initialized in the order in which they were added to the list.
        /// </summary>
        public static async Task InitializeAsync(EngineConfiguration config, IEngineBehaviour behaviour, List <IEngineService> services)
        {
            if (IsInitialized)
            {
                return;
            }
            if (IsInitializing)
            {
                await initializeTCS.Task; return;
            }

            OnInitializationStarted?.Invoke();

            Engine.config = config;
            initializeTCS = new TaskCompletionSource <object>();

            Behaviour = behaviour;
            Behaviour.OnBehaviourDestroy += Destroy;

            Engine.services = services;
            foreach (var service in services)
            {
                await service.InitializeServiceAsync();
            }

            initializeTCS.TrySetResult(null);
            OnInitializationFinished?.Invoke();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes the engine behaviour and services.
        /// Services will be initialized in the order in which they were added to the list.
        /// </summary>
        /// <param name="configurationProvider">Configuration provider to use when resolving configuration objects.</param>
        /// <param name="behaviour">Unity's <see cref="MonoBehaviour"/> proxy to use.</param>
        /// <param name="services">List of engine services to initialize (order will be preserved).</param>
        public static async UniTask InitializeAsync(IConfigurationProvider configurationProvider, IEngineBehaviour behaviour, IList <IEngineService> services)
        {
            if (Initialized)
            {
                return;
            }
            if (Initializing)
            {
                await initializeTCS.Task;
                return;
            }

            initializeTCS = new UniTaskCompletionSource();
            OnInitializationStarted?.Invoke();

            for (int i = preInitializationTasks.Count - 1; i >= 0; i--)
            {
                await preInitializationTasks[i]();
                if (!Initializing)
                {
                    return;                // In case initialization process was terminated (eg, exited playmode).
                }
            }

            Engine.configurationProvider = configurationProvider;
            Configuration = GetConfiguration <EngineConfiguration>();

            Behaviour = behaviour;
            Behaviour.OnBehaviourDestroy += Destroy;

            objects.Clear();
            Engine.services.Clear();
            Engine.services.AddRange(services);

            for (var i = 0; i < Engine.services.Count; i++)
            {
                OnInitializationProgress?.Invoke(i / (float)Engine.services.Count);
                await Engine.services[i].InitializeServiceAsync();
                if (!Initializing)
                {
                    return;
                }
            }

            for (int i = postInitializationTasks.Count - 1; i >= 0; i--)
            {
                await postInitializationTasks[i]();
                if (!Initializing)
                {
                    return;
                }
            }

            initializeTCS?.TrySetResult();
            OnInitializationFinished?.Invoke();
        }