Reworked the dialogue system

Now the dialogue can use tags to add special effects
Reorganized project structure, updating may require adding some using statements
This commit is contained in:
Gerard Gascón 2022-09-15 17:27:28 +02:00 committed by GitHub
parent 95bad523b9
commit 0e8b8b1835
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 1912 additions and 1266 deletions

View file

@ -2,261 +2,270 @@
using UnityEngine;
using System;
public class AudioManager : MonoBehaviour{
namespace SimpleTools.AudioManager {
public class AudioManager : MonoBehaviour {
public static AudioManager instance;
public static AudioManager instance;
[SerializeField] Sounds soundList = default;
[SerializeField] Sounds soundList = default;
void Awake(){
if(instance == null){
instance = this;
}else{
Destroy(gameObject);
return;
}
DontDestroyOnLoad(gameObject);
void Awake() {
if (instance == null) {
instance = this;
} else {
Destroy(gameObject);
return;
}
DontDestroyOnLoad(gameObject);
foreach(Sounds.List s in soundList.sounds){
if(string.IsNullOrEmpty(s.name) || string.IsNullOrWhiteSpace(s.name)){
Debug.LogWarning("The name one sound is empty");
continue;
}
foreach (Sounds.List s in soundList.sounds) {
if (string.IsNullOrEmpty(s.name) || string.IsNullOrWhiteSpace(s.name)) {
Debug.LogWarning("The name one sound is empty");
continue;
}
GameObject sound = new GameObject(s.name);
sound.transform.parent = transform;
s.source = sound.AddComponent<AudioSource>();
GameObject sound = new GameObject(s.name);
sound.transform.parent = transform;
s.source = sound.AddComponent<AudioSource>();
if(soundList.mainMixer && soundList.sfxMixer){
if (s.type == Sounds.List.Type.Music)
s.source.outputAudioMixerGroup = soundList.mainMixer;
else
s.source.outputAudioMixerGroup = soundList.sfxMixer;
}
if (soundList.mainMixer && soundList.sfxMixer) {
if (s.type == Sounds.List.Type.Music)
s.source.outputAudioMixerGroup = soundList.mainMixer;
else
s.source.outputAudioMixerGroup = soundList.sfxMixer;
}
s.source.clip = s.clip;
s.source.clip = s.clip;
s.source.volume = s.volume;
s.source.pitch = s.pitch;
s.source.loop = s.loop;
}
}
s.source.volume = s.volume;
s.source.pitch = s.pitch;
s.source.loop = s.loop;
}
}
#region Play
/// <summary>Use this to play a sound with a specific name
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void Play(string name){
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if(s == null){
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.pitch = s.RandomPitch;
s.source.volume = s.RandomVolume;
s.source.Play();
}
/// <summary>Use this to play a sound with a specific name and with a certain delay
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void Play(string name, float delay){
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null){
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.pitch = s.RandomPitch;
s.source.volume = s.RandomVolume;
s.source.PlayDelayed(delay);
}
/// <summary>Use this to play one shot of a sound with a specific name
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void PlayOneShot(string name){
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null){
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.pitch = s.RandomPitch;
s.source.volume = s.RandomVolume;
s.source.PlayOneShot(s.clip);
}
/// <summary>Use this to play an intro song and then start playing the song loop
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void PlayWithIntro(string intro, string song){
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == intro);
if (s == null){
Debug.LogWarning("Sound: " + intro + " not found!");
return;
}
s.source.pitch = s.RandomPitch;
s.source.volume = s.RandomVolume;
s.source.Play();
#region Play
/// <summary>Use this to play a sound with a specific name
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void Play(string name) {
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null) {
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.pitch = s.RandomPitch;
s.source.volume = s.RandomVolume;
s.source.Play();
}
/// <summary>Use this to play a sound with a specific name and with a certain delay
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void Play(string name, float delay) {
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null) {
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.pitch = s.RandomPitch;
s.source.volume = s.RandomVolume;
s.source.PlayDelayed(delay);
}
/// <summary>Use this to play one shot of a sound with a specific name
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void PlayOneShot(string name) {
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null) {
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.pitch = s.RandomPitch;
s.source.volume = s.RandomVolume;
s.source.PlayOneShot(s.clip);
}
/// <summary>Use this to play an intro song and then start playing the song loop
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void PlayWithIntro(string intro, string song) {
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == intro);
if (s == null) {
Debug.LogWarning("Sound: " + intro + " not found!");
return;
}
s.source.pitch = s.RandomPitch;
s.source.volume = s.RandomVolume;
s.source.Play();
float introDuration = s.clip.length;
Play(song, introDuration);
}
#endregion
#region Pause
/// <summary>Use this to pause a sound with a specific name
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void Pause(string name){
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null){
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.pitch = s.RandomPitch;
s.source.volume = s.RandomVolume;
s.source.Pause();
}
/// <summary>Use this to unpause a sound with a specific name
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void UnPause(string name){
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null){
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.pitch = s.RandomPitch;
s.source.volume = s.RandomVolume;
s.source.UnPause();
}
#endregion
#region Stop
/// <summary>Use this to stop a sound with a specific name
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void Stop(string name){
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null){
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.pitch = s.RandomPitch;
s.source.volume = s.RandomVolume;
s.source.Stop();
}
/// <summary>Use this to stop all the sounds
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void StopAll(){
foreach (Sounds.List s in soundList.sounds){
if (s.source){
s.source.Stop();
}
}
}
#endregion
/// <summary>This function returns the AudioSource that contains a specific sound
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public AudioSource GetSource(string name){
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null){
Debug.LogWarning("Sound: " + name + " not found!");
return null;
}
return s.source;
}
#region Fades
/// <summary>Use this to start playing a sound with a fade in
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void FadeIn(string name, float duration){
StartCoroutine(FadeInCoroutine(name, duration));
}
IEnumerator FadeInCoroutine(string name, float fadeTime){
AudioSource audioSource = GetSource(name);
if (audioSource != null && !audioSource.isPlaying){
float volume = audioSource.volume;
audioSource.volume = 0;
audioSource.Play();
while (audioSource.volume < volume){
audioSource.volume += Time.deltaTime / fadeTime;
yield return null;
}
float introDuration = s.clip.length;
Play(song, introDuration);
}
/// <summary>Use this to play one shot of a random sound within a list
/// <para>They have to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void PlayRandomSound(params string[] names) {
int random = UnityEngine.Random.Range(0, names.Length);
PlayOneShot(names[random]);
}
#endregion
#region Pause
/// <summary>Use this to pause a sound with a specific name
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void Pause(string name) {
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null) {
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.pitch = s.RandomPitch;
s.source.volume = s.RandomVolume;
s.source.Pause();
}
/// <summary>Use this to unpause a sound with a specific name
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void UnPause(string name) {
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null) {
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.pitch = s.RandomPitch;
s.source.volume = s.RandomVolume;
s.source.UnPause();
}
#endregion
#region Stop
/// <summary>Use this to stop a sound with a specific name
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void Stop(string name) {
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null) {
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.pitch = s.RandomPitch;
s.source.volume = s.RandomVolume;
s.source.Stop();
}
/// <summary>Use this to stop all the sounds
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void StopAll() {
foreach (Sounds.List s in soundList.sounds) {
if (s.source) {
s.source.Stop();
}
}
}
#endregion
/// <summary>This function returns the AudioSource that contains a specific sound
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public AudioSource GetSource(string name) {
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null) {
Debug.LogWarning("Sound: " + name + " not found!");
return null;
}
return s.source;
}
#region Fades
/// <summary>Use this to start playing a sound with a fade in
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void FadeIn(string name, float duration) {
StartCoroutine(FadeInCoroutine(name, duration));
}
IEnumerator FadeInCoroutine(string name, float fadeTime) {
AudioSource audioSource = GetSource(name);
if (audioSource != null && !audioSource.isPlaying) {
float volume = audioSource.volume;
audioSource.volume = 0;
audioSource.Play();
while (audioSource.volume < volume) {
audioSource.volume += Time.deltaTime / fadeTime;
yield return null;
}
audioSource.volume = volume;
}
}
/// <summary>Use this to stop playing a sound with a fade out
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void FadeOut(string name, float duration){
StartCoroutine(FadeOutCoroutine(name, duration));
}
IEnumerator FadeOutCoroutine(string name, float fadeTime){
AudioSource audioSource = GetSource(name);
audioSource.volume = volume;
}
}
/// <summary>Use this to stop playing a sound with a fade out
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void FadeOut(string name, float duration) {
StartCoroutine(FadeOutCoroutine(name, duration));
}
IEnumerator FadeOutCoroutine(string name, float fadeTime) {
AudioSource audioSource = GetSource(name);
if (audioSource && audioSource.isPlaying){
float startVolume = audioSource.volume;
if (audioSource && audioSource.isPlaying) {
float startVolume = audioSource.volume;
while (audioSource.volume > 0){
audioSource.volume -= startVolume * Time.deltaTime / fadeTime;
yield return null;
}
while (audioSource.volume > 0) {
audioSource.volume -= startVolume * Time.deltaTime / fadeTime;
yield return null;
}
audioSource.Stop();
audioSource.volume = startVolume;
}
}
audioSource.Stop();
audioSource.volume = startVolume;
}
}
/// <summary>Use this to start playing a sound muted
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void PlayMuted(string name){
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null){
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.pitch = s.RandomPitch;
s.source.volume = 0f;
s.source.Play();
}
/// <summary>Use this to fade in a sound that is currently muted
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// <para>WARNING: If the PlayMuted hasn't been called before, this function won't work</para>
/// </summary>
public void FadeMutedIn(string name, float duration){
StartCoroutine(FadeMutedInCoroutine(name, duration));
}
IEnumerator FadeMutedInCoroutine(string name, float fadeTime){
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null){
Debug.LogWarning("Sound: " + name + " not found!");
yield break;
}
/// <summary>Use this to start playing a sound muted
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void PlayMuted(string name) {
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null) {
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.pitch = s.RandomPitch;
s.source.volume = 0f;
s.source.Play();
}
/// <summary>Use this to fade in a sound that is currently muted
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// <para>WARNING: If the PlayMuted hasn't been called before, this function won't work</para>
/// </summary>
public void FadeMutedIn(string name, float duration) {
StartCoroutine(FadeMutedInCoroutine(name, duration));
}
IEnumerator FadeMutedInCoroutine(string name, float fadeTime) {
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null) {
Debug.LogWarning("Sound: " + name + " not found!");
yield break;
}
while (s.source.volume < s.volume){
s.source.volume += Time.deltaTime / fadeTime;
yield return null;
}
s.source.volume = s.volume;
}
/// <summary>Use this to fade out a sound and keep playing that muted
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void FadeMutedOut(string name, float duration){
StartCoroutine(FadeMutedOutCoroutine(name, duration));
}
IEnumerator FadeMutedOutCoroutine(string name, float fadeTime){
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null){
Debug.LogWarning("Sound: " + name + " not found!");
yield break;
}
while (s.source.volume < s.volume) {
s.source.volume += Time.deltaTime / fadeTime;
yield return null;
}
s.source.volume = s.volume;
}
/// <summary>Use this to fade out a sound and keep playing that muted
/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
/// </summary>
public void FadeMutedOut(string name, float duration) {
StartCoroutine(FadeMutedOutCoroutine(name, duration));
}
IEnumerator FadeMutedOutCoroutine(string name, float fadeTime) {
Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
if (s == null) {
Debug.LogWarning("Sound: " + name + " not found!");
yield break;
}
while (s.source.volume > 0){
s.source.volume -= Time.deltaTime / fadeTime;
yield return null;
}
s.source.volume = 0;
}
#endregion
}
while (s.source.volume > 0) {
s.source.volume -= Time.deltaTime / fadeTime;
yield return null;
}
s.source.volume = 0;
}
#endregion
}
}

View file

@ -1,50 +1,53 @@
using UnityEngine;
using UnityEngine.Audio;
[CreateAssetMenu(fileName = "Sounds", menuName = "Simple Tools/Sounds", order = 11)]
public class Sounds : ScriptableObject{
namespace SimpleTools.AudioManager {
[CreateAssetMenu(fileName = "Sounds", menuName = "Simple Tools/Sounds", order = 11)]
public class Sounds : ScriptableObject {
[Tooltip("The music mixer.")]
public AudioMixerGroup mainMixer = default;
[Tooltip("The SFX mixer.")]
public AudioMixerGroup sfxMixer = default;
[Tooltip("The music mixer.")]
public AudioMixerGroup mainMixer = default;
[Tooltip("The SFX mixer.")]
public AudioMixerGroup sfxMixer = default;
public List[] sounds;
public List[] sounds;
[System.Serializable] public class List{
[Tooltip("Name of the sound. Each name has to be different between each other.")]
public string name;
[System.Serializable]
public class List {
[Tooltip("Name of the sound. Each name has to be different between each other.")]
public string name;
public AudioClip clip;
public AudioClip clip;
[System.Serializable] public enum Type { Music, SFX }
[Space]
[Tooltip("Is it part of the music or the SFX?")] public Type type;
[System.Serializable] public enum Type { Music, SFX }
[Space]
[Tooltip("Is it part of the music or the SFX?")] public Type type;
[Space]
[Tooltip("Default volume of the sound."), Range(0f, 1f)] public float volume;
[Tooltip("Variance percentage of the volume"), Range(0f, 1f)] public float volumeVariance;
[Tooltip("Default pitch of the sound."), Range(.1f, 3f)] public float pitch;
[Tooltip("Variance percentage of the pitch"), Range(0f, 1f)] public float pitchVariance;
[Space]
[Tooltip("Default volume of the sound."), Range(0f, 1f)] public float volume;
[Tooltip("Variance percentage of the volume"), Range(0f, 1f)] public float volumeVariance;
[Tooltip("Default pitch of the sound."), Range(.1f, 3f)] public float pitch;
[Tooltip("Variance percentage of the pitch"), Range(0f, 1f)] public float pitchVariance;
public bool loop;
public bool loop;
[HideInInspector] public AudioSource source;
[HideInInspector] public AudioSource source;
float randomVolume;
public float RandomVolume{
get{
randomVolume = volume * (1f + Random.Range(-volumeVariance / 2f, volumeVariance / 2f));
return randomVolume;
}
}
float randomVolume;
public float RandomVolume {
get {
randomVolume = volume * (1f + Random.Range(-volumeVariance / 2f, volumeVariance / 2f));
return randomVolume;
}
}
float randomPitch;
public float RandomPitch{
get{
randomPitch = pitch * (1f + Random.Range(-pitchVariance / 2f, pitchVariance / 2f));
return randomPitch;
}
}
}
}
float randomPitch;
public float RandomPitch {
get {
randomPitch = pitch * (1f + Random.Range(-pitchVariance / 2f, pitchVariance / 2f));
return randomPitch;
}
}
}
}
}