Finally, priority VO
This commit is contained in:
83
Assets/Scripts/Sound/AppleAudioSource.cs
Normal file
83
Assets/Scripts/Sound/AppleAudioSource.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using AudioSourceEvents;
|
||||
using System;
|
||||
using System.Diagnostics.Tracing;
|
||||
using UnityEditor.UI;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Audio;
|
||||
|
||||
/// <summary>
|
||||
/// We automatically add the AudioSource component here so we can control it. Do not add it manually!
|
||||
/// </summary>
|
||||
[RequireComponent(typeof(AudioSource))]
|
||||
public class AppleAudioSource : MonoBehaviour
|
||||
{
|
||||
public enum AudioSourceType{CriticalVO,VO,Ambience,SFX,Music}
|
||||
public AudioSourceType audioSourceType;
|
||||
public AudioSource audioSource;
|
||||
private AudioMixer _audioMixer;
|
||||
|
||||
public int priority;
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
{
|
||||
audioSource = GetComponent<AudioSource>();
|
||||
AudioManager.Instance.RegisterNewAudioSource(this);
|
||||
_audioMixer = AudioManager.Instance.audioMixer;
|
||||
InitializeAudioSource();
|
||||
|
||||
}
|
||||
|
||||
public void InitializeAudioSource()
|
||||
{
|
||||
// Route the audio to the correct bus depending on type
|
||||
switch (audioSourceType)
|
||||
{
|
||||
case AppleAudioSource.AudioSourceType.CriticalVO:
|
||||
audioSource.outputAudioMixerGroup = _audioMixer.FindMatchingGroups("Critical VO")[0];
|
||||
break;
|
||||
case AppleAudioSource.AudioSourceType.VO:
|
||||
audioSource.outputAudioMixerGroup = _audioMixer.FindMatchingGroups("VO")[0];
|
||||
break;
|
||||
case AppleAudioSource.AudioSourceType.SFX:
|
||||
audioSource.outputAudioMixerGroup = _audioMixer.FindMatchingGroups("SFX")[0];
|
||||
break;
|
||||
case AppleAudioSource.AudioSourceType.Ambience:
|
||||
audioSource.outputAudioMixerGroup = _audioMixer.FindMatchingGroups("Ambience")[0];
|
||||
break;
|
||||
case AppleAudioSource.AudioSourceType.Music:
|
||||
audioSource.outputAudioMixerGroup = _audioMixer.FindMatchingGroups("Music")[0];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void Play()
|
||||
{
|
||||
if (audioSourceType == AudioSourceType.CriticalVO || audioSourceType == AudioSourceType.VO)
|
||||
{
|
||||
if (AudioManager.Instance.RequestPlayVO(this))
|
||||
{
|
||||
audioSource.Play();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("AppleAudioSource " + name + " was suppressed because something more important is playing");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void InterruptAudio(string nameOfInterruptingAudio)
|
||||
{
|
||||
Debug.Log("AppleAudioSource " + name + " was interrupted by source: " + nameOfInterruptingAudio);
|
||||
Stop();
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
audioSource.Stop();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
2
Assets/Scripts/Sound/AppleAudioSource.cs.meta
Normal file
2
Assets/Scripts/Sound/AppleAudioSource.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 242e6101be071f44fb14c3c12641c833
|
||||
@@ -4,9 +4,11 @@ using Core;
|
||||
using PuzzleS;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Audio;
|
||||
using Bootstrap;
|
||||
using AppleHills.Core;
|
||||
using AppleHills.Core.Interfaces;
|
||||
using System.Collections.Generic;
|
||||
using AudioSourceEvents;
|
||||
using System;
|
||||
|
||||
public class AudioManager : MonoBehaviour, IPausable
|
||||
{
|
||||
@@ -14,17 +16,26 @@ public class AudioManager : MonoBehaviour, IPausable
|
||||
/// Play all audio, just music or no audio at all when the game is paused.
|
||||
/// </summary>
|
||||
public enum PauseBehavior
|
||||
{PlayAllAudio, MusicOnly, NoAudio}
|
||||
{ PlayAllAudio, MusicOnly, NoAudio }
|
||||
|
||||
public PauseBehavior currentPauseBehavior;
|
||||
|
||||
public AudioMixer _audioMixer;
|
||||
public AudioMixer audioMixer;
|
||||
private AudioListener _audioListener;
|
||||
public AppleAudioSource currentlyPlayingVO;
|
||||
|
||||
|
||||
private static AudioManager _instance;
|
||||
private GameObject _player;
|
||||
|
||||
public List<AppleAudioSource> criticalVOSources;
|
||||
public List<AppleAudioSource> VOSources;
|
||||
public List<AppleAudioSource> musicSources;
|
||||
public List<AppleAudioSource> ambienceSources;
|
||||
public List<AppleAudioSource> SFXSources;
|
||||
|
||||
private IAudioEventSource _eventSource;
|
||||
|
||||
/// <summary>
|
||||
/// Singleton instance of the AudioManager.
|
||||
/// </summary>
|
||||
@@ -48,7 +59,11 @@ public class AudioManager : MonoBehaviour, IPausable
|
||||
void Start()
|
||||
{
|
||||
_player = QuickAccess.Instance.PlayerGameObject;
|
||||
_audioListener = QuickAccess.Instance.MainCamera.GetComponent<AudioListener>();
|
||||
_audioListener = QuickAccess.Instance.MainCamera.GetComponent<AudioListener>();
|
||||
foreach (AppleAudioSource _audioSource in criticalVOSources)
|
||||
{
|
||||
Debug.Log("Found source: " + _audioSource.name);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetAudioPauseBehavior(PauseBehavior newPauseBehavior)
|
||||
@@ -56,20 +71,39 @@ public class AudioManager : MonoBehaviour, IPausable
|
||||
switch (newPauseBehavior)
|
||||
{
|
||||
case PauseBehavior.PlayAllAudio:
|
||||
_audioMixer.updateMode = AudioMixerUpdateMode.UnscaledTime;
|
||||
audioMixer.updateMode = AudioMixerUpdateMode.UnscaledTime;
|
||||
AudioListener.pause = false;
|
||||
break;
|
||||
case PauseBehavior.MusicOnly:
|
||||
_audioMixer.updateMode = AudioMixerUpdateMode.UnscaledTime; break;
|
||||
audioMixer.updateMode = AudioMixerUpdateMode.UnscaledTime; break;
|
||||
//TODO: Pause all audio mixers except music mixer
|
||||
case PauseBehavior.NoAudio:
|
||||
_audioMixer.updateMode = AudioMixerUpdateMode.Normal;
|
||||
audioMixer.updateMode = AudioMixerUpdateMode.Normal;
|
||||
AudioListener.pause = true;
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public LevelAudioObject GetCurrentLevelAudioObject()
|
||||
{
|
||||
Debug.Log("Audio objects: " + FindObjectsByType<LevelAudioObject>(FindObjectsInactive.Include, FindObjectsSortMode.None).Length);
|
||||
if (FindObjectsByType<LevelAudioObject>(FindObjectsInactive.Include, FindObjectsSortMode.None).Length > 1)
|
||||
{
|
||||
Debug.LogWarning("Warning! More than one LevelAudioObject in the level! Using the first one found");
|
||||
return FindObjectsByType<LevelAudioObject>(FindObjectsInactive.Include, FindObjectsSortMode.None)[0];
|
||||
}
|
||||
if (FindObjectsByType<LevelAudioObject>(FindObjectsInactive.Include, FindObjectsSortMode.None).Length == 0)
|
||||
{
|
||||
Debug.LogWarning("Error! No LevelAudioObject found, AudioManager might not function properly!");
|
||||
return null;
|
||||
}
|
||||
else
|
||||
return FindFirstObjectByType<LevelAudioObject>();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void Pause()
|
||||
{
|
||||
SetAudioPauseBehavior(PauseBehavior.NoAudio);
|
||||
@@ -79,4 +113,94 @@ public class AudioManager : MonoBehaviour, IPausable
|
||||
{
|
||||
SetAudioPauseBehavior(PauseBehavior.PlayAllAudio);
|
||||
}
|
||||
|
||||
public void RegisterNewAudioSource(AppleAudioSource newAudioSource)
|
||||
{
|
||||
switch (newAudioSource.audioSourceType)
|
||||
{
|
||||
case AppleAudioSource.AudioSourceType.CriticalVO:
|
||||
criticalVOSources.Add(newAudioSource);
|
||||
break;
|
||||
case AppleAudioSource.AudioSourceType.VO:
|
||||
VOSources.Add(newAudioSource);
|
||||
break;
|
||||
case AppleAudioSource.AudioSourceType.SFX:
|
||||
SFXSources.Add(newAudioSource);
|
||||
break;
|
||||
case AppleAudioSource.AudioSourceType.Ambience:
|
||||
ambienceSources.Add(newAudioSource);
|
||||
break;
|
||||
case AppleAudioSource.AudioSourceType.Music:
|
||||
musicSources.Add(newAudioSource);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Request playing a VO line. Returns true if whatever is playing is not critical, or weight of requested VO line is lower.
|
||||
/// </summary>
|
||||
public bool RequestPlayVO(AppleAudioSource requestedAudioSource)
|
||||
{
|
||||
|
||||
if (currentlyPlayingVO == null)
|
||||
{
|
||||
currentlyPlayingVO = requestedAudioSource;
|
||||
Debug.Log($"CurrentVO prio: {currentlyPlayingVO.priority} requested VO prio: {requestedAudioSource.priority}");
|
||||
RegisterStartStopEvents(requestedAudioSource.audioSource);
|
||||
return true;
|
||||
}
|
||||
if(currentlyPlayingVO.audioSourceType != AppleAudioSource.AudioSourceType.CriticalVO)
|
||||
{
|
||||
currentlyPlayingVO.InterruptAudio(requestedAudioSource.name);
|
||||
currentlyPlayingVO = requestedAudioSource;
|
||||
Debug.Log($"CurrentVO prio: {currentlyPlayingVO.priority} requested VO prio: {requestedAudioSource.priority}");
|
||||
RegisterStartStopEvents(requestedAudioSource.audioSource);
|
||||
return true;
|
||||
}
|
||||
if (currentlyPlayingVO.audioSourceType == AppleAudioSource.AudioSourceType.CriticalVO && currentlyPlayingVO.priority > requestedAudioSource.priority)
|
||||
{
|
||||
currentlyPlayingVO.InterruptAudio(requestedAudioSource.name);
|
||||
Debug.Log($"CurrentVO prio: {currentlyPlayingVO.priority} requested VO prio: {requestedAudioSource.priority}");
|
||||
currentlyPlayingVO = requestedAudioSource;
|
||||
RegisterStartStopEvents(requestedAudioSource.audioSource);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log($"CurrentVO prio: {currentlyPlayingVO.priority} requested VO prio: {requestedAudioSource.priority}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnApplicationQuit()
|
||||
{
|
||||
// TODO: Release the handles safely ReleaseAllHandles();
|
||||
}
|
||||
|
||||
private void RegisterStartStopEvents(AudioSource audioSource)
|
||||
{
|
||||
if (audioSource.resource == null)
|
||||
{
|
||||
Debug.Log($"AppleAudioSource {audioSource.name} could not register Start and Stop events.");
|
||||
}
|
||||
else
|
||||
{
|
||||
_eventSource = audioSource.RequestEventHandlers();
|
||||
_eventSource.AudioStopped += OnAudioStopped;
|
||||
_eventSource.AudioStarted += OnAudioStarted;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnAudioStopped(object sender, EventArgs e)
|
||||
{
|
||||
currentlyPlayingVO = null;
|
||||
_eventSource.AudioStopped -= OnAudioStopped;
|
||||
_eventSource.AudioStarted -= OnAudioStarted;
|
||||
|
||||
}
|
||||
|
||||
private void OnAudioStarted(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,10 +41,11 @@ public class BushAudioController : MonoBehaviour
|
||||
|
||||
private void PlayBirdCounter(object sender, EventArgs e)
|
||||
{
|
||||
_eventSource.AudioStopped -= PlayBirdCounter;
|
||||
VOPlayer.resource = birdCounterClip[birdGameStats.birdsFoundInLevel];
|
||||
VOPlayer.Play();
|
||||
birdGameStats.BirdFound();
|
||||
_eventSource.AudioStopped -= PlayBirdCounter;
|
||||
|
||||
}
|
||||
|
||||
public void OnDisable()
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Audio;
|
||||
using System;
|
||||
using UnityEngine.Events;
|
||||
|
||||
public class NarratorVO : MonoBehaviour
|
||||
public class LevelAudioObject : MonoBehaviour
|
||||
{
|
||||
public AudioSource narratorAudioSource;
|
||||
public AppleAudioSource narratorAudioSource;
|
||||
public AudioResource firstNarration;
|
||||
public UnityEvent narrationFinished;
|
||||
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
@@ -18,14 +15,8 @@ public class NarratorVO : MonoBehaviour
|
||||
|
||||
void PlayNarrationAudio()
|
||||
{
|
||||
|
||||
narratorAudioSource.resource = firstNarration;
|
||||
narratorAudioSource.audioSource.resource = firstNarration;
|
||||
narratorAudioSource.Play();
|
||||
}
|
||||
|
||||
private void NarrationFinished(object sender, EventArgs e)
|
||||
{
|
||||
narrationFinished.Invoke();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user