Advertisement
Guest User

Wwise PlayMaker Action with delay

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