From f937aed6cb46443162bb13ff381103e1784f77dc Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Sun, 13 Mar 2016 22:56:57 +0000 Subject: [PATCH] Audio commands can use variables in properties --- .../Fungus/Audio/Editor/ControlAudioEditor.cs | 2 +- .../Audio/Scripts/Commands/ControlAudio.cs | 80 ++++++++++++------- .../Audio/Scripts/Commands/PlayUsfxrSound.cs | 36 +++++++-- .../Fungus/Flowchart/Editor/VariableEditor.cs | 4 + .../VariableTypes/AudioSourceVariable.cs | 51 ++++++++++++ .../VariableTypes/AudioSourceVariable.cs.meta | 12 +++ 6 files changed, 148 insertions(+), 37 deletions(-) create mode 100644 Assets/Fungus/Flowchart/Scripts/VariableTypes/AudioSourceVariable.cs create mode 100644 Assets/Fungus/Flowchart/Scripts/VariableTypes/AudioSourceVariable.cs.meta diff --git a/Assets/Fungus/Audio/Editor/ControlAudioEditor.cs b/Assets/Fungus/Audio/Editor/ControlAudioEditor.cs index a2f96c9a..5b13f995 100644 --- a/Assets/Fungus/Audio/Editor/ControlAudioEditor.cs +++ b/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"); diff --git a/Assets/Fungus/Audio/Scripts/Commands/ControlAudio.cs b/Assets/Fungus/Audio/Scripts/Commands/ControlAudio.cs index 3a12aebe..e1a17011 100644 --- a/Assets/Fungus/Audio/Scripts/Commands/ControlAudio.cs +++ b/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(); foreach (AudioSource a in audioSources) { - if ((a.GetComponent() != audioSource) && (a.tag == audioSource.tag)) + if ((a.GetComponent() != _audioSource.Value) && (a.tag == _audioSource.Value.tag)) { StopLoop(a.GetComponent()); } @@ -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().Play(); - LeanTween.value(audioSource.gameObject,0,endVolume,fadeDuration + _audioSource.Value.volume = 0; + _audioSource.Value.loop = true; + _audioSource.Value.GetComponent().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().Play(); + _audioSource.Value.volume = 1; + _audioSource.Value.loop = true; + _audioSource.Value.GetComponent().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().Pause(); + _audioSource.Value.GetComponent().Pause(); if (waitUntilFinished) { Continue(); @@ -180,7 +186,7 @@ namespace Fungus } else { - audioSource.GetComponent().Pause(); + _audioSource.Value.GetComponent().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; + } + } } } \ No newline at end of file diff --git a/Assets/Fungus/Audio/Scripts/Commands/PlayUsfxrSound.cs b/Assets/Fungus/Audio/Scripts/Commands/PlayUsfxrSound.cs index 8f3c6733..cff75f29 100644 --- a/Assets/Fungus/Audio/Scripts/Commands/PlayUsfxrSound.cs +++ b/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 = ""; + } + } + } } diff --git a/Assets/Fungus/Flowchart/Editor/VariableEditor.cs b/Assets/Fungus/Flowchart/Editor/VariableEditor.cs index bf0f8e0e..935bf295 100644 --- a/Assets/Fungus/Flowchart/Editor/VariableEditor.cs +++ b/Assets/Fungus/Flowchart/Editor/VariableEditor.cs @@ -327,4 +327,8 @@ namespace Fungus [CustomPropertyDrawer (typeof(TransformData))] public class TransformDataDrawer : VariableDataDrawer {} + + [CustomPropertyDrawer (typeof(AudioSourceData))] + public class AudioSourceDrawer : VariableDataDrawer + {} } \ No newline at end of file diff --git a/Assets/Fungus/Flowchart/Scripts/VariableTypes/AudioSourceVariable.cs b/Assets/Fungus/Flowchart/Scripts/VariableTypes/AudioSourceVariable.cs new file mode 100644 index 00000000..1dc14994 --- /dev/null +++ b/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 + {} + + [System.Serializable] + public struct AudioSourceData + { + [SerializeField] + [VariableProperty("", 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; + } + } + } + +} \ No newline at end of file diff --git a/Assets/Fungus/Flowchart/Scripts/VariableTypes/AudioSourceVariable.cs.meta b/Assets/Fungus/Flowchart/Scripts/VariableTypes/AudioSourceVariable.cs.meta new file mode 100644 index 00000000..47854598 --- /dev/null +++ b/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: