From b7815dea0c8d27e9d04b001a8c76130406b51dda Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Mon, 23 Feb 2015 17:55:42 +0000 Subject: [PATCH] Added Control Audio command #84 --- .../Fungus/Audio/Editor/ControlAudioEditor.cs | 60 +++++ .../Audio/Editor/ControlAudioEditor.cs.meta | 8 + .../Audio/Scripts/Commands/ControlAudio.cs | 251 ++++++++++++++++++ .../Scripts/Commands/ControlAudio.cs.meta | 8 + 4 files changed, 327 insertions(+) create mode 100644 Assets/Fungus/Audio/Editor/ControlAudioEditor.cs create mode 100644 Assets/Fungus/Audio/Editor/ControlAudioEditor.cs.meta create mode 100644 Assets/Fungus/Audio/Scripts/Commands/ControlAudio.cs create mode 100644 Assets/Fungus/Audio/Scripts/Commands/ControlAudio.cs.meta diff --git a/Assets/Fungus/Audio/Editor/ControlAudioEditor.cs b/Assets/Fungus/Audio/Editor/ControlAudioEditor.cs new file mode 100644 index 00000000..366b49e8 --- /dev/null +++ b/Assets/Fungus/Audio/Editor/ControlAudioEditor.cs @@ -0,0 +1,60 @@ +using UnityEditor; +using UnityEditorInternal; +using UnityEngine; +using UnityEngine.UI; +using UnityEngine.Events; +using System; +using System.Collections; +using System.Collections.Generic; +using Rotorz.ReorderableList; + + +namespace Fungus +{ + + [CustomEditor (typeof(ControlAudio))] + public class ControlAudioEditor : CommandEditor + { + protected SerializedProperty controlProp; + protected SerializedProperty audioSourceProp; + protected SerializedProperty startVolumeProp; + protected SerializedProperty endVolumeProp; + protected SerializedProperty fadeDurationProp; + protected SerializedProperty waitUntilFinishedProp; + + protected virtual void OnEnable() + { + controlProp = serializedObject.FindProperty("control"); + audioSourceProp = serializedObject.FindProperty("audioSource"); + startVolumeProp = serializedObject.FindProperty("startVolume"); + endVolumeProp = serializedObject.FindProperty("endVolume"); + fadeDurationProp = serializedObject.FindProperty("fadeDuration"); + waitUntilFinishedProp = serializedObject.FindProperty("waitUntilFinished"); + } + + public override void DrawCommandGUI() + { + serializedObject.Update(); + + ControlAudio t = target as ControlAudio; + + EditorGUILayout.PropertyField(controlProp); + EditorGUILayout.PropertyField(audioSourceProp); + string fadeLabel = "Fade Out Duration"; + if (t.control != ControlAudio.controlType.StopLoop && t.control != ControlAudio.controlType.PauseLoop) + { + fadeLabel = "Fade In Duration"; + string volumeLabel = "End Volume"; + if (t.control == ControlAudio.controlType.ChangeVolume) + { + fadeLabel = "Fade Duration"; + volumeLabel = "New Volume"; + } + EditorGUILayout.PropertyField(endVolumeProp,new GUIContent(volumeLabel)); + } + EditorGUILayout.PropertyField(fadeDurationProp,new GUIContent(fadeLabel)); + EditorGUILayout.PropertyField(waitUntilFinishedProp); + serializedObject.ApplyModifiedProperties(); + } + } +} \ No newline at end of file diff --git a/Assets/Fungus/Audio/Editor/ControlAudioEditor.cs.meta b/Assets/Fungus/Audio/Editor/ControlAudioEditor.cs.meta new file mode 100644 index 00000000..016fcb28 --- /dev/null +++ b/Assets/Fungus/Audio/Editor/ControlAudioEditor.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 5d1c9ed42e441784684b7d550738d944 +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: diff --git a/Assets/Fungus/Audio/Scripts/Commands/ControlAudio.cs b/Assets/Fungus/Audio/Scripts/Commands/ControlAudio.cs new file mode 100644 index 00000000..f01009c7 --- /dev/null +++ b/Assets/Fungus/Audio/Scripts/Commands/ControlAudio.cs @@ -0,0 +1,251 @@ +using UnityEngine; +using System.Collections; + +namespace Fungus +{ + [CommandInfo("Audio", + "Control Audio", + "Plays, loops, or stops an audiosource.")] + public class ControlAudio : Command + { + public enum controlType + { + PlayOnce, + PlayLoop, + PauseLoop, + StopLoop, + ChangeVolume + } + [Tooltip("What to do to audio")] + public controlType control; + + [Tooltip("Audio clip to play")] + public AudioSource audioSource; + + [Range(0,1)] + [Tooltip("Start audio at this volume")] + public float startVolume = 1; + + [Range(0,1)] + [Tooltip("End audio at this volume")] + public float endVolume = 1; + + [Tooltip("Time to fade between current volume level and target volume level.")] + public float fadeDuration; + + public bool waitUntilFinished = false; + + public override void OnEnter() + { + if (audioSource == null) + { + Continue(); + return; + } + + audioSource.volume = endVolume; + switch(control) + { + case controlType.PlayOnce: + StopAudio(); + PlayOnce(); + break; + case controlType.PlayLoop: + StopAudio(); + PlayLoop(); + break; + case controlType.PauseLoop: + PauseLoop(); + break; + case controlType.StopLoop: + StopLoop(); + break; + case controlType.ChangeVolume: + ChangeVolume(); + break; + } + if (!waitUntilFinished) + { + Continue(); + } + } + + protected void StopAudio() + { + if (audioSource == null) + { + return; + } + + StopLoop(); + } + + protected void PlayOnce() + { + if (fadeDuration > 0) + { + audioSource.volume = 0; + PlaySoundWithCallback(audioSource.audio.clip, endVolume, AudioFinished); + LeanTween.value(audioSource.gameObject,0,endVolume,fadeDuration + ).setOnUpdate( + (float updateVolume)=>{ + audioSource.volume = updateVolume; + } + ).setOnComplete( + ()=>{ + if (waitUntilFinished) + { + Continue(); + } + } + ); + } + else + { + audioSource.volume = 1; + PlaySoundWithCallback(audioSource.audio.clip, endVolume, AudioFinished); + } + } + + protected void PlayLoop() + { + if (fadeDuration > 0) + { + audioSource.volume = 0; + audioSource.loop = true; + audioSource.audio.Play(); + LeanTween.value(audioSource.gameObject,0,endVolume,fadeDuration + ).setOnUpdate( + (float updateVolume)=>{ + audioSource.volume = updateVolume; + } + ).setOnComplete( + ()=>{ + if (waitUntilFinished) + { + Continue(); + } + } + ); + } + else + { + audioSource.volume = 1; + audioSource.loop = true; + audioSource.audio.Play(); + } + } + + protected void PauseLoop() + { + if (fadeDuration > 0) + { + LeanTween.value(audioSource.gameObject,audioSource.volume,0,fadeDuration + ).setOnUpdate( + (float updateVolume)=>{ + audioSource.volume = updateVolume; + } + ).setOnComplete( + ()=>{ + + audioSource.audio.Pause(); + if (waitUntilFinished) + { + Continue(); + } + } + ); + } + else + { + audioSource.audio.Pause(); + } + } + + protected void StopLoop() + { + if (fadeDuration > 0) + { + LeanTween.value(audioSource.gameObject,audioSource.volume,0,fadeDuration + ).setOnUpdate( + (float updateVolume)=>{ + audioSource.volume = updateVolume; + } + ).setOnComplete( + ()=>{ + + audioSource.audio.Stop(); + if (waitUntilFinished) + { + Continue(); + } + } + ); + } + else + { + audioSource.audio.Stop(); + } + } + + protected void ChangeVolume() + { + LeanTween.value(audioSource.gameObject,audioSource.volume,endVolume,fadeDuration + ).setOnUpdate( + (float updateVolume)=>{ + audioSource.volume = updateVolume; + } + ); + } + + + // 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.audio.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() + { + Continue(); + } + + public override string GetSummary() + { + if (audioSource == null) + { + return "Error: No sound clip selected"; + } + string fadeType = ""; + if (fadeDuration > 0) + { + fadeType = " Fade out"; + if (control != controlType.StopLoop) + { + fadeType = " Fade in volume to " + endVolume; + } + if (control == controlType.ChangeVolume) + { + fadeType = " to " + endVolume; + } + fadeType += " over " + fadeDuration + " seconds."; + } + return control.ToString() + " \"" + audioSource.name + "\"" + fadeType; + } + + public override Color GetButtonColor() + { + return new Color32(242, 209, 176, 255); + } + } + +} \ No newline at end of file diff --git a/Assets/Fungus/Audio/Scripts/Commands/ControlAudio.cs.meta b/Assets/Fungus/Audio/Scripts/Commands/ControlAudio.cs.meta new file mode 100644 index 00000000..1b4fb1e7 --- /dev/null +++ b/Assets/Fungus/Audio/Scripts/Commands/ControlAudio.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: bc30c92f7ffe3d746ac76cd528d616e5 +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: