Browse Source

Added Control Audio command #84

master
chrisgregan 10 years ago
parent
commit
b7815dea0c
  1. 60
      Assets/Fungus/Audio/Editor/ControlAudioEditor.cs
  2. 8
      Assets/Fungus/Audio/Editor/ControlAudioEditor.cs.meta
  3. 251
      Assets/Fungus/Audio/Scripts/Commands/ControlAudio.cs
  4. 8
      Assets/Fungus/Audio/Scripts/Commands/ControlAudio.cs.meta

60
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();
}
}
}

8
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:

251
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);
}
}
}

8
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:
Loading…
Cancel
Save