Advertisement
Guest User

Untitled

a guest
Jul 14th, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 KB | None | 0 0
  1.  
  2.  
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. namespace HutongGames.PlayMaker.Actions
  7. {
  8.     [ActionCategory(ActionCategory.Audio)]
  9.     [Tooltip("Plays a Random Audio Clip at a position defined by a Game Object or a Vector3. If a position is defined, it takes priority over the game object. You can set the relative weight of the clips to control how often they are selected.")]
  10.     public class PlayRandomSound : FsmStateAction
  11.     {
  12.         public FsmOwnerDefault gameObject;
  13.         public FsmVector3 position;
  14.         [CompoundArray("Audio Clips", "Audio Clip", "Weight")]
  15.         [ObjectType(typeof(AudioClip))]
  16.         public FsmObject[] audioClips;
  17.         [HasFloatSlider(0, 1)]
  18.         public FsmFloat[] weights;
  19.         [HasFloatSlider(0, 1)]
  20.         public FsmFloat volume = 1f;
  21.        
  22.         public override void Reset()
  23.         {
  24.             gameObject = null;
  25.             position = new FsmVector3 { UseVariable = true };
  26.             weights = new FsmFloat[] {1,1,1};
  27.             volume = 1;
  28.         }
  29.  
  30.         public override void OnEnter()
  31.         {
  32.             DoPlayRandomClip();
  33.            
  34.             Finish();
  35.         }
  36.  
  37.         void DoPlayRandomClip()
  38.         {
  39.             if (audioClips.Length == 0) return;
  40.  
  41.             int randomIndex = ActionHelpers.GetRandomWeightedIndex(weights);
  42.            
  43.             if (randomIndex != -1)
  44.             {
  45.                 AudioClip clip = audioClips[randomIndex].Value as AudioClip;
  46.                 if (clip != null)
  47.                 {
  48.                     if (!position.IsNone)
  49.                     {
  50.                         AudioSource.PlayClipAtPoint(clip, position.Value, volume.Value);
  51.                     }
  52.                     else
  53.                     {
  54.                         GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
  55.                         if (go == null) return;
  56.                        
  57.                         AudioSource.PlayClipAtPoint(clip, go.transform.position, volume.Value);
  58.                     }              
  59.                 }
  60.             }
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement