cultivation/Assets/Scripts/Common/GameLauncher.cs

73 lines
1.7 KiB
C#

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<Task<bool>> ResourceTasks = new();
protected bool IsResourcesLoaded;
public async void Start()
{
CreateLifetimeScope();
CreateResourcesList();
await LoadResources();
Launch();
}
private void CreateLifetimeScope()
{
var lifetimeScopeName = ResolveLifetimeScopePrefab();
var g = Resources.Load<LifetimeScope>($"Scopes/{lifetimeScopeName}");
lifetimeScope = _globalScope.CreateChildFromPrefab(g);
}
protected virtual string ResolveLifetimeScopePrefab()
{
return "GameScope";
}
protected virtual void CreateResourcesList()
{
}
protected virtual void Launch()
{
}
private async Task<bool> LoadResources()
{
await Task.WhenAll(ResourceTasks);
IsResourcesLoaded = true;
foreach (var task in ResourceTasks)
{
if (!task.Result)
{
IsResourcesLoaded = false;
break;
}
}
if (!IsResourcesLoaded)
{
return false;
}
return true;
}
}
}