Second commit
This commit is contained in:
commit
36840f3ad4
999 files changed
+61364
No files matched your search
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Code.Infrastructure.Configs;
|
||||
using Code.Infrastructure.Utils;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using UnityEngine.ResourceManagement.AsyncOperations;
|
||||
|
||||
namespace Code.Infrastructure.Audio
|
||||
{
|
||||
public interface IAudioFactory
|
||||
{
|
||||
void UnloadAudioClips(AudioPartition groupPartition);
|
||||
UniTask LoadAudioClips(AudioPartition groupPartition);
|
||||
public Dictionary<AudioPartition, Dictionary<int, AudioReference>> ReferenceDictionary { get; }
|
||||
}
|
||||
|
||||
public interface IAudioFactoryInitializer
|
||||
{
|
||||
UniTask LoadConfigs();
|
||||
}
|
||||
|
||||
public class AudioFactory : IAudioFactory, IDisposable, IAudioFactoryInitializer
|
||||
{
|
||||
private string _label;
|
||||
private AsyncOperationHandle<IList<AudioInfoConfig>> _configsHandle;
|
||||
|
||||
public Dictionary<AudioPartition, Dictionary<int, AudioReference>> ReferenceDictionary { get; } = new();
|
||||
|
||||
public AudioFactory(IConfigLocator configLocator)
|
||||
{
|
||||
_label = configLocator.TryGetManyConfigAssetReferences<AudioInfoConfig>();
|
||||
|
||||
}
|
||||
|
||||
public async UniTask LoadConfigs()
|
||||
{
|
||||
if(!await AddressablesUtilities.CheckLabelExists(_label)) return;
|
||||
|
||||
_configsHandle = Addressables.LoadAssetsAsync<AudioInfoConfig>(_label);
|
||||
|
||||
await _configsHandle.ToUniTask();
|
||||
|
||||
foreach (AudioInfoConfig config in _configsHandle.Result)
|
||||
{
|
||||
if(!ReferenceDictionary.ContainsKey(config.GroupPartition))
|
||||
ReferenceDictionary.Add(config.GroupPartition, new());
|
||||
ReferenceDictionary[config.GroupPartition].Add(config.id, config.Reference);
|
||||
}
|
||||
}
|
||||
|
||||
public async UniTask LoadAudioClips(AudioPartition groupPartition)
|
||||
{
|
||||
foreach (var reference in ReferenceDictionary[groupPartition])
|
||||
{
|
||||
var load = reference.Value.Load();
|
||||
if (load.Status == UniTaskStatus.Pending)
|
||||
await load;
|
||||
}
|
||||
}
|
||||
|
||||
public void UnloadAudioClips(AudioPartition groupPartition)
|
||||
{
|
||||
foreach (var reference in ReferenceDictionary[groupPartition])
|
||||
{
|
||||
reference.Value.Unload();
|
||||
}
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (var partition in ReferenceDictionary)
|
||||
{
|
||||
foreach (var reference in partition.Value)
|
||||
{
|
||||
reference.Value.Unload();
|
||||
}
|
||||
}
|
||||
|
||||
if(_configsHandle.IsValid()) _configsHandle.Release();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user