2026-06-11 사운드시스템 (BaseScene참고)

This commit is contained in:
skrwns304@gmail.com
2026-06-11 15:32:56 +09:00
parent 6a20f521ce
commit 9f173a4ebc
23 changed files with 640 additions and 6 deletions

View File

@@ -1,9 +1,16 @@
using System;
using UnityEngine; using UnityEngine;
public class GameManager : MonoBehaviour,ISceneInitializable public class GameManager : MonoBehaviour,ISceneInitializable
{ {
public static GameManager Instance; public static GameManager Instance;
//현재 플레이어가 위치한 구역 (구역 상태의 단일 진실 출처)
public Zone CurrentZone { get; private set; } = Zone.None;
//구역이 바뀌면 발생. 사운드 등 여러 시스템이 구독해서 반응한다.
public event Action<Zone> OnZoneChanged;
private void Awake() private void Awake()
{ {
if (Instance == null) if (Instance == null)
@@ -17,8 +24,16 @@ private void Awake()
} }
} }
//구역 변경 요청 (ZoneTrigger 등에서 호출)
public void SetZone(Zone zone)
{
if (zone == CurrentZone) return; //같은 구역이면 무시
CurrentZone = zone;
OnZoneChanged?.Invoke(zone); //구독자(SoundManager 등)에게 통지
}
public void OnSceneLoaded() public void OnSceneLoaded()
{ {
} }
} }

View File

@@ -0,0 +1,234 @@
using System;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using UnityEngine.Audio;
//인스펙터에서 구역별 BGM을 매핑하기 위한 구조체
[Serializable]
public struct ZoneBgm
{
public Zone Zone;
public AudioClip Clip;
}
public class SoundManager : MonoBehaviour
{
public static SoundManager Instance;
[Header("Mixer")]
[SerializeField] private AudioMixer _mainMixer; //메인 믹서 하나
[SerializeField] private AudioMixerGroup _bgmGroup;
[SerializeField] private AudioMixerGroup _sfxGroup;
[Header("BGM")]
[SerializeField] private AudioSource _bgmSource; //BGM 전용 단일 오디오 소스
[SerializeField, Range(0f, 1f)] private float _bgmVolume = 1f;
[SerializeField] private float _bgmFadeDuration = 1.5f;
[SerializeField] private List<ZoneBgm> _zoneBgmList = new();
[Header("SFX Pool")]
[SerializeField] private int _sfxPoolSize = 10; //시작 시 미리 생성할 SFX 소스 개수
//구역 → BGM 빠른 조회용 (런타임 변환)
private readonly Dictionary<Zone, AudioClip> _zoneBgmMap = new();
//SFX 풀 - 대기중인 소스
private readonly Queue<AudioSource> _sfxPool = new();
//BGM 전환(페이드)을 취소하기 위한 토큰. 구역을 빠르게 오갈 때 이전 전환을 취소함
private CancellationTokenSource _bgmCts;
private void Awake()
{
if (Instance == null)
{
Instance = this; //만들어진 자신을 인스턴스로 설정
DontDestroyOnLoad(gameObject);
Initialize();
}
else
{
Destroy(gameObject); //이미 인스턴스가 있으면 자신을 파괴
}
}
private void Initialize()
{
//구역별 BGM 매핑을 딕셔너리로 변환
foreach (var entry in _zoneBgmList)
{
_zoneBgmMap[entry.Zone] = entry.Clip;
}
//BGM 소스 기본 설정
if (_bgmSource != null)
{
_bgmSource.outputAudioMixerGroup = _bgmGroup;
_bgmSource.playOnAwake = false;
_bgmSource.loop = true;
}
//SFX 풀 미리 생성
for (int i = 0; i < _sfxPoolSize; i++)
{
_sfxPool.Enqueue(CreateSfxSource());
}
}
//모든 매니저의 Awake가 끝난 뒤 이벤트를 구독한다
private void Start()
{
if (Instance != this) return; //중복 인스턴스로 파괴 예정이면 구독하지 않음
if (GameManager.Instance != null)
{
GameManager.Instance.OnZoneChanged += HandleZoneChanged;
}
}
private void OnDestroy()
{
if (GameManager.Instance != null)
{
GameManager.Instance.OnZoneChanged -= HandleZoneChanged;
}
}
//=========================== BGM ===========================
//구역이 바뀌면 호출됨 (GameManager.OnZoneChanged 구독)
private void HandleZoneChanged(Zone zone)
{
AudioClip clip = _zoneBgmMap.GetValueOrDefault(zone); //없으면 null → 무음 처리
//이전 전환이 진행중이면 취소
_bgmCts?.Cancel();
_bgmCts?.Dispose();
_bgmCts = CancellationTokenSource.CreateLinkedTokenSource(this.destroyCancellationToken);
_ = ChangeBGM(clip, _bgmCts.Token);
}
//페이드아웃 → 클립 교체 → 페이드인 (단일 소스라 순차 진행)
private async Awaitable ChangeBGM(AudioClip clip, CancellationToken token)
{
try
{
//현재 재생중이면 먼저 페이드아웃
if (_bgmSource.isPlaying)
{
await BGMFade(_bgmSource.volume, 0f, token);
_bgmSource.Stop();
}
if (clip == null) return; //해당 구역에 BGM이 없으면 무음 유지
//새 BGM 페이드인
_bgmSource.clip = clip;
_bgmSource.volume = 0f;
_bgmSource.Play();
await BGMFade(0f, _bgmVolume, token);
}
catch (OperationCanceledException)
{
//구역이 다시 바뀌어 전환이 취소됨 - 정상 흐름
}
}
//BGM 소스 볼륨을 from→to로 부드럽게 이동
private async Awaitable BGMFade(float from, float to, CancellationToken token)
{
float t = 0f;
while (t < _bgmFadeDuration)
{
t += Time.deltaTime;
_bgmSource.volume = Mathf.Lerp(from, to, t / _bgmFadeDuration);
await Awaitable.NextFrameAsync(token);
}
_bgmSource.volume = to;
}
//=========================== SFX ===========================
//2D SFX 재생 (UI 사운드 등 위치 무관)
public void PlaySFX(AudioClip clip, float volume = 1f)
{
if (clip == null) return;
AudioSource source = GetSfxSource();
source.transform.localPosition = Vector3.zero;
source.spatialBlend = 0f; //2D
Play(source, clip, volume);
_ = ReturnAfterPlay(source, clip.length); //재생이 끝나면 풀에 반납
}
//3D SFX 재생 (VR 공간음향 - 특정 위치에서 들림)
public void PlaySFXAt(AudioClip clip, Vector3 position, float volume = 1f)
{
if (clip == null) return;
AudioSource source = GetSfxSource();
source.transform.position = position;
source.spatialBlend = 1f; //3D
Play(source, clip, volume);
_ = ReturnAfterPlay(source, clip.length); //재생이 끝나면 풀에 반납
}
private void Play(AudioSource source, AudioClip clip, float volume)
{
source.clip = clip;
source.volume = volume;
source.Play();
}
//풀에서 사용 가능한 SFX 소스를 가져옴 (없으면 새로 생성 - 자동 증설)
private AudioSource GetSfxSource()
{
AudioSource source = _sfxPool.Count > 0 ? _sfxPool.Dequeue() : CreateSfxSource();
source.gameObject.SetActive(true);
return source;
}
//재생 길이만큼 대기 후 소스를 풀로 반납
private async Awaitable ReturnAfterPlay(AudioSource source, float duration)
{
try
{
await Awaitable.WaitForSecondsAsync(duration, this.destroyCancellationToken);
}
catch (OperationCanceledException)
{
return; //매니저 파괴 시 종료
}
source.Stop();
source.clip = null;
source.gameObject.SetActive(false);
_sfxPool.Enqueue(source);
}
//SFX용 AudioSource를 자식 오브젝트로 생성
private AudioSource CreateSfxSource()
{
GameObject go = new("SFX_Source");
go.transform.SetParent(transform);
go.SetActive(false);
AudioSource source = go.AddComponent<AudioSource>();
source.outputAudioMixerGroup = _sfxGroup;
source.playOnAwake = false;
return source;
}
//=========================== Mixer ===========================
//믹서 노출 파라미터로 볼륨 조절 (0~1 → dB 변환)
public void SetVolume(string exposedParam, float normalized)
{
//0에 가까우면 -80dB(무음), 그 외에는 로그 스케일로 변환
float db = normalized <= 0.0001f ? -80f : Mathf.Log10(normalized) * 20f;
_mainMixer.SetFloat(exposedParam, db);
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 93d1361875b43e443adfb756664f5bd5

View File

@@ -1,6 +1,7 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 76da097b7938bf642b435bf7f19fb38f guid: 08bd0d3e4d808b04ea8e3acea901beb4
TextScriptImporter: folderAsset: yes
DefaultImporter:
externalObjects: {} externalObjects: {}
userData: userData:
assetBundleName: assetBundleName:

View File

@@ -0,0 +1,38 @@
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.Splines;
//스플라인 선을 따라 깔리는 환경음(파도 등)
//소스 하나가 플레이어와 "스플라인 위에서 가장 가까운 지점"을 추적해
//어떤 모양의 해안선이든 방향성 + 거리감쇠를 처리한다. (닫힌 섬 형태 포함)
[RequireComponent(typeof(AudioSource))]
public class LineAmbienceAudio : MonoBehaviour
{
[Header("스플라인")]
[SerializeField] private SplineContainer _spline; //환경음이 깔릴 선 (해안선 등)
[Header("추적 대상")]
[SerializeField] private Transform _listener; //플레이어 (보통 메인 카메라). 비우면 자동으로 Camera.main 사용
private void LateUpdate()
{
Transform listener = _listener != null ? _listener : (Camera.main != null ? Camera.main.transform : null);
if (listener == null || _spline == null) return;
//리스너 위치를 스플라인 로컬 공간으로 변환 (컨테이너의 Transform 반영)
float3 localListener = _spline.transform.InverseTransformPoint(listener.position);
//스플라인 곡선 위에서 가장 가까운 지점을 구함 (Knot 사이 곡선까지 정확)
SplineUtility.GetNearestPoint(_spline.Spline, localListener, out float3 localNearest, out _);
//다시 월드 공간으로 변환해서 소스를 그 지점으로 이동
transform.position = _spline.transform.TransformPoint(localNearest);
}
//에디터에서 현재 소스가 추적중인 지점을 표시 (스플라인 자체는 SplineContainer가 그려줌)
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.yellow;
Gizmos.DrawSphere(transform.position, 0.5f);
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 9039653488e18f14496af8f3e47e084a

View File

@@ -0,0 +1,9 @@
//게임 내 공간 구역(Zone) 정의
//BGM 전환, 환경음, 게임플레이 등 여러 시스템에서 공통으로 사용하는 공용 타입.
public enum Zone
{
None,
Ocean,
Island,
Seaside
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: d83d93eb32f8f6344b80bcaf21cc62dc

View File

@@ -0,0 +1,44 @@
using UnityEngine;
//구역마다 트리거 콜라이더를 하나씩 두고 어떤 구역인지 지정해서 사용.
[RequireComponent(typeof(Collider))]
public class ZoneTrigger : MonoBehaviour
{
[SerializeField] private Zone _zone; //이 영역이 나타내는 구역
[SerializeField] private string _playerTag = "Player"; //플레이어로 인식할 태그
private void Reset()
{
//이 컴포넌트를 붙이면 콜라이더를 자동으로 트리거로 설정
GetComponent<Collider>().isTrigger = true;
}
private void OnTriggerEnter(Collider other)
{
if (!other.CompareTag(_playerTag)) return;
if (GameManager.Instance == null) return;
//구역 상태만 바꾸면, BGM 등은 OnZoneChanged 구독자가 알아서 반응한다
GameManager.Instance.SetZone(_zone);
}
//에디터에서 구역 범위를 시각화
private void OnDrawGizmosSelected()
{
Collider col = GetComponent<Collider>();
if (col == null) return;
Gizmos.color = new Color(0f, 1f, 1f, 0.15f);
Gizmos.matrix = transform.localToWorldMatrix;
//박스/구 콜라이더만 간단히 표시
if (col is BoxCollider box)
{
Gizmos.DrawCube(box.center, box.size);
}
else if (col is SphereCollider sphere)
{
Gizmos.DrawSphere(sphere.center, sphere.radius);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 3b4941db6b378ff41a5ba55f2112b16c

View File

@@ -0,0 +1,179 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!244 &-1673388970353958467
AudioMixerEffectController:
m_ObjectHideFlags: 3
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name:
m_EffectID: bd8ba8d9deb646c41a48253b4a1d27e2
m_EffectName: Attenuation
m_MixLevel: 71df4c32703aa08479adad14693a46dd
m_Parameters: []
m_SendTarget: {fileID: 0}
m_EnableWetMix: 0
m_Bypass: 0
--- !u!241 &24100000
AudioMixerController:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: MainMixer
m_OutputGroup: {fileID: 0}
m_MasterGroup: {fileID: 24300002}
m_Snapshots:
- {fileID: 24500006}
m_StartSnapshot: {fileID: 24500006}
m_SuspendThreshold: -80
m_EnableSuspend: 1
m_UpdateMode: 0
m_ExposedParameters:
- guid: 9f0e4a07104ffe649a45656e080cb519
name: AmbienceVolume
- guid: 398925895b9378749bbb47de5020fbca
name: BGMVolume
- guid: 11cb72cc53f92134abd037128f0f06a4
name: SFXVolume
m_AudioMixerGroupViews:
- guids:
- 28fbd7fac61fe144bae7d8f65706e521
- 2155007a53a8fb444a7db4f2e4890f9a
- 31cbdc5ffd8a0694eaf4fd134e4c02d6
- d3363b36cc1520c42a243c5b2e20cf8c
name: View
m_CurrentViewIndex: 0
m_TargetSnapshot: {fileID: 24500006}
--- !u!243 &24300002
AudioMixerGroupController:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Master
m_AudioMixer: {fileID: 24100000}
m_GroupID: 28fbd7fac61fe144bae7d8f65706e521
m_Children:
- {fileID: 2390854483045795540}
- {fileID: 9170324399740677654}
- {fileID: 1773006847107679546}
m_Volume: 7f92ac78cd51bee41b359cc1cf10ef22
m_Pitch: 934a9094b1f5c3a49b0427be0bc8d89f
m_Send: 00000000000000000000000000000000
m_Effects:
- {fileID: 24400004}
m_UserColorIndex: 0
m_Mute: 0
m_Solo: 0
m_BypassEffects: 0
--- !u!244 &24400004
AudioMixerEffectController:
m_ObjectHideFlags: 3
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name:
m_EffectID: 12545a3b326e7f94b84334b31c3c81ea
m_EffectName: Attenuation
m_MixLevel: c09d80eb66b30eb43b044e387b13ebdc
m_Parameters: []
m_SendTarget: {fileID: 0}
m_EnableWetMix: 0
m_Bypass: 0
--- !u!245 &24500006
AudioMixerSnapshotController:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Snapshot
m_AudioMixer: {fileID: 24100000}
m_SnapshotID: f0413c567f9d2a646a463d5d8abf141e
m_FloatValues: {}
m_TransitionOverrides: {}
--- !u!243 &1773006847107679546
AudioMixerGroupController:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Ambience
m_AudioMixer: {fileID: 24100000}
m_GroupID: d3363b36cc1520c42a243c5b2e20cf8c
m_Children: []
m_Volume: 9f0e4a07104ffe649a45656e080cb519
m_Pitch: 183e61b7487e8f44f91f3fa19e3a07bf
m_Send: 00000000000000000000000000000000
m_Effects:
- {fileID: 3632671860622926541}
m_UserColorIndex: 0
m_Mute: 0
m_Solo: 0
m_BypassEffects: 0
--- !u!243 &2390854483045795540
AudioMixerGroupController:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: BGM
m_AudioMixer: {fileID: 24100000}
m_GroupID: 2155007a53a8fb444a7db4f2e4890f9a
m_Children: []
m_Volume: 398925895b9378749bbb47de5020fbca
m_Pitch: e709cef564d2cc542801fedb1cc82692
m_Send: 00000000000000000000000000000000
m_Effects:
- {fileID: -1673388970353958467}
m_UserColorIndex: 0
m_Mute: 0
m_Solo: 0
m_BypassEffects: 0
--- !u!244 &3632671860622926541
AudioMixerEffectController:
m_ObjectHideFlags: 3
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name:
m_EffectID: 581be5ac5061ed642ab1aefd15c669fd
m_EffectName: Attenuation
m_MixLevel: d2e980b4e22c70245b14a7fd42e9f12a
m_Parameters: []
m_SendTarget: {fileID: 0}
m_EnableWetMix: 0
m_Bypass: 0
--- !u!244 &8058370271489578540
AudioMixerEffectController:
m_ObjectHideFlags: 3
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name:
m_EffectID: eee986f4dc560644793ff5093a2cc23e
m_EffectName: Attenuation
m_MixLevel: 0e2942af24172e047a4a42d86649973c
m_Parameters: []
m_SendTarget: {fileID: 0}
m_EnableWetMix: 0
m_Bypass: 0
--- !u!243 &9170324399740677654
AudioMixerGroupController:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: SFX
m_AudioMixer: {fileID: 24100000}
m_GroupID: 31cbdc5ffd8a0694eaf4fd134e4c02d6
m_Children: []
m_Volume: 11cb72cc53f92134abd037128f0f06a4
m_Pitch: 3f83f2f2a838d0c42952d5717a23cbbd
m_Send: 00000000000000000000000000000000
m_Effects:
- {fileID: 8058370271489578540}
m_UserColorIndex: 0
m_Mute: 0
m_Solo: 0
m_BypassEffects: 0

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 6fa1b3d602efa32458b98d95a591b844
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 24100000
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 066d121f8d540524da5ad956ceb0185e
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: ed22ef34eacee71409838f7f61050a02
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@@ -0,0 +1,23 @@
fileFormatVersion: 2
guid: 6b0a18eaf025027449f8393f0018779b
AudioImporter:
externalObjects: {}
serializedVersion: 8
defaultSettings:
serializedVersion: 2
loadType: 0
sampleRateSetting: 0
sampleRateOverride: 44100
compressionFormat: 1
quality: 1
conversionMode: 0
preloadAudioData: 0
platformSettingOverrides: {}
forceToMono: 0
normalize: 1
loadInBackground: 0
ambisonic: 0
3D: 1
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 3b0a2a2661b81fd4ba58fb04c7409d2a
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@@ -0,0 +1,23 @@
fileFormatVersion: 2
guid: 25be830f2f0243b4f956b068867f6550
AudioImporter:
externalObjects: {}
serializedVersion: 8
defaultSettings:
serializedVersion: 2
loadType: 0
sampleRateSetting: 0
sampleRateOverride: 44100
compressionFormat: 1
quality: 1
conversionMode: 0
preloadAudioData: 0
platformSettingOverrides: {}
forceToMono: 0
normalize: 1
loadInBackground: 0
ambisonic: 0
3D: 1
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1 +0,0 @@
지울것

View File

@@ -1,5 +1,6 @@
{ {
"dependencies": { "dependencies": {
"com.unity.cinemachine": "3.1.7",
"com.unity.feature.development": "1.0.2", "com.unity.feature.development": "1.0.2",
"com.unity.graphtoolkit": "0.4.0-exp.2", "com.unity.graphtoolkit": "0.4.0-exp.2",
"com.unity.ide.rider": "3.0.39", "com.unity.ide.rider": "3.0.39",
@@ -7,6 +8,7 @@
"com.unity.learn.iet-framework": "5.0.3", "com.unity.learn.iet-framework": "5.0.3",
"com.unity.multiplayer.center": "1.0.1", "com.unity.multiplayer.center": "1.0.1",
"com.unity.render-pipelines.universal": "17.3.0", "com.unity.render-pipelines.universal": "17.3.0",
"com.unity.splines": "2.6.1",
"com.unity.timeline": "1.8.12", "com.unity.timeline": "1.8.12",
"com.unity.xr.androidxr-openxr": "1.2.0", "com.unity.xr.androidxr-openxr": "1.2.0",
"com.unity.xr.arfoundation": "6.4.1", "com.unity.xr.arfoundation": "6.4.1",

View File

@@ -10,6 +10,16 @@
}, },
"url": "https://packages.unity.com" "url": "https://packages.unity.com"
}, },
"com.unity.cinemachine": {
"version": "3.1.7",
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.splines": "2.0.0",
"com.unity.modules.imgui": "1.0.0"
},
"url": "https://packages.unity.com"
},
"com.unity.collections": { "com.unity.collections": {
"version": "2.6.2", "version": "2.6.2",
"depth": 2, "depth": 2,
@@ -179,6 +189,17 @@
"com.unity.searcher": "4.9.3" "com.unity.searcher": "4.9.3"
} }
}, },
"com.unity.splines": {
"version": "2.8.2",
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.mathematics": "1.2.1",
"com.unity.modules.imgui": "1.0.0",
"com.unity.settings-manager": "1.0.3"
},
"url": "https://packages.unity.com"
},
"com.unity.test-framework": { "com.unity.test-framework": {
"version": "1.6.0", "version": "1.6.0",
"depth": 1, "depth": 1,