Browse Source

Audio commands can use variables in properties

master
chrisgregan 9 years ago
parent
commit
f937aed6cb
  1. 2
      Assets/Fungus/Audio/Editor/ControlAudioEditor.cs
  2. 80
      Assets/Fungus/Audio/Scripts/Commands/ControlAudio.cs
  3. 36
      Assets/Fungus/Audio/Scripts/Commands/PlayUsfxrSound.cs
  4. 4
      Assets/Fungus/Flowchart/Editor/VariableEditor.cs
  5. 51
      Assets/Fungus/Flowchart/Scripts/VariableTypes/AudioSourceVariable.cs
  6. 12
      Assets/Fungus/Flowchart/Scripts/VariableTypes/AudioSourceVariable.cs.meta

2
Assets/Fungus/Audio/Editor/ControlAudioEditor.cs

@ -28,7 +28,7 @@ namespace Fungus
return;
controlProp = serializedObject.FindProperty("control");
audioSourceProp = serializedObject.FindProperty("audioSource");
audioSourceProp = serializedObject.FindProperty("_audioSource");
startVolumeProp = serializedObject.FindProperty("startVolume");
endVolumeProp = serializedObject.FindProperty("endVolume");
fadeDurationProp = serializedObject.FindProperty("fadeDuration");

80
Assets/Fungus/Audio/Scripts/Commands/ControlAudio.cs

@ -1,4 +1,5 @@
using UnityEngine;
using UnityEngine.Serialization;
using System.Collections;
namespace Fungus
@ -6,8 +7,12 @@ namespace Fungus
[CommandInfo("Audio",
"Control Audio",
"Plays, loops, or stops an audiosource. Any AudioSources with the same tag as the target Audio Source will automatically be stoped.")]
public class ControlAudio : Command
public class ControlAudio : Command, ISerializationCallbackReceiver
{
#region Obsolete Properties
[HideInInspector] [FormerlySerializedAs("audioSource")] public AudioSource audioSourceOLD;
#endregion
public enum controlType
{
PlayOnce,
@ -21,7 +26,7 @@ namespace Fungus
public controlType control;
[Tooltip("Audio clip to play")]
public AudioSource audioSource;
public AudioSourceData _audioSource;
[Range(0,1)]
[Tooltip("Start audio at this volume")]
@ -39,13 +44,13 @@ namespace Fungus
public override void OnEnter()
{
if (audioSource == null)
if (_audioSource.Value == null)
{
Continue();
return;
}
audioSource.volume = endVolume;
_audioSource.Value.volume = endVolume;
switch(control)
{
case controlType.PlayOnce:
@ -60,7 +65,7 @@ namespace Fungus
PauseLoop();
break;
case controlType.StopLoop:
StopLoop(audioSource);
StopLoop(_audioSource.Value);
break;
case controlType.ChangeVolume:
ChangeVolume();
@ -79,7 +84,8 @@ namespace Fungus
protected void StopAudioWithSameTag()
{
// Don't stop audio if there's no tag assigned
if (audioSource.tag == "Untagged")
if (_audioSource.Value == null ||
_audioSource.Value.tag == "Untagged")
{
return;
}
@ -87,7 +93,7 @@ namespace Fungus
AudioSource[] audioSources = GameObject.FindObjectsOfType<AudioSource>();
foreach (AudioSource a in audioSources)
{
if ((a.GetComponent<AudioSource>() != audioSource) && (a.tag == audioSource.tag))
if ((a.GetComponent<AudioSource>() != _audioSource.Value) && (a.tag == _audioSource.Value.tag))
{
StopLoop(a.GetComponent<AudioSource>());
}
@ -99,17 +105,17 @@ namespace Fungus
if (fadeDuration > 0)
{
// Fade volume in
LeanTween.value(audioSource.gameObject,
audioSource.volume,
LeanTween.value(_audioSource.Value.gameObject,
_audioSource.Value.volume,
endVolume,
fadeDuration
).setOnUpdate(
(float updateVolume)=>{
audioSource.volume = updateVolume;
_audioSource.Value.volume = updateVolume;
});
}
audioSource.PlayOneShot(audioSource.clip);
_audioSource.Value.PlayOneShot(_audioSource.Value.clip);
if (waitUntilFinished)
{
@ -121,7 +127,7 @@ namespace Fungus
{
// Poll the audiosource until playing has finished
// This allows for things like effects added to the audiosource.
while (audioSource.isPlaying)
while (_audioSource.Value.isPlaying)
{
yield return null;
}
@ -133,13 +139,13 @@ namespace Fungus
{
if (fadeDuration > 0)
{
audioSource.volume = 0;
audioSource.loop = true;
audioSource.GetComponent<AudioSource>().Play();
LeanTween.value(audioSource.gameObject,0,endVolume,fadeDuration
_audioSource.Value.volume = 0;
_audioSource.Value.loop = true;
_audioSource.Value.GetComponent<AudioSource>().Play();
LeanTween.value(_audioSource.Value.gameObject,0,endVolume,fadeDuration
).setOnUpdate(
(float updateVolume)=>{
audioSource.volume = updateVolume;
_audioSource.Value.volume = updateVolume;
}
).setOnComplete(
()=>{
@ -152,9 +158,9 @@ namespace Fungus
}
else
{
audioSource.volume = 1;
audioSource.loop = true;
audioSource.GetComponent<AudioSource>().Play();
_audioSource.Value.volume = 1;
_audioSource.Value.loop = true;
_audioSource.Value.GetComponent<AudioSource>().Play();
}
}
@ -162,15 +168,15 @@ namespace Fungus
{
if (fadeDuration > 0)
{
LeanTween.value(audioSource.gameObject,audioSource.volume,0,fadeDuration
LeanTween.value(_audioSource.Value.gameObject,_audioSource.Value.volume,0,fadeDuration
).setOnUpdate(
(float updateVolume)=>{
audioSource.volume = updateVolume;
_audioSource.Value.volume = updateVolume;
}
).setOnComplete(
()=>{
audioSource.GetComponent<AudioSource>().Pause();
_audioSource.Value.GetComponent<AudioSource>().Pause();
if (waitUntilFinished)
{
Continue();
@ -180,7 +186,7 @@ namespace Fungus
}
else
{
audioSource.GetComponent<AudioSource>().Pause();
_audioSource.Value.GetComponent<AudioSource>().Pause();
}
}
@ -188,7 +194,7 @@ namespace Fungus
{
if (fadeDuration > 0)
{
LeanTween.value(source.gameObject,audioSource.volume,0,fadeDuration
LeanTween.value(source.gameObject,_audioSource.Value.volume,0,fadeDuration
).setOnUpdate(
(float updateVolume)=>{
source.volume = updateVolume;
@ -212,10 +218,10 @@ namespace Fungus
protected void ChangeVolume()
{
LeanTween.value(audioSource.gameObject,audioSource.volume,endVolume,fadeDuration
LeanTween.value(_audioSource.Value.gameObject,_audioSource.Value.volume,endVolume,fadeDuration
).setOnUpdate(
(float updateVolume)=>{
audioSource.volume = updateVolume;
_audioSource.Value.volume = updateVolume;
}
);
}
@ -230,7 +236,7 @@ namespace Fungus
public override string GetSummary()
{
if (audioSource == null)
if (_audioSource.Value == null)
{
return "Error: No sound clip selected";
}
@ -248,13 +254,29 @@ namespace Fungus
}
fadeType += " over " + fadeDuration + " seconds.";
}
return control.ToString() + " \"" + audioSource.name + "\"" + fadeType;
return control.ToString() + " \"" + _audioSource.Value.name + "\"" + fadeType;
}
public override Color GetButtonColor()
{
return new Color32(242, 209, 176, 255);
}
//
// ISerializationCallbackReceiver implementation
//
public virtual void OnBeforeSerialize()
{}
public virtual void OnAfterDeserialize()
{
if (audioSourceOLD != null)
{
_audioSource.Value = audioSourceOLD;
audioSourceOLD = null;
}
}
}
}

36
Assets/Fungus/Audio/Scripts/Commands/PlayUsfxrSound.cs

@ -1,28 +1,33 @@
namespace Fungus {
using System;
using UnityEngine;
using UnityEngine.Serialization;
[CommandInfo("Audio",
"Play Usfxr Sound",
"Plays a usfxr synth sound. Use the usfxr editor [Tools > Fungus > Utilities > Generate usfxr Sound Effects] to create the SettingsString. Set a ParentTransform if using positional sound. See https://github.com/zeh/usfxr for more information about usfxr.")]
[AddComponentMenu("")]
public class PlayUsfxrSound : Command
public class PlayUsfxrSound : Command, ISerializationCallbackReceiver
{
#region Obsolete Properties
[HideInInspector] [FormerlySerializedAs("SettingsString")] public String SettingsStringOLD;
#endregion
protected SfxrSynth _synth = new SfxrSynth();
[Tooltip("Transform to use for positional audio")]
public Transform ParentTransform = null;
[Tooltip("Settings string which describes the audio")]
public String SettingsString = "";
public StringData _SettingsString = new StringData("");
[Tooltip("Time to wait before executing the next command")]
public float waitDuration = 0;
//Call this if the settings have changed
protected void UpdateCache() {
if (SettingsString != null) {
_synth.parameters.SetSettingsString(SettingsString);
if (_SettingsString.Value != null) {
_synth.parameters.SetSettingsString(_SettingsString.Value);
_synth.CacheSound();
}
}
@ -51,17 +56,34 @@
}
public override string GetSummary() {
if (String.IsNullOrEmpty(SettingsString)) {
if (String.IsNullOrEmpty(_SettingsString.Value)) {
return "Settings String hasn't been set!";
}
if (ParentTransform != null) {
return "" + ParentTransform.name + ": " + SettingsString;
return "" + ParentTransform.name + ": " + _SettingsString.Value;
}
return "Camera.main: " + SettingsString;
return "Camera.main: " + _SettingsString.Value;
}
public override Color GetButtonColor() {
return new Color32(128, 200, 200, 255);
}
//
// ISerializationCallbackReceiver implementation
//
public virtual void OnBeforeSerialize()
{}
public virtual void OnAfterDeserialize()
{
if (SettingsStringOLD != "")
{
_SettingsString.Value = SettingsStringOLD;
SettingsStringOLD = "";
}
}
}
}

4
Assets/Fungus/Flowchart/Editor/VariableEditor.cs

@ -327,4 +327,8 @@ namespace Fungus
[CustomPropertyDrawer (typeof(TransformData))]
public class TransformDataDrawer : VariableDataDrawer<TransformVariable>
{}
[CustomPropertyDrawer (typeof(AudioSourceData))]
public class AudioSourceDrawer : VariableDataDrawer<AudioSourceVariable>
{}
}

51
Assets/Fungus/Flowchart/Scripts/VariableTypes/AudioSourceVariable.cs

@ -0,0 +1,51 @@
using UnityEngine;
using System.Collections;
namespace Fungus
{
[VariableInfo("Other", "AudioSource")]
[AddComponentMenu("")]
public class AudioSourceVariable : VariableBase<AudioSource>
{}
[System.Serializable]
public struct AudioSourceData
{
[SerializeField]
[VariableProperty("<Value>", typeof(AudioSourceVariable))]
public AudioSourceVariable audioSourceRef;
[SerializeField]
public AudioSource audioSourceVal;
public AudioSourceData(AudioSource v)
{
audioSourceVal = v;
audioSourceRef = null;
}
public static implicit operator AudioSource(AudioSourceData audioSourceData)
{
return audioSourceData.Value;
}
public AudioSource Value
{
get { return (audioSourceRef == null) ? audioSourceVal : audioSourceRef.value; }
set { if (audioSourceRef == null) { audioSourceVal = value; } else { audioSourceRef.value = value; } }
}
public string GetDescription()
{
if (audioSourceRef == null)
{
return audioSourceVal.ToString();
}
else
{
return audioSourceRef.key;
}
}
}
}

12
Assets/Fungus/Flowchart/Scripts/VariableTypes/AudioSourceVariable.cs.meta

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: fe0d9dc5080b040bd8daff067b407916
timeCreated: 1457892188
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Loading…
Cancel
Save