Advertisement
Guest User

Untitled

a guest
Jul 14th, 2014
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 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.             audioClips = new FsmObject[3];
  28.             volume = 1;
  29.         }
  30.  
  31.         public override void OnEnter()
  32.         {
  33.             DoPlayRandomClip();
  34.            
  35.             Finish();
  36.         }
  37.  
  38.         void DoPlayRandomClip()
  39.         {
  40.             if (audioClips.Length == 0) return;
  41.  
  42.             int randomIndex = ActionHelpers.GetRandomWeightedIndex(weights);
  43.            
  44.             if (randomIndex != -1)
  45.             {
  46.                 AudioClip clip = audioClips[randomIndex].Value as AudioClip;
  47.                 if (clip != null)
  48.                 {
  49.                     if (!position.IsNone)
  50.                     {
  51.                         AudioSource.PlayClipAtPoint(clip, position.Value, volume.Value);
  52.                     }
  53.                     else
  54.                     {
  55.                         GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
  56.                         if (go == null) return;
  57.                        
  58.                         AudioSource.PlayClipAtPoint(clip, go.transform.position, volume.Value);
  59.                     }              
  60.                 }
  61.             }
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement