Guest User

Wwise PlayMaker Action with delay

a guest
Feb 2nd, 2016
1,072
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.76 KB | None | 0 0
  1. //
  2. //   _  _  ___  _  _   _ _____ ___  __  __ ___ ___   _   _      _
  3. //  | \| |/ _ \| \| | /_\_   _/ _ \|  \/  |_ _/ __| | | | |_ __| |
  4. //  | .` | (_) | .` |/ _ \| || (_) | |\/| || | (__  | |_|  _/ _` |_
  5. //  |_|\_|\___/|_|\_/_/ \_\_| \___/|_|  |_|___\___| |____\__\__,_(_)
  6. //
  7. //  Created by Nonatomic Ltd.
  8. //  https://www.nonatomic.co.uk
  9. //
  10. //
  11. using UnityEngine;
  12. using HutongGames.PlayMaker;
  13. using Tooltip = HutongGames.PlayMaker.TooltipAttribute;
  14.  
  15. namespace Nonatomic.PlayMaker.Actions
  16. {
  17.     [ActionCategory("Wwise Audio")]
  18.     [Tooltip("Plays a Wwise Sound")]
  19.     public class PlayWwiseSound : FsmStateAction
  20.     {
  21.         public FsmOwnerDefault gameObject;
  22.        
  23.         [RequiredField]
  24.         [Tooltip("This is the name of the soundbank. You do not need to include the .bnk file extension")]
  25.         [Title("SoundBank")]
  26.         public FsmString soundBank;
  27.        
  28.         [RequiredField]
  29.         [Title("EventName")]
  30.         public FsmString eventName;
  31.        
  32.         [Title("Delay")]
  33.         public FsmFloat delay;
  34.        
  35.         [Title("One Shot")]
  36.         public FsmBool oneShot;
  37.         private bool triggered;
  38.        
  39.        
  40.         private float remainingDelay;
  41.         private GameObject go;
  42.        
  43.         public override void Reset()
  44.         {
  45.             gameObject = null;
  46.         }
  47.  
  48.         public override void OnEnter()
  49.         {
  50.             remainingDelay = delay.Value;
  51.        
  52.             go = Fsm.GetOwnerDefaultTarget(gameObject);
  53.              
  54.             //soundBank is the name of a Wwise soundBank to target. SoundBanks have a .bnk file extension
  55.             if(!soundBank.Value.Contains(".bnk"))
  56.             {
  57.                 soundBank.Value += ".bnk";
  58.             }
  59.              
  60.             //Collider is required
  61.             if(go.GetComponent<Collider>() == null)
  62.             {
  63.                 Debug.LogWarning("To play Wwise sound requires a collider on the target GameObject. Adding a default BoxCollider, but recommend you add your own");
  64.                 go.AddComponent<BoxCollider>();
  65.             }
  66.            
  67.             if(delay.Value == 0)
  68.             {
  69.                 PlaySound();
  70.             }
  71.         }
  72.        
  73.         public override void OnUpdate()
  74.         {
  75.            if(remainingDelay <= 0)
  76.             return;
  77.            
  78.            remainingDelay -= Time.deltaTime;
  79.            
  80.            if(remainingDelay <= 0)
  81.            {
  82.                PlaySound();
  83.            }
  84.         }
  85.        
  86.         private void PlaySound()
  87.         {
  88.             if(oneShot.Value && triggered)
  89.                 return;
  90.                
  91.             triggered = true;
  92.            
  93.             uint bankID;
  94.             AkSoundEngine.LoadBank(soundBank.Value, AkSoundEngine.AK_DEFAULT_POOL_ID, out bankID);
  95.             AkSoundEngine.PostEvent(eventName.Value, go);
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment