@ -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 . Valu e = = null )
{
Continue ( ) ;
return ;
}
audioSource . volume = endVolume ;
_ audioSource . Valu e . volume = endVolume ;
switch ( control )
{
case controlType . PlayOnce :
@ -60,7 +65,7 @@ namespace Fungus
PauseLoop ( ) ;
break ;
case controlType . StopLoop :
StopLoop ( audioSource ) ;
StopLoop ( _ audioSource . Valu e ) ;
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 . Valu e ) & & ( a . tag = = _ audioSource . Valu e . 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 . Valu e . gameObject ,
_ audioSource . Valu e . volume ,
endVolume ,
fadeDuration
) . setOnUpdate (
( float updateVolume ) = > {
audioSource . volume = updateVolume ;
_ audioSource . Valu e . volume = updateVolume ;
} ) ;
}
audioSource . PlayOneShot ( audioSource . clip ) ;
_ audioSource . Valu e . PlayOneShot ( _ audioSource . Valu e . 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 . Valu e . 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 . Valu e . volume = 0 ;
_ audioSource . Valu e . loop = true ;
_ audioSource . Valu e . GetComponent < AudioSource > ( ) . Play ( ) ;
LeanTween . value ( _ audioSource . Valu e . gameObject , 0 , endVolume , fadeDuration
) . setOnUpdate (
( float updateVolume ) = > {
audioSource . volume = updateVolume ;
_ audioSource . Valu e . volume = updateVolume ;
}
) . setOnComplete (
( ) = > {
@ -152,9 +158,9 @@ namespace Fungus
}
else
{
audioSource . volume = 1 ;
audioSource . loop = true ;
audioSource . GetComponent < AudioSource > ( ) . Play ( ) ;
_ audioSource . Valu e . volume = 1 ;
_ audioSource . Valu e . loop = true ;
_ audioSource . Valu e . GetComponent < AudioSource > ( ) . Play ( ) ;
}
}
@ -162,15 +168,15 @@ namespace Fungus
{
if ( fadeDuration > 0 )
{
LeanTween . value ( audioSource . gameObject , audioSource . volume , 0 , fadeDuration
LeanTween . value ( _ audioSource . Valu e . gameObject , _ audioSource . Valu e . volume , 0 , fadeDuration
) . setOnUpdate (
( float updateVolume ) = > {
audioSource . volume = updateVolume ;
_ audioSource . Valu e . volume = updateVolume ;
}
) . setOnComplete (
( ) = > {
audioSource . GetComponent < AudioSource > ( ) . Pause ( ) ;
_ audioSource . Valu e . GetComponent < AudioSource > ( ) . Pause ( ) ;
if ( waitUntilFinished )
{
Continue ( ) ;
@ -180,7 +186,7 @@ namespace Fungus
}
else
{
audioSource . GetComponent < AudioSource > ( ) . Pause ( ) ;
_ audioSource . Valu e . 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 . Valu e . 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 . Valu e . gameObject , _ audioSource . Valu e . volume , endVolume , fadeDuration
) . setOnUpdate (
( float updateVolume ) = > {
audioSource . volume = updateVolume ;
_ audioSource . Valu e . volume = updateVolume ;
}
) ;
}
@ -230,7 +236,7 @@ namespace Fungus
public override string GetSummary ( )
{
if ( audioSource = = null )
if ( _ audioSource . Valu e = = 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 . Valu e . name + "\"" + fadeType ;
}
public override Color GetButtonColor ( )
{
return new Color32 ( 2 4 2 , 2 0 9 , 1 7 6 , 2 5 5 ) ;
}
/ /
// ISerializationCallbackReceiver implementation
/ /
public virtual void OnBeforeSerialize ( )
{ }
public virtual void OnAfterDeserialize ( )
{
if ( audioSourceOLD ! = null )
{
_ audioSource . Value = audioSourceOLD ;
audioSourceOLD = null ;
}
}
}
}