From bf03a3c8a28d5f2bcb8394f5c8dad1b2eff27d05 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Tue, 15 Sep 2015 16:31:55 +0100 Subject: [PATCH] Fixed: ControlAudio waitUntilFinished property doesn't wait for correct time #174 --- .../Audio/Scripts/Commands/ControlAudio.cs | 55 +++--- Assets/Tests/Audio/AudioTests.unity | 169 +++++++++++++++++- 2 files changed, 184 insertions(+), 40 deletions(-) diff --git a/Assets/Fungus/Audio/Scripts/Commands/ControlAudio.cs b/Assets/Fungus/Audio/Scripts/Commands/ControlAudio.cs index 66e6b065..3a12aebe 100644 --- a/Assets/Fungus/Audio/Scripts/Commands/ControlAudio.cs +++ b/Assets/Fungus/Audio/Scripts/Commands/ControlAudio.cs @@ -98,29 +98,37 @@ namespace Fungus { if (fadeDuration > 0) { - audioSource.volume = 0; - PlaySoundWithCallback(audioSource.GetComponent().clip, endVolume, AudioFinished); - LeanTween.value(audioSource.gameObject,0,endVolume,fadeDuration + // Fade volume in + LeanTween.value(audioSource.gameObject, + audioSource.volume, + endVolume, + fadeDuration ).setOnUpdate( (float updateVolume)=>{ audioSource.volume = updateVolume; - } - ).setOnComplete( - ()=>{ - if (waitUntilFinished) - { - Continue(); - } - } - ); + }); } - else + + audioSource.PlayOneShot(audioSource.clip); + + if (waitUntilFinished) { - audioSource.volume = 1; - PlaySoundWithCallback(audioSource.GetComponent().clip, endVolume, AudioFinished); + StartCoroutine(WaitAndContinue()); } } - + + protected virtual IEnumerator WaitAndContinue() + { + // Poll the audiosource until playing has finished + // This allows for things like effects added to the audiosource. + while (audioSource.isPlaying) + { + yield return null; + } + + Continue(); + } + protected void PlayLoop() { if (fadeDuration > 0) @@ -212,21 +220,6 @@ namespace Fungus ); } - // Allows ControlAudio to "Wait Until Finished" playing audio before moving onto the next command - public delegate void AudioCallback(); - - public void PlaySoundWithCallback(AudioClip clip, float endVolume, AudioCallback callback) - { - audioSource.GetComponent().PlayOneShot(audioSource.clip, endVolume); - StartCoroutine(DelayedCallback(audioSource.clip.length, callback)); - } - - private IEnumerator DelayedCallback(float time, AudioCallback callback) - { - yield return new WaitForSeconds(time); - callback(); - } - void AudioFinished() { if (waitUntilFinished) diff --git a/Assets/Tests/Audio/AudioTests.unity b/Assets/Tests/Audio/AudioTests.unity index 8e929c38..5aa0ed61 100644 --- a/Assets/Tests/Audio/AudioTests.unity +++ b/Assets/Tests/Audio/AudioTests.unity @@ -37,9 +37,6 @@ RenderSettings: m_ReflectionIntensity: 1 m_CustomReflection: {fileID: 0} m_Sun: {fileID: 0} ---- !u!127 &3 -LevelGameManager: - m_ObjectHideFlags: 0 --- !u!157 &4 LightmapSettings: m_ObjectHideFlags: 0 @@ -68,6 +65,7 @@ LightmapSettings: m_TextureCompression: 1 m_FinalGather: 0 m_FinalGatherRayCount: 1024 + m_ReflectionCompression: 2 m_LightmapSnapshot: {fileID: 0} m_RuntimeCPUUsage: 25 --- !u!196 &5 @@ -155,6 +153,7 @@ Camera: m_RenderingPath: -1 m_TargetTexture: {fileID: 0} m_TargetDisplay: 0 + m_TargetEye: 3 m_HDR: 0 m_OcclusionCulling: 1 m_StereoConvergence: 10 @@ -205,6 +204,10 @@ GameObject: - 114: {fileID: 154271134} - 114: {fileID: 154271133} - 114: {fileID: 154271131} + - 114: {fileID: 154271140} + - 114: {fileID: 154271139} + - 114: {fileID: 154271138} + - 114: {fileID: 154271141} m_Layer: 0 m_Name: Flowchart m_TagString: Untagged @@ -249,7 +252,8 @@ MonoBehaviour: width: 1114 height: 859 selectedBlock: {fileID: 154271132} - selectedCommands: [] + selectedCommands: + - {fileID: 154271141} variables: [] description: stepPause: 0 @@ -257,6 +261,7 @@ MonoBehaviour: hideComponents: 1 saveSelection: 1 localizationId: + hideCommands: [] --- !u!114 &154271131 MonoBehaviour: m_ObjectHideFlags: 2 @@ -292,7 +297,7 @@ MonoBehaviour: nodeRect: serializedVersion: 2 x: 64 - y: 69 + y: 67 width: 120 height: 40 itemId: 0 @@ -305,6 +310,10 @@ MonoBehaviour: - {fileID: 154271134} - {fileID: 154271133} - {fileID: 154271131} + - {fileID: 154271140} + - {fileID: 154271139} + - {fileID: 154271141} + - {fileID: 154271138} --- !u!114 &154271133 MonoBehaviour: m_ObjectHideFlags: 2 @@ -392,6 +401,71 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: parentBlock: {fileID: 154271132} +--- !u!114 &154271138 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 154271128} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 4920f47cde1a84b11ad07b7317568494, type: 3} + m_Name: + m_EditorClassIdentifier: + itemId: 8 + errorMessage: + indentLevel: 0 +--- !u!114 &154271139 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 154271128} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: bc30c92f7ffe3d746ac76cd528d616e5, type: 3} + m_Name: + m_EditorClassIdentifier: + itemId: 7 + errorMessage: + indentLevel: 0 + control: 0 + audioSource: {fileID: 2113119600} + startVolume: 1 + endVolume: 1 + fadeDuration: 0 + waitUntilFinished: 1 +--- !u!114 &154271140 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 154271128} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 3315ad2ebb85443909a1203d56d9344e, type: 3} + m_Name: + m_EditorClassIdentifier: + itemId: 6 + errorMessage: + indentLevel: 0 + duration: 2 +--- !u!114 &154271141 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 154271128} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: ea6e8f632db87477eb750446b28d73a3, type: 3} + m_Name: + m_EditorClassIdentifier: + itemId: 0 + errorMessage: + indentLevel: 0 + commenterName: + commentText: Should be a pause here until audio finishes playing --- !u!1 &353260577 GameObject: m_ObjectHideFlags: 0 @@ -674,7 +748,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 61dddfdc5e0e44ca298d8f46f7f5a915, type: 3} m_Name: m_EditorClassIdentifier: - selectedFlowchart: {fileID: 1716415747} + selectedFlowchart: {fileID: 154271130} --- !u!4 &845793761 Transform: m_ObjectHideFlags: 1 @@ -730,6 +804,7 @@ AudioSource: m_Pitch: 1 Loop: 0 Mute: 0 + Spatialize: 0 Priority: 128 DopplerLevel: 1 MinDistance: 1 @@ -964,6 +1039,7 @@ MonoBehaviour: hideComponents: 1 saveSelection: 1 localizationId: + hideCommands: [] --- !u!114 &1716415748 MonoBehaviour: m_ObjectHideFlags: 2 @@ -1116,6 +1192,80 @@ MonoBehaviour: fadeWhenDone: 1 waitForClick: 1 setSayDialog: {fileID: 0} +--- !u!1 &1972173457 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + serializedVersion: 4 + m_Component: + - 4: {fileID: 1972173461} + - 114: {fileID: 1972173460} + - 114: {fileID: 1972173459} + - 114: {fileID: 1972173458} + m_Layer: 0 + m_Name: EventSystem + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &1972173458 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1972173457} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 1997211142, guid: f5f67c52d1564df4a8936ccd202a3bd8, type: 3} + m_Name: + m_EditorClassIdentifier: + m_ForceModuleActive: 0 +--- !u!114 &1972173459 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1972173457} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 1077351063, guid: f5f67c52d1564df4a8936ccd202a3bd8, type: 3} + m_Name: + m_EditorClassIdentifier: + m_HorizontalAxis: Horizontal + m_VerticalAxis: Vertical + m_SubmitButton: Submit + m_CancelButton: Cancel + m_InputActionsPerSecond: 10 + m_RepeatDelay: .5 + m_ForceModuleActive: 0 +--- !u!114 &1972173460 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1972173457} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: -619905303, guid: f5f67c52d1564df4a8936ccd202a3bd8, type: 3} + m_Name: + m_EditorClassIdentifier: + m_FirstSelected: {fileID: 0} + m_sendNavigationEvents: 1 + m_DragThreshold: 5 +--- !u!4 &1972173461 +Transform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 1972173457} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 4 --- !u!1 &2109416471 GameObject: m_ObjectHideFlags: 0 @@ -1131,7 +1281,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!114 &2109416472 MonoBehaviour: m_ObjectHideFlags: 0 @@ -1143,9 +1293,9 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: b1dba0b27b0864740a8720e920aa88c0, type: 3} m_Name: m_EditorClassIdentifier: - timeout: 5 + timeout: 10 ignored: 0 - succeedAfterAllAssertionsAreExecuted: 1 + succeedAfterAllAssertionsAreExecuted: 0 expectException: 0 expectedExceptionList: succeedWhenExceptionIsThrown: 0 @@ -1212,6 +1362,7 @@ AudioSource: m_Pitch: 1 Loop: 0 Mute: 0 + Spatialize: 0 Priority: 128 DopplerLevel: 1 MinDistance: 1