Миксер
Почини интерфейс
This commit is contained in:
1 parent
1aceea1b79
commit
60c28f54f2
74 files changed
+8060
-1059
No files matched your search
@@ -1,8 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using Code.Infrastructure.Configs;
|
||||
using Code.Infrastructure.EventBus;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using PrimeTween;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using UnityEngine.Audio;
|
||||
|
||||
namespace Code.Infrastructure.Audio
|
||||
@@ -12,16 +16,18 @@ namespace Code.Infrastructure.Audio
|
||||
AudioClip GetClip(AudioNames groupName, int id);
|
||||
void PlayTest();
|
||||
|
||||
UniTaskVoid PlayOneShot(AudioNames groupName, int id, Vector3 position = new Vector3(),
|
||||
UniTaskVoid PlayOneShot(AudioNames groupName, int id, AudioGroupType audioGroupType, Vector3 position = new Vector3(),
|
||||
AudioSourceType sourceType = AudioSourceType.NoBlend,
|
||||
AudioSource audioSource = null);
|
||||
|
||||
UniTaskVoid PlayRepeatedly(AudioNames groupName, int id,
|
||||
UniTaskVoid PlayRepeatedly(AudioNames groupName, int id, AudioGroupType audioGroupType,
|
||||
AudioSourceType sourceType = AudioSourceType.NoBlend, AudioSource audioSource = null, CancellationTokenSource cancellationTokenSource = null);
|
||||
|
||||
bool CheckClip(AudioNames groupName, int id);
|
||||
void CancelMusic(AudioNames groupName, int id);
|
||||
void ReleaseCancellationSource(AudioNames groupName, int id);
|
||||
void ApplyParameter(string effectName, float value, float time, Action callback = null);
|
||||
void ChangeGroupVolume(AudioGroupType groupType, float value, float time, Action callback = null);
|
||||
}
|
||||
|
||||
public partial class AudioService : IAudioClipsEventHandle, IAudioService
|
||||
@@ -33,7 +39,7 @@ namespace Code.Infrastructure.Audio
|
||||
private AudioMixer _mixer;
|
||||
|
||||
private Dictionary<AudioGroupType, AudioMixerGroup> _audioMixerGroups = new();
|
||||
public AudioService(EventBus.EventBus eventBus, IAudioFactory audioFactory, IAudioSourcePool pool)
|
||||
public AudioService(EventBus.EventBus eventBus, IAudioFactory audioFactory, IAudioSourcePool pool, IConfigLocator configLocator)
|
||||
{
|
||||
_eventBus = eventBus;
|
||||
_audioFactory = audioFactory;
|
||||
@@ -44,16 +50,27 @@ namespace Code.Infrastructure.Audio
|
||||
for (int i = 0; i < typeof(AudioNames).GetEnumValues().Length; i++)
|
||||
_audioClips.Add((AudioNames)i, new Dictionary<int, AudioClip>());
|
||||
|
||||
//InitializeMixer();
|
||||
LoadMixerAsset(configLocator.TryGetConfigAssetReference<AudioMixerConfigConfig>()).Forget();
|
||||
}
|
||||
|
||||
private partial void InitializeMixer();
|
||||
|
||||
private async UniTaskVoid LoadMixerAsset(AssetReference audioMixerConfig)
|
||||
{
|
||||
var audioMixer = await Addressables.LoadAssetAsync<AudioMixerConfigConfig>(audioMixerConfig);
|
||||
|
||||
_mixer = audioMixer.mixerAsset;
|
||||
|
||||
InitializeMixer();
|
||||
|
||||
_eventBus.RaiseEvent<IAudioServiceHandle>(x=> x.AudioServiceLoaded());
|
||||
}
|
||||
public void PlayTest()
|
||||
{
|
||||
_pool.GetSource(AudioSourceType.NoBlend).PlayOneShot(GetClip(AudioNames.UISounds, 0));
|
||||
}
|
||||
|
||||
public async UniTaskVoid PlayOneShot(AudioNames groupName, int id, Vector3 position = new Vector3(),
|
||||
public async UniTaskVoid PlayOneShot(AudioNames groupName, int id, AudioGroupType audioGroupType, Vector3 position = new Vector3(),
|
||||
AudioSourceType sourceType = AudioSourceType.NoBlend,
|
||||
AudioSource audioSource = null)
|
||||
{
|
||||
@@ -62,6 +79,7 @@ namespace Code.Infrastructure.Audio
|
||||
if (!audioSource) audioSource = _pool.GetSource(sourceType);
|
||||
|
||||
audioSource.transform.position = position;
|
||||
audioSource.outputAudioMixerGroup = _audioMixerGroups[audioGroupType];
|
||||
audioSource.PlayOneShot(GetClip(groupName, id));
|
||||
|
||||
await UniTask.NextFrame();
|
||||
@@ -74,7 +92,7 @@ namespace Code.Infrastructure.Audio
|
||||
|
||||
public void CancelMusic(AudioNames groupName, int id) => _cancellationTokenSources[new KeyValuePair<AudioNames, int>(groupName, id)].Cancel();
|
||||
public void ReleaseCancellationSource(AudioNames groupName, int id) => _cancellationTokenSources[new KeyValuePair<AudioNames, int>(groupName, id)] = null;
|
||||
public async UniTaskVoid PlayRepeatedly(AudioNames groupName, int id,
|
||||
public async UniTaskVoid PlayRepeatedly(AudioNames groupName, int id, AudioGroupType audioGroupType,
|
||||
AudioSourceType sourceType = AudioSourceType.NoBlend, AudioSource audioSource = null, CancellationTokenSource cancellationTokenSource = null)
|
||||
{
|
||||
if (!CheckClip(groupName, id)) return;
|
||||
@@ -87,7 +105,7 @@ namespace Code.Infrastructure.Audio
|
||||
|
||||
if (!audioSource) audioSource = _pool.GetSource(sourceType);
|
||||
if(!audioSource.resource) audioSource.resource = GetClip(groupName, id);
|
||||
|
||||
audioSource.outputAudioMixerGroup = _audioMixerGroups[audioGroupType];
|
||||
audioSource.Play();
|
||||
|
||||
await UniTask.NextFrame();
|
||||
@@ -107,7 +125,7 @@ namespace Code.Infrastructure.Audio
|
||||
return;
|
||||
}
|
||||
|
||||
PlayRepeatedly(groupName, id, sourceType, audioSource, cancellationTokenSource).Forget();
|
||||
PlayRepeatedly(groupName, id, AudioGroupType.Music, sourceType, audioSource, cancellationTokenSource).Forget();
|
||||
}
|
||||
|
||||
public AudioClip GetClip(AudioNames groupName, int id) => _audioClips[groupName][id];
|
||||
@@ -124,10 +142,26 @@ namespace Code.Infrastructure.Audio
|
||||
_audioClips[groupName].Clear();
|
||||
_audioClips[groupName] = null;
|
||||
}
|
||||
|
||||
public void ApplyParameter(string effectName, float value, float time, Action callback = null)
|
||||
{
|
||||
_mixer.GetFloat(effectName, out float startValue);
|
||||
Tween.Custom(startValue, value, time, newValue => {
|
||||
_mixer.SetFloat(effectName, newValue);
|
||||
}).OnComplete(callback);
|
||||
}
|
||||
public void ChangeGroupVolume(AudioGroupType groupType, float value, float time, Action callback = null)
|
||||
{
|
||||
ApplyParameter(_audioMixerGroups[groupType].name, Mathf.Log10(value == 0f ? 0.001f : value) * 20, time, callback);
|
||||
}
|
||||
}
|
||||
|
||||
public interface IReadyToPlayHandle : IGlobalSubscriber
|
||||
{
|
||||
void OnReadyToPlay(AudioNames groupName);
|
||||
}
|
||||
public interface IAudioServiceHandle : IGlobalSubscriber
|
||||
{
|
||||
void AudioServiceLoaded();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user