Skip to content

Commit

Permalink
feat(background-music): add emmitter and config to play (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
Long18 authored Jan 23, 2024
1 parent 809cb22 commit cd49315
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Assets/AddressableAssetsData/AddressableAssetSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ MonoBehaviour:
m_DefaultGroup: a6a10e199b443454ba3e0754129e4913
m_currentHash:
serializedVersion: 2
Hash: 383e5346901155784c0510435203ab9f
Hash: 8ab13f72977a6ff07d748a771a2bf865
m_OptimizeCatalogSize: 0
m_BuildRemoteCatalog: 0
m_BundleLocalCatalog: 0
Expand All @@ -38,7 +38,7 @@ MonoBehaviour:
m_Id:
m_RemoteCatalogLoadPath:
m_Id:
m_ContentStateBuildPathProfileVariableName:
m_ContentStateBuildPathProfileVariableName: <default settings path>
m_CustomContentStateBuildPath:
m_ContentStateBuildPath:
m_BuildAddressablesWithPlayerBuild: 0
Expand Down
15 changes: 15 additions & 0 deletions Assets/Scenes/WIP/AudioTestScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ PrefabInstance:
propertyPath: m_Name
value: AudioManager
objectReference: {fileID: 0}
- target: {fileID: 8029590146148956303, guid: 367a3aea81f87844c8a9c1454fce8d1f, type: 3}
propertyPath: _emitter
value:
objectReference: {fileID: 6679447067400465031}
- target: {fileID: 8683984623389327866, guid: 367a3aea81f87844c8a9c1454fce8d1f, type: 3}
propertyPath: m_LocalPosition.x
value: 0
Expand Down Expand Up @@ -329,6 +333,17 @@ PrefabInstance:
m_AddedGameObjects: []
m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: cfe0c4e0865e4374b93e8f32ef301852, type: 3}
--- !u!114 &6679447067400465031 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 4367108581900793607, guid: cfe0c4e0865e4374b93e8f32ef301852, type: 3}
m_PrefabInstance: {fileID: 6679447067400465030}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 76e0b6e43a6b4db79a466d5fc32e0f61, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1001 &7240087349313893617
PrefabInstance:
m_ObjectHideFlags: 0
Expand Down
24 changes: 14 additions & 10 deletions Assets/Scripts/System/Audio/AudioManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ public class AudioManager : MonoBehaviour

{
[SerializeField] private AudioCueEventChannelSO _musicEventChannel;
[SerializeField] private AudioEmitter _emitter;

private AudioEmitter _playingMusicAudioEmitter;
private AudioEmitter _musicEmitter;
private AudioCueSO _currentBgmCue;

private void OnEnable()
Expand Down Expand Up @@ -38,40 +39,43 @@ private void PlayMusic(AudioCueSO audioToPlay, bool requestPlay)

private void HandleMusicToPlay(AudioCueSO audioToPlay)
{
float startTime = 0f;

if (_currentBgmCue != null)
{
_currentBgmCue.GetPlayableAsset().ReleaseAsset();
}

AudioHelper.TryToLoadData(audioToPlay, currentClip =>
void OnAudioClipLoaded(AudioClip currentClip)
{
if (IsAudioPlaying())
{
AudioClip musicToPlay = currentClip;
if (_playingMusicAudioEmitter.GetClip() == musicToPlay) return;
if (_musicEmitter.GetClip() == musicToPlay) return;
startTime = _musicEmitter.FadeMusicOut();
}

if (_playingMusicAudioEmitter == null)
{
// TODO: Get new audio
}
if (!_musicEmitter) _musicEmitter = _emitter;
_musicEmitter.FadeMusicIn(currentClip, startTime);

_currentBgmCue = audioToPlay;

Debug.Log($"[AudioManager::HandleMusicToPlay] Playing background music: {audioToPlay.name}");
});
}

AudioHelper.TryToLoadData(audioToPlay, OnAudioClipLoaded);
}


private void HandleMusicToStop()
{
if (!IsAudioPlaying()) return;

_playingMusicAudioEmitter.Stop();
_musicEmitter.Stop();
Debug.Log($"[AudioManager] Stopped playing background music");
}


private bool IsAudioPlaying() => _playingMusicAudioEmitter != null && _playingMusicAudioEmitter.IsPlaying();
private bool IsAudioPlaying() => _musicEmitter != null && _musicEmitter.IsPlaying();
}
}
76 changes: 76 additions & 0 deletions Assets/Scripts/System/Audio/Emitters/AudioEmitter.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,92 @@
using System.Collections;
using UnityEngine;
using UnityEngine.Events;

namespace Long18.System.Audio.Emitters
{
[RequireComponent((typeof(AudioSource)))]
public class AudioEmitter : MonoBehaviour
{
public event UnityAction<AudioEmitterValue> OnFinishedPlaying;
private const float DEFAULT_VOLUME = 3f;
private const float FADE_VOLUME_DURATION = 2f;

[SerializeField] private AudioSource _audioSource;

private AudioEmitterValue _emitterValue;

public void PlayAudioClip(AudioClip clip, bool hasLoop)
{
_emitterValue = new AudioEmitterValue(this);

_audioSource.clip = clip;
// TODO: Create volume config
_audioSource.volume = DEFAULT_VOLUME;
_audioSource.loop = hasLoop;
_audioSource.time = 0f;
_audioSource.Play();

if (hasLoop) return;
Invoke(nameof(OnFinishedPlay), clip.length);
}

public void FadeMusicIn(AudioClip clip, float startTime = 0f)
{
PlayAudioClip(clip, true);
StartCoroutine(FadeIn(0, 0.5f));

if (startTime <= _audioSource.clip.length)
{
_audioSource.time = startTime;
}

// TODO: Create volume config
StartCoroutine(FadeIn(DEFAULT_VOLUME, FADE_VOLUME_DURATION));
}

public float FadeMusicOut()
{
StartCoroutine(FadeOut(FADE_VOLUME_DURATION));

return _audioSource.time;
}

private IEnumerator FadeIn(float targetVolume, float duration)
{
float currentTime = 0;
float startVolume = _audioSource.volume;

while (currentTime < duration)
{
currentTime += Time.deltaTime;
_audioSource.volume = Mathf.Lerp(startVolume, targetVolume, currentTime / duration);
yield return null;
}

_audioSource.volume = targetVolume;
}

private IEnumerator FadeOut(float duration)
{
float currentTime = 0;
float startVolume = _audioSource.volume;

while (currentTime < duration)
{
currentTime += Time.deltaTime;
_audioSource.volume = Mathf.Lerp(startVolume, 0f, currentTime / duration);
yield return null;
}

_audioSource.volume = 0f;
OnFinishedPlay();
}

public void Resume() => _audioSource.Play();
public void Pause() => _audioSource.Pause();
public void Stop() => _audioSource.Stop();
public AudioClip GetClip() => _audioSource.clip;
public bool IsPlaying() => _audioSource.isPlaying;
private void OnFinishedPlay() => OnFinishedPlaying?.Invoke(_emitterValue);
}
}
22 changes: 22 additions & 0 deletions Assets/Scripts/System/Audio/Emitters/AudioEmitterValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using UnityEngine.Events;

namespace Long18.System.Audio.Emitters
{
[Serializable]
public struct AudioEmitterValue
{
private AudioEmitter _audioEmitter;
public AudioEmitterValue(AudioEmitter audioEmitter) => _audioEmitter = audioEmitter;

public void UnregisterAudioFinishedPlayingEvent(UnityAction<AudioEmitterValue> audioFinishedPlaying)
{
if (_audioEmitter) _audioEmitter.OnFinishedPlaying -= audioFinishedPlaying;
}

public void StopAudio()
{
if (_audioEmitter) _audioEmitter.Stop();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cd49315

Please sign in to comment.