Пытаюсь добавить AudioMixer

This commit is contained in:
ThomasCharester committed 2026-01-28 18:07:47 +03:00
1 parent 1f8e14d078
commit ec60c47652
20 files changed
+197 -80

No files matched your search

@@ -0,0 +1,11 @@
using Code.Infrastructure.Generators;
namespace Code.Infrastructure.Audio
{
[AudioMixerEnum]
public enum AudioGroupType
{
Master,
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 754a72a6b2c140a49a23bf6d39654c94
timeCreated: 1769609499
@@ -11,9 +11,10 @@ namespace Code.Infrastructure.Audio
[ToConfig] public int id;
[ToConfig] public AssetReference fileReference;
}
[Config("Configs/Audio/AudioSource")]
public class AudioSourcePrefab
[Config("Configs/Audio/AudioServiceSettings")]
public class AudioServiceSettings
{
[ToConfig] public AssetReference prefabReference;
[ToConfig] public AssetReference sourcePrefab;
[ToConfig] public AssetReference mixerPrefab;
}
}
@@ -3,6 +3,7 @@ using System.Threading;
using Code.Infrastructure.EventBus;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.Audio;
namespace Code.Infrastructure.Audio
{
@@ -19,13 +20,15 @@ namespace Code.Infrastructure.Audio
CancellationTokenSource cancellationTokenSource, AudioSourceType sourceType = AudioSourceType.NoBlend, AudioSource audioSource = null);
}
public class AudioService : IAudioClipsEventHandle, IAudioService
public partial class AudioService : IAudioClipsEventHandle, IAudioService
{
private EventBus.EventBus _eventBus;
private IAudioFactory _audioFactory;
private IAudioSourcePool _pool;
private Dictionary<AudioNames, Dictionary<int, AudioClip>> _audioClips = new();
private AudioMixer _mixer;
private Dictionary<AudioGroupType, AudioMixerGroup> _audioMixerGroups = new();
public AudioService(EventBus.EventBus eventBus, IAudioFactory audioFactory, IAudioSourcePool pool)
{
_eventBus = eventBus;
@@ -36,8 +39,11 @@ namespace Code.Infrastructure.Audio
for (int i = 0; i < typeof(AudioNames).GetEnumValues().Length; i++)
_audioClips.Add((AudioNames)i, new Dictionary<int, AudioClip>());
InitializeMixer();
}
private partial void InitializeMixer();
public void PlayTest()
{
_pool.GetSource(AudioSourceType.NoBlend).PlayOneShot(GetClip(AudioNames.UISounds, 0));
@@ -33,7 +33,7 @@ namespace Code.Infrastructure.Audio
private string _configsLabel;
private AsyncOperationHandle<IList<AudioSourceSettingsConfig>> _handles;
private AssetReference _sourcePrefabConfigReference;
private AsyncOperationHandle<AudioSourcePrefabConfig> _prefabConfigHandle;
private AsyncOperationHandle<AudioServiceSettingsConfig> _prefabConfigHandle;
private AsyncOperationHandle<GameObject> _prefabHandle;
private EventBus.EventBus _eventBus;
@@ -42,21 +42,21 @@ namespace Code.Infrastructure.Audio
_eventBus = eventBus;
_configsLabel = configLocator.TryGetManyConfigAssetReferences<AudioSourceSettingsConfig>();
_sourcePrefabConfigReference = configLocator.TryGetConfigAssetReference<AudioSourcePrefabConfig>();
_sourcePrefabConfigReference = configLocator.TryGetConfigAssetReference<AudioServiceSettingsConfig>();
LoadConfigs().Forget();
}
private async UniTaskVoid LoadConfigs()
{
_prefabConfigHandle = Addressables.LoadAssetAsync<AudioSourcePrefabConfig>(_sourcePrefabConfigReference);
_prefabConfigHandle = Addressables.LoadAssetAsync<AudioServiceSettingsConfig>(_sourcePrefabConfigReference);
_handles = Addressables.LoadAssetsAsync<AudioSourceSettingsConfig>(_configsLabel);
await UniTask.WhenAll(
_prefabConfigHandle.ToUniTask(),
_handles.ToUniTask());
_prefabHandle = Addressables.LoadAssetAsync<GameObject>(_prefabConfigHandle.Result.prefabReference);
_prefabHandle = Addressables.LoadAssetAsync<GameObject>(_prefabConfigHandle.Result.sourcePrefab);
_settings = _handles.Result.ToDictionary(x => x.sourceType);
@@ -9,5 +9,6 @@ namespace Code.Infrastructure.Audio
[ToConfig] public float spatialBlend;
[ToConfig] public AudioSourceType sourceType;
}
}
@@ -46,4 +46,7 @@ namespace Code.Infrastructure.Generators
public class SceneIdEnum : Attribute
{
}
public class AudioMixerEnum : Attribute
{
}
}
@@ -0,0 +1,73 @@
#if UNITY_EDITOR
using UnityCodeGen;
using UnityEditor;
using UnityEngine;
namespace Code.Infrastructure.Generators
{
[Generator]
public class AudioMixerGenerator : ICodeGenerator
{
public void Execute(GeneratorContext context)
{
var Enum = TypeCache.GetTypesWithAttribute<AudioMixerEnum>();
var initializedCode = "";
var serviceCode = "";
var interfacesUsingsCode = $@"
// using Code.Infrastructure.Audio;
";
foreach (var en in Enum)
{
if (!en.IsEnum) return;
var members = en.GetFields();
foreach (var member in members)
{
if (member.FieldType.FullName != en.FullName) continue;
initializedCode += $@"
_audioMixerGroups.Add({en.FullName}.{member.Name}, _mixer.FindMatchingGroups(""{member.Name}"")[0]);
";
// dictionaryCode += $@"
//
// _scenesActions.Add(SceneId.{member.Name},() => _eventBus.RaiseEvent<IOn{member.Name}SceneLoadedHandler>(h=> h.On{member.Name}SceneLoaded()));
// ";
}
serviceCode += $@"
#pragma warning disable CS0105
{interfacesUsingsCode}
#pragma warning restore CS0105
namespace Code.Infrastructure.Audio{{
public partial class AudioService
{{
private partial void InitializeMixer(){{
{initializedCode}
}}
}}
}}";
}
//
// loaderCode = $@"
// #pragma warning disable CS0105
// {interfacesUsingsCode}
// #pragma warning restore CS0105
//
// namespace Code.Infrastructure.SceneLoader
// {{
// public partial class SceneLoader{{
// partial void SetupScenesInfo(){{
// {dictionaryCode}
// }}
// }}
// {initializedCode}
// }}
// ";
context.AddCode($"AudioMixer.Generated.cs", serviceCode);
}
}
}
#endif
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c0e1349496af435c8bad4f3a409d8d58
timeCreated: 1769608573