playMaker

Author Topic: AudioPlay volume fix  (Read 4791 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
AudioPlay volume fix
« on: January 07, 2013, 02:07:37 AM »
Hi everyone,

 Following some issues on the audioPlay official action, the volume was not updating, it would only set the volume when the action begins.
http://hutonggames.com/playmakerforum/index.php?topic=2765.0

 I have fixed the action, BUT, it can not work for oOne Shot Audio, ONLY the Audio source of the gameObject targeted can have it's volume controlled, playing a oneshot audio is a kind of a hack on the unity component itself ( implemened by Unity, so nothing PlayMaker can do about it)

 IF you want to use this action with a one shot audio and still control the volume, simply use "Set Audio Clip" before, and play the audio normally, then with this action revision, controlling the volume is possible.



   [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 the AudioSource component.")]
      public FsmOwnerDefault gameObject;
      
      [HasFloatSlider(0,1)]
      public FsmFloat volume;
      
      [ObjectType(typeof(AudioClip))]
      [Tooltip("Optionally play a 'one shot' AudioClip.")]
      public FsmObject oneShotClip;
      
      [Tooltip("Event to send when the AudioClip finished playing.")]
      public FsmEvent finishedEvent;

      AudioSource audio;
         
      
      public override void Reset()
      {
         gameObject = null;
         volume = 1f;
         oneShotClip = null;
      }

      public override void OnEnter()
      {
         var go = Fsm.GetOwnerDefaultTarget(gameObject);
         if (go != null)
         {
            // cache the AudioSource component
            
            audio = go.audio;
            if (audio != null)
            {
               var audioClip = oneShotClip.Value as AudioClip;

               if (audioClip == null)
               {
                  audio.Play();
                  
                  if (!volume.IsNone)
                  {
                     audio.volume = volume.Value;
                  }
                  
                  return;
               }
               
               if (!volume.IsNone)
               {
                  audio.PlayOneShot(audioClip, volume.Value);
               }
               else
               {
                  audio.PlayOneShot(audioClip);
               }
                  
               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.Value!= audio.volume)
            {

               audio.volume = volume.Value;
            }
            
         
         }
      }
   }
}]
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
   [ActionCategory(ActionCategory.Audio)]
   [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 the AudioSource component.")]
      public FsmOwnerDefault gameObject;
      
      [HasFloatSlider(0,1)]
      public FsmFloat volume;
      
      [ObjectType(typeof(AudioClip))]
      [Tooltip("Optionally play a 'one shot' AudioClip.")]
      public FsmObject oneShotClip;
      
      [Tooltip("Event to send when the AudioClip finished playing.")]
      public FsmEvent finishedEvent;

      AudioSource audio;
         
      
      public override void Reset()
      {
         gameObject = null;
         volume = 1f;
         oneShotClip = null;
      }

      public override void OnEnter()
      {
         var go = Fsm.GetOwnerDefaultTarget(gameObject);
         if (go != null)
         {
            // cache the AudioSource component
            
            audio = go.audio;
            if (audio != null)
            {
               var audioClip = oneShotClip.Value as AudioClip;

               if (audioClip == null)
               {
                  audio.Play();
                  
                  if (!volume.IsNone)
                  {
                     audio.volume = volume.Value;
                  }
                  
                  return;
               }
               
               if (!volume.IsNone)
               {
                  audio.PlayOneShot(audioClip, volume.Value);
               }
               else
               {
                  audio.PlayOneShot(audioClip);
               }
                  
               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.Value!= audio.volume)
            {

               audio.volume = volume.Value;
            }
            
         
         }
      }
   }
}

DylanJones

  • Playmaker Newbie
  • *
  • Posts: 5
Re: AudioPlay volume fix
« Reply #1 on: January 22, 2013, 11:20:37 PM »
Looking forward to trying this out.

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: AudioPlay volume fix
« Reply #2 on: January 22, 2013, 11:49:41 PM »
Thanks Jean! I've integrated this fix in the next update...

DylanJones

  • Playmaker Newbie
  • *
  • Posts: 5
Re: AudioPlay volume fix
« Reply #3 on: January 26, 2013, 10:35:48 PM »
Does not seem to have an effect on animated floats.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: AudioPlay volume fix
« Reply #4 on: January 28, 2013, 01:10:00 AM »
Hi,

 the way you animate the float that you inject in this action should not have any impact.

 I would suggest you debug this using the inspector and make the float animation a lot slower, to make sure that it first animates like you want, and that the values are correct ( you might animate the volume in a small range not noticeable to the hear)

bye,

Jean