using System.Collections.Generic; using System.Threading.Tasks; using UnityEngine; using VContainer; using VContainer.Unity; namespace Common { public class GameLauncher : IStartable { [Inject] private LifetimeScope _globalScope; protected LifetimeScope lifetimeScope; protected List> ResourceTasks = new(); protected bool IsResourcesLoaded; public async void Start() { CreateLifetimeScope(); CreateResourcesList(); await LoadResources(); Launch(); } private void CreateLifetimeScope() { var lifetimeScopeName = ResolveLifetimeScopePrefab(); var g = Resources.Load($"Scopes/{lifetimeScopeName}"); lifetimeScope = _globalScope.CreateChildFromPrefab(g); } protected virtual string ResolveLifetimeScopePrefab() { return "GameScope"; } protected virtual void CreateResourcesList() { } protected virtual void Launch() { } private async Task LoadResources() { await Task.WhenAll(ResourceTasks); IsResourcesLoaded = true; foreach (var task in ResourceTasks) { if (!task.Result) { IsResourcesLoaded = false; break; } } if (!IsResourcesLoaded) { return false; } return true; } } }