Browse Source

Add writing sound volume property on Dialogs #108

master
chrisgregan 10 years ago
parent
commit
cd29f7a297
  1. 71
      Assets/Fungus/Narrative/Resources/SayDialog.prefab
  2. 63
      Assets/Fungus/Narrative/Scripts/Dialog.cs
  3. 17
      Assets/Fungus/Narrative/Scripts/DialogText.cs

71
Assets/Fungus/Narrative/Resources/SayDialog.prefab

@ -121,6 +121,7 @@ GameObject:
- 114: {fileID: 11488906} - 114: {fileID: 11488906}
- 225: {fileID: 22588896} - 225: {fileID: 22588896}
- 114: {fileID: 11488894} - 114: {fileID: 11488894}
- 82: {fileID: 8294266}
m_Layer: 5 m_Layer: 5
m_Name: SayDialog m_Name: SayDialog
m_TagString: Untagged m_TagString: Untagged
@ -128,6 +129,75 @@ GameObject:
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 1 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 --- !u!95 &9588894
Animator: Animator:
serializedVersion: 3 serializedVersion: 3
@ -255,6 +325,7 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
writingSpeed: 30 writingSpeed: 30
writingSound: {fileID: 8300000, guid: f637c5ce9d10e45c7855ed89bfc6b97e, type: 3} writingSound: {fileID: 8300000, guid: f637c5ce9d10e45c7855ed89bfc6b97e, type: 3}
writingVolume: 1
loopWritingSound: 1 loopWritingSound: 1
beepPerCharacter: 0 beepPerCharacter: 0
slowBeepsAt: 10 slowBeepsAt: 10

63
Assets/Fungus/Narrative/Scripts/Dialog.cs

@ -15,6 +15,8 @@ namespace Fungus
public float writingSpeed = 60; public float writingSpeed = 60;
public AudioClip writingSound; public AudioClip writingSound;
[Range(0,1)]
public float writingVolume = 1f;
public bool loopWritingSound = true; public bool loopWritingSound = true;
public bool beepPerCharacter = false; public bool beepPerCharacter = false;
public float slowBeepsAt = 10f; public float slowBeepsAt = 10f;
@ -50,6 +52,9 @@ namespace Fungus
protected AudioSource voiceOverAudio; protected AudioSource voiceOverAudio;
// The current target writing volume to move towards
protected float targetWritingVolume;
protected virtual void LateUpdate() protected virtual void LateUpdate()
{ {
wasPointerClicked = false; wasPointerClicked = false;
@ -67,6 +72,30 @@ namespace Fungus
wasPointerClicked = true; wasPointerClicked = true;
clickCooldownTimer = 0.2f; clickCooldownTimer = 0.2f;
} }
// Fade writing sound
AudioSource audio = GetComponent<AudioSource>();
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) public virtual void ShowDialog(bool visible)
@ -90,6 +119,18 @@ namespace Fungus
} }
} }
public virtual void SetTypingSoundVolume(bool audible)
{
if (audible)
{
targetWritingVolume = writingVolume;
}
else
{
targetWritingVolume = 0f;
}
}
public virtual void FadeInDialog() public virtual void FadeInDialog()
{ {
LeanTween.cancel(dialogCanvas.gameObject); LeanTween.cancel(dialogCanvas.gameObject);
@ -271,32 +312,32 @@ namespace Fungus
} }
DialogText dialogText = new DialogText(); DialogText dialogText = new DialogText();
dialogText.parentDialog = this;
dialogText.writingSpeed = writingSpeed; dialogText.writingSpeed = writingSpeed;
dialogText.punctuationPause = punctuationPause; dialogText.punctuationPause = punctuationPause;
dialogText.beepPerCharacter = beepPerCharacter; dialogText.beepPerCharacter = beepPerCharacter;
dialogText.slowBeepsAt = slowBeepsAt; dialogText.slowBeepsAt = slowBeepsAt;
dialogText.fastBeepsAt = fastBeepsAt; dialogText.fastBeepsAt = fastBeepsAt;
GameObject typingAudio = null; AudioSource typingAudio = GetComponent<AudioSource>();
if (characterTypingSound != null || writingSound != null) if (characterTypingSound != null || writingSound != null)
{ {
typingAudio = new GameObject("WritingSound");
typingAudio.AddComponent<AudioSource>();
typingAudio.hideFlags = HideFlags.HideInHierarchy;
if (characterTypingSound != null) if (characterTypingSound != null)
{ {
typingAudio.GetComponent<AudioSource>().clip = characterTypingSound; typingAudio.clip = characterTypingSound;
} }
else if (writingSound != null) else if (writingSound != null)
{ {
typingAudio.GetComponent<AudioSource>().clip = writingSound; typingAudio.clip = writingSound;
} }
typingAudio.GetComponent<AudioSource>().loop = loopWritingSound; typingAudio.loop = loopWritingSound;
typingAudio.GetComponent<AudioSource>().Play();
// Start at full volume
typingAudio.volume = writingVolume;
SetTypingSoundVolume(true);
dialogText.typingAudio = typingAudio.GetComponent<AudioSource>(); typingAudio.Play();
} }
foreach (Token token in parser.tokens) foreach (Token token in parser.tokens)
@ -490,7 +531,7 @@ namespace Fungus
prevStoryText = storyText.text; prevStoryText = storyText.text;
Destroy(typingAudio); SetTypingSoundVolume(false);
if (onWritingComplete != null) if (onWritingComplete != null)
{ {

17
Assets/Fungus/Narrative/Scripts/DialogText.cs

@ -26,10 +26,10 @@ namespace Fungus
public string colorText { get; set; } public string colorText { get; set; }
public float writingSpeed { get; set; } public float writingSpeed { get; set; }
public float punctuationPause { get; set; } public float punctuationPause { get; set; }
public AudioSource typingAudio { get; set; }
public float slowBeepsAt { get; set; } public float slowBeepsAt { get; set; }
public float fastBeepsAt { get; set; } public float fastBeepsAt { get; set; }
public bool beepPerCharacter { get; set; } public bool beepPerCharacter { get; set; }
public Dialog parentDialog { get; set; }
public virtual void Clear() public virtual void Clear()
{ {
@ -38,6 +38,8 @@ namespace Fungus
public virtual void Append(string words) public virtual void Append(string words)
{ {
AudioSource typingAudio = parentDialog.GetComponent<AudioSource>();
if (beepPerCharacter && (writingSpeed <= slowBeepsAt || writingSpeed >= fastBeepsAt)) // beeps match character speed at these speeds if (beepPerCharacter && (writingSpeed <= slowBeepsAt || writingSpeed >= fastBeepsAt)) // beeps match character speed at these speeds
oneBeep = true; oneBeep = true;
else else
@ -114,6 +116,8 @@ namespace Fungus
*/ */
public virtual bool UpdateGlyphs(bool instantComplete) public virtual bool UpdateGlyphs(bool instantComplete)
{ {
AudioSource typingAudio = parentDialog.GetComponent<AudioSource>();
float elapsedTime = Time.deltaTime; float elapsedTime = Time.deltaTime;
foreach (Glyph glyph in glyphs) foreach (Glyph glyph in glyphs)
@ -129,7 +133,7 @@ namespace Fungus
if (typingAudio != null && if (typingAudio != null &&
glyph.hasPunctuationPause) glyph.hasPunctuationPause)
{ {
typingAudio.volume = 0f; parentDialog.SetTypingSoundVolume(false);
} }
bool finished = false; bool finished = false;
@ -140,7 +144,7 @@ namespace Fungus
// Some elapsed time left over, so carry on to next glyph // Some elapsed time left over, so carry on to next glyph
if ((oneBeep && typingAudio != null)) if ((oneBeep && typingAudio != null))
{ {
if(!typingAudio.isPlaying && if (!typingAudio.isPlaying &&
(glyph.character != " " && glyph.character != "\t" && glyph.character != "\n" ) ) (glyph.character != " " && glyph.character != "\t" && glyph.character != "\n" ) )
{ {
typingAudio.PlayOneShot(typingAudio.clip); typingAudio.PlayOneShot(typingAudio.clip);
@ -156,10 +160,9 @@ namespace Fungus
// Check if we need to restore audio after a punctuation pause // Check if we need to restore audio after a punctuation pause
if (typingAudio != null && if (typingAudio != null &&
glyph.hideTimer == 0f && glyph.hideTimer == 0f)
typingAudio.volume == 0f)
{ {
typingAudio.volume = 1f; parentDialog.SetTypingSoundVolume(true);
} }
if (finished) if (finished)
@ -171,7 +174,7 @@ namespace Fungus
if (typingAudio != null) if (typingAudio != null)
{ {
typingAudio.Stop(); parentDialog.SetTypingSoundVolume(false);
} }
return true; return true;

Loading…
Cancel
Save