diff --git a/Assets/Fungus/Narrative/Resources/SayDialog.prefab b/Assets/Fungus/Narrative/Resources/SayDialog.prefab index ebf6a503..a9115820 100644 --- a/Assets/Fungus/Narrative/Resources/SayDialog.prefab +++ b/Assets/Fungus/Narrative/Resources/SayDialog.prefab @@ -121,6 +121,7 @@ GameObject: - 114: {fileID: 11488906} - 225: {fileID: 22588896} - 114: {fileID: 11488894} + - 82: {fileID: 8294266} m_Layer: 5 m_Name: SayDialog m_TagString: Untagged @@ -128,6 +129,75 @@ GameObject: m_NavMeshLayer: 0 m_StaticEditorFlags: 0 m_IsActive: 1 +--- !u!82 &8294266 +AudioSource: + m_ObjectHideFlags: 1 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 100100000} + m_GameObject: {fileID: 188902} + m_Enabled: 1 + serializedVersion: 4 + OutputAudioMixerGroup: {fileID: 0} + m_audioClip: {fileID: 0} + m_PlayOnAwake: 0 + m_Volume: 1 + m_Pitch: 1 + Loop: 0 + Mute: 0 + Priority: 128 + DopplerLevel: 1 + MinDistance: 1 + MaxDistance: 500 + Pan2D: 0 + rolloffMode: 0 + BypassEffects: 0 + BypassListenerEffects: 0 + BypassReverbZones: 0 + rolloffCustomCurve: + serializedVersion: 2 + m_Curve: + - time: 0 + value: 1 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + - time: 1 + value: 0 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + m_PreInfinity: 2 + m_PostInfinity: 2 + panLevelCustomCurve: + serializedVersion: 2 + m_Curve: + - time: 0 + value: 0 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + m_PreInfinity: 2 + m_PostInfinity: 2 + spreadCustomCurve: + serializedVersion: 2 + m_Curve: + - time: 0 + value: 0 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + m_PreInfinity: 2 + m_PostInfinity: 2 + reverbZoneMixCustomCurve: + serializedVersion: 2 + m_Curve: + - time: 0 + value: 1 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + m_PreInfinity: 2 + m_PostInfinity: 2 --- !u!95 &9588894 Animator: serializedVersion: 3 @@ -255,6 +325,7 @@ MonoBehaviour: m_EditorClassIdentifier: writingSpeed: 30 writingSound: {fileID: 8300000, guid: f637c5ce9d10e45c7855ed89bfc6b97e, type: 3} + writingVolume: 1 loopWritingSound: 1 beepPerCharacter: 0 slowBeepsAt: 10 diff --git a/Assets/Fungus/Narrative/Scripts/Dialog.cs b/Assets/Fungus/Narrative/Scripts/Dialog.cs index 6afc69f7..535df611 100644 --- a/Assets/Fungus/Narrative/Scripts/Dialog.cs +++ b/Assets/Fungus/Narrative/Scripts/Dialog.cs @@ -15,6 +15,8 @@ namespace Fungus public float writingSpeed = 60; public AudioClip writingSound; + [Range(0,1)] + public float writingVolume = 1f; public bool loopWritingSound = true; public bool beepPerCharacter = false; public float slowBeepsAt = 10f; @@ -50,6 +52,9 @@ namespace Fungus protected AudioSource voiceOverAudio; + // The current target writing volume to move towards + protected float targetWritingVolume; + protected virtual void LateUpdate() { wasPointerClicked = false; @@ -67,6 +72,30 @@ namespace Fungus wasPointerClicked = true; clickCooldownTimer = 0.2f; } + + // Fade writing sound + AudioSource audio = GetComponent(); + if (audio != null) + { + if (audio.volume == 0f && + targetWritingVolume > 0) + { + audio.Play(); + } + + if (targetWritingVolume > audio.volume) + { + audio.volume = targetWritingVolume; + } + else + { + audio.volume = Mathf.MoveTowards(audio.volume, targetWritingVolume, Time.deltaTime * 8f); + if (audio.volume == 0f) + { + audio.Stop(); + } + } + } } public virtual void ShowDialog(bool visible) @@ -89,7 +118,19 @@ namespace Fungus clickCooldownTimer = 0.2f; } } - + + public virtual void SetTypingSoundVolume(bool audible) + { + if (audible) + { + targetWritingVolume = writingVolume; + } + else + { + targetWritingVolume = 0f; + } + } + public virtual void FadeInDialog() { LeanTween.cancel(dialogCanvas.gameObject); @@ -271,32 +312,32 @@ namespace Fungus } DialogText dialogText = new DialogText(); + dialogText.parentDialog = this; dialogText.writingSpeed = writingSpeed; dialogText.punctuationPause = punctuationPause; dialogText.beepPerCharacter = beepPerCharacter; dialogText.slowBeepsAt = slowBeepsAt; dialogText.fastBeepsAt = fastBeepsAt; - - GameObject typingAudio = null; + + AudioSource typingAudio = GetComponent(); if (characterTypingSound != null || writingSound != null) { - typingAudio = new GameObject("WritingSound"); - typingAudio.AddComponent(); - typingAudio.hideFlags = HideFlags.HideInHierarchy; - if (characterTypingSound != null) { - typingAudio.GetComponent().clip = characterTypingSound; + typingAudio.clip = characterTypingSound; } else if (writingSound != null) { - typingAudio.GetComponent().clip = writingSound; + typingAudio.clip = writingSound; } - typingAudio.GetComponent().loop = loopWritingSound; - typingAudio.GetComponent().Play(); + typingAudio.loop = loopWritingSound; - dialogText.typingAudio = typingAudio.GetComponent(); + // Start at full volume + typingAudio.volume = writingVolume; + SetTypingSoundVolume(true); + + typingAudio.Play(); } foreach (Token token in parser.tokens) @@ -489,8 +530,8 @@ namespace Fungus } prevStoryText = storyText.text; - - Destroy(typingAudio); + + SetTypingSoundVolume(false); if (onWritingComplete != null) { diff --git a/Assets/Fungus/Narrative/Scripts/DialogText.cs b/Assets/Fungus/Narrative/Scripts/DialogText.cs index ba126cdb..7da3913e 100644 --- a/Assets/Fungus/Narrative/Scripts/DialogText.cs +++ b/Assets/Fungus/Narrative/Scripts/DialogText.cs @@ -26,10 +26,10 @@ namespace Fungus public string colorText { get; set; } public float writingSpeed { get; set; } public float punctuationPause { get; set; } - public AudioSource typingAudio { get; set; } public float slowBeepsAt { get; set; } public float fastBeepsAt { get; set; } public bool beepPerCharacter { get; set; } + public Dialog parentDialog { get; set; } public virtual void Clear() { @@ -38,6 +38,8 @@ namespace Fungus public virtual void Append(string words) { + AudioSource typingAudio = parentDialog.GetComponent(); + if (beepPerCharacter && (writingSpeed <= slowBeepsAt || writingSpeed >= fastBeepsAt)) // beeps match character speed at these speeds oneBeep = true; else @@ -114,6 +116,8 @@ namespace Fungus */ public virtual bool UpdateGlyphs(bool instantComplete) { + AudioSource typingAudio = parentDialog.GetComponent(); + float elapsedTime = Time.deltaTime; foreach (Glyph glyph in glyphs) @@ -129,7 +133,7 @@ namespace Fungus if (typingAudio != null && glyph.hasPunctuationPause) { - typingAudio.volume = 0f; + parentDialog.SetTypingSoundVolume(false); } bool finished = false; @@ -140,7 +144,7 @@ namespace Fungus // Some elapsed time left over, so carry on to next glyph if ((oneBeep && typingAudio != null)) { - if(!typingAudio.isPlaying && + if (!typingAudio.isPlaying && (glyph.character != " " && glyph.character != "\t" && glyph.character != "\n" ) ) { typingAudio.PlayOneShot(typingAudio.clip); @@ -156,10 +160,9 @@ namespace Fungus // Check if we need to restore audio after a punctuation pause if (typingAudio != null && - glyph.hideTimer == 0f && - typingAudio.volume == 0f) + glyph.hideTimer == 0f) { - typingAudio.volume = 1f; + parentDialog.SetTypingSoundVolume(true); } if (finished) @@ -171,7 +174,7 @@ namespace Fungus if (typingAudio != null) { - typingAudio.Stop(); + parentDialog.SetTypingSoundVolume(false); } return true;