playMaker

Author Topic: Audio Play on Audio source and quick finish  (Read 4418 times)

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 770
Audio Play on Audio source and quick finish
« on: November 21, 2019, 04:06:17 AM »
So there it is. I arranged the code of this action for the wait function to work properly for both audio sources and one shot clips.
Previously, the WaitForEndOfClip boolean wasn't taken into account for audio sources, the test wasn't done in the subloop that played the audio source if no short clip was referenced (short clips have the priority over audio sources in the current action). So I also added more info to the AudioClip field to highlight this reality.

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2013. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Audio)]
[ActionTarget(typeof(AudioSource), "gameObject")]
[ActionTarget(typeof(AudioClip), "oneShotClip")]
[Tooltip("Plays the Audio Clip set with Set Audio Clip or in the Audio Source inspector on a Game Object. Optionally plays a one shot Audio Clip.")]
public class AudioPlay : FsmStateAction
{
[RequiredField]
[CheckForComponent(typeof(AudioSource))]
[Tooltip("The GameObject with an AudioSource component.")]
public FsmOwnerDefault gameObject;

[HasFloatSlider(0,1)]
[Tooltip("Set the volume.")]
public FsmFloat volume;

[ObjectType(typeof(AudioClip))]
[Tooltip("Optionally plays a 'one shot' AudioClip. The Audio source (above) will be ignored if an AudioClip is referenced.\nNOTE: Volume cannot be adjusted while playing a 'one shot' AudioClip.")]
public FsmObject oneShotClip;

[Tooltip("Wait until the end of the clip to send the Finish Event. Set to false to send the finish event immediately.")]
public FsmBool WaitForEndOfClip;

[Tooltip("Event to send when the action finishes.")]
public FsmEvent finishedEvent;

private AudioSource audio;

public override void Reset()
{
gameObject = null;
volume = 1f;
oneShotClip = null;
finishedEvent = null;
WaitForEndOfClip = true;
}

public override void OnEnter()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go != null)
{
// cache the AudioSource component

audio = go.GetComponent<AudioSource>();
if (audio != null)
{
var audioClip = oneShotClip.Value as AudioClip;

if (audioClip == null)
{
audio.Play();

if (!volume.IsNone)
{
audio.volume = volume.Value;
}
if (WaitForEndOfClip.Value == false)
{
Fsm.Event(finishedEvent);
Finish();
}

return;
}

if (!volume.IsNone)
{
audio.PlayOneShot(audioClip, volume.Value);
}
else
{
audio.PlayOneShot(audioClip);
}
if (WaitForEndOfClip.Value == false)
{
Fsm.Event(finishedEvent);
Finish();
}

return;
}
}

// Finish if failed to play sound

Finish();
}

public override void OnUpdate ()
{
if (audio == null)
{
Finish();
}
else
{
if (!audio.isPlaying)
{
Fsm.Event(finishedEvent);
Finish();
}
else if (!volume.IsNone && volume.Value != audio.volume)
{
audio.volume = volume.Value;
}
}
}

#if UNITY_EDITOR
public override string AutoName()
{
if (oneShotClip.Value != null && !oneShotClip.IsNone)
{
ActionHelpers.AutoName(this, oneShotClip);
}

return null;
}
#endif
}
}

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Audio Play on Audio source and quick finish
« Reply #1 on: January 07, 2020, 04:05:15 AM »
Hi,

 Cool, thanks for that, this will be in the next update of PlayMaker.

Bye,

 Jean