59 lines
1.8 KiB
C#
59 lines
1.8 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Code.Infrastructure.EventBus;
|
|
using Code.Infrastructure.Generators;
|
|
using Cysharp.Threading.Tasks;
|
|
using Reflex.Attributes;
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
|
|
namespace Code.Infrastructure.Configs
|
|
{
|
|
[Config("Configs/LayerMasksConfig", true)]
|
|
public struct LayerMasksConfig
|
|
{
|
|
[ToConfig] public LayerMask mask;
|
|
[ToConfig] public LayerMaskType type;
|
|
}
|
|
|
|
public class LayerMasks
|
|
{
|
|
public static Dictionary<LayerMaskType, LayerMask> Masks { get; } = new();
|
|
|
|
private static void SetMasks(IList<LayerMasksConfigConfig> configs)
|
|
{
|
|
foreach (var config in configs.Where(config => !Masks.ContainsKey(config.type)))
|
|
Masks.Add(config.type, config.mask);
|
|
}
|
|
|
|
public LayerMasks(IConfigLocator configLocator, EventBus.EventBus eventBus)
|
|
{
|
|
SetupMasks(configLocator, eventBus).Forget();
|
|
}
|
|
private async UniTaskVoid SetupMasks(IConfigLocator configLocator, EventBus.EventBus eventBus)
|
|
{
|
|
var label = configLocator.TryGetManyConfigAssetReferences<LayerMasksConfigConfig>();
|
|
|
|
if ((await Addressables.LoadResourceLocationsAsync(label).Task).Count != 0)
|
|
{
|
|
|
|
var handle =
|
|
Addressables.LoadAssetsAsync<LayerMasksConfigConfig>(
|
|
label, null, Addressables.MergeMode.Union);
|
|
|
|
await handle.ToUniTask();
|
|
|
|
SetMasks(handle.Result);
|
|
|
|
Addressables.Release(handle);
|
|
}
|
|
|
|
eventBus.RaiseEvent<ILayerMasksInitializedHandle>(x => x.LayerMasksInitialized());
|
|
}
|
|
}
|
|
|
|
public interface ILayerMasksInitializedHandle : IGlobalSubscriber
|
|
{
|
|
void LayerMasksInitialized();
|
|
}
|
|
} |