Skip to content

Instantly share code, notes, and snippets.

@athiedmann
Last active October 3, 2023 22:09
Show Gist options
  • Select an option

  • Save athiedmann/6753758e046659753dbefec7580ea773 to your computer and use it in GitHub Desktop.

Select an option

Save athiedmann/6753758e046659753dbefec7580ea773 to your computer and use it in GitHub Desktop.
A custom script for handling Wwise looping events within Unity. It acts like a component, assign the necessary events and select playback behaviour. Modify the script as needed for your project.
using UnityEngine;
public class WwisePlayerLoopEvents : MonoBehaviour
{
#if UNITY_EDITOR
[SerializeField] bool DebugScript;
#endif
[Header("Playback Settings")]
[SerializeField] bool playOnStart;
[SerializeField] bool akTriggerEnterExit;
[SerializeField] bool addRigidbody = true;
[SerializeField] float maxAttenuationDistance = 20.0f; // Manually defined maximum attenuation distance, set it equivalent to the max value of the Wwise attenuation
[Header("Wwise Events")]
[SerializeField] AK.Wwise.Event playLoopEvent;
[SerializeField] AK.Wwise.Event stopLoopEvent;
[Header("Trigger Object (only one needed if using AkTriggerEnter)")]
[SerializeField] string triggerObjectTagName = "Player";
[SerializeField] GameObject triggerGameObject;
private SphereCollider sphereCollider;
private Rigidbody AkRigidbody;
private void Awake()
{
InitializeTriggerColliderFromAttenuation();
SetOrAddRigidbody();
}
// Creates and sets sphere collider radius
private void InitializeTriggerColliderFromAttenuation()
{
if (akTriggerEnterExit)
{
sphereCollider = GetComponent<SphereCollider>();
if (sphereCollider == null)
{
Logger.LogWarning($"Sphere collider added during runtime at {gameObject.name}");
sphereCollider = gameObject.AddComponent<SphereCollider>();
}
sphereCollider.isTrigger = true;
sphereCollider.radius = maxAttenuationDistance;
}
else
{
sphereCollider = null;
return;
}
Logger.Log($"Sphere collider initialized at {gameObject.name}");
}
// Creates and initializes Rigidbody
private void SetOrAddRigidbody()
{
AkRigidbody = GetComponent<Rigidbody>();
if (AkRigidbody == null)
{
if (!addRigidbody)
{
AkRigidbody = null;
return;
}
AkRigidbody = gameObject.AddComponent<Rigidbody>(); // Add the Rigidbody component dynamically if it doesn't exist
Logger.LogWarning($"Rigidbody added during runtime for {gameObject.name}");
}
AkRigidbody.useGravity = false;
AkRigidbody.isKinematic = true;
if (DebugScript) Logger.Log($"Rigidbody set at {gameObject.name}");
}
private void Start()
{
if (!playLoopEvent.IsValid())
{
if (DebugScript) Logger.LogWarning($"No Wwise Play Loop Event assigned at {gameObject.name}.");
return;
}
if (playOnStart)
{
PlayLoopEvent();
if (DebugScript) Logger.Log($"Playing Wwise event on start at {gameObject.name}");
}
}
private void OnTriggerEnter(Collider other)
{
if (akTriggerEnterExit && (other.gameObject.CompareTag(triggerObjectTagName) || other.gameObject == triggerGameObject))
{
if (DebugScript) Logger.Log($"Player detected in {gameObject.name} audio trigger area");
PlayLoopEvent();
}
}
private void OnTriggerExit(Collider other)
{
if (akTriggerEnterExit && (other.gameObject.tag == triggerObjectTagName || other.gameObject == triggerGameObject))
{
if (DebugScript) Logger.Log($"Player left the {gameObject.name} audio trigger area");
StopLoopEvent();
}
}
public void PlayLoopEvent()
{
if (!playLoopEvent.IsValid())
{
return;
}
playLoopEvent.Post(gameObject);
if (DebugScript) Logger.Log($"Wwise Play event posted at {gameObject.name}");
}
public void StopLoopEvent()
{
if (!playLoopEvent.IsValid())
{
return;
}
if (!stopLoopEvent.IsValid())
{
if (DebugScript) Logger.LogWarning($"No Wwise Stop Loop Event assigned at {gameObject.name}, stopping Play Loop Event directly");
playLoopEvent.Stop(gameObject);
}
else
{
if (DebugScript) Logger.Log($"Wwise Stop event posted at {gameObject.name}");
stopLoopEvent.Post(gameObject);
}
}
// Call method to stop looping forever if a condition is met in-game
public void DestroySphereColliderAndStopEvent()
{
StopLoopEvent();
Destroy(sphereCollider);
if (DebugScript) Logger.Log($"Destroyed sphere collider and stopped event at {gameObject.name}");
Destroy(this);
}
}
#region UnityDebugLogScript
//using UnityEngine;
//public class WwiseLoopEvents : MonoBehaviour
//{
//#if UNITY_EDITOR
// [SerializeField] bool DebugScript;
//#endif
// [Header("Playback Settings")]
// [SerializeField] bool playOnStart;
// [SerializeField] bool akTriggerEnterExit;
// [SerializeField] bool addRigidbody = true;
// [SerializeField] float maxAttenuationDistance = 20.0f; // Manually defined maximum attenuation distance, set it equivalent to the max value of the Wwise attenuation
// [Header("Wwise Events")]
// [SerializeField] AK.Wwise.Event playLoopEvent;
// [SerializeField] AK.Wwise.Event stopLoopEvent;
// [Header("Trigger Object (only one needed if using AkTriggerEnter)")]
// [SerializeField] string triggerObjectTagName = "Player";
// [SerializeField] GameObject triggerGameObject;
// private SphereCollider sphereCollider;
// private Rigidbody AkRigidbody;
// private void Awake()
// {
// InitializeTriggerColliderFromAttenuation();
// SetOrAddRigidbody();
// }
// // Creates and sets sphere collider radius
// private void InitializeTriggerColliderFromAttenuation()
// {
// if (akTriggerEnterExit)
// {
// sphereCollider = GetComponent<SphereCollider>();
// if (sphereCollider == null)
// {
//#if UNITY_EDITOR
// DebugMessage($"Sphere collider added during runtime at {gameObject.name}", 1);
//#endif
// sphereCollider = gameObject.AddComponent<SphereCollider>();
// }
// sphereCollider.isTrigger = true;
// sphereCollider.radius = maxAttenuationDistance;
// }
// else
// {
// sphereCollider = null;
// return;
// }
//#if UNITY_EDITOR
// DebugMessage($"Sphere collider initialized at {gameObject.name}", 0);
//#endif
// }
// // Creates and initializes Rigidbody
// private void SetOrAddRigidbody()
// {
// AkRigidbody = GetComponent<Rigidbody>();
// if (AkRigidbody == null)
// {
// if (!addRigidbody)
// {
// AkRigidbody = null;
// return;
// }
// AkRigidbody = gameObject.AddComponent<Rigidbody>(); // Add the Rigidbody component dynamically if it doesn't exist
//#if UNITY_EDITOR
// DebugMessage($"Rigidbody added during runtime for {gameObject.name}", 1);
//#endif
// }
// AkRigidbody.useGravity = false;
// AkRigidbody.isKinematic = true;
//#if UNITY_EDITOR
// DebugMessage($"Rigidbody set at {gameObject.name}", 0);
//#endif
// }
// private void Start()
// {
// if (!playLoopEvent.IsValid())
// {
// DebugMessage($"No Wwise Play Loop Event assigned at {gameObject.name}.", 2);
// return;
// }
// if (playOnStart)
// {
// PlayLoopEvent();
//#if UNITY_EDITOR
// DebugMessage($"Playing Wwise event on start at {gameObject.name}", 0);
//#endif
// }
// }
// private void OnTriggerEnter(Collider other)
// {
// if (akTriggerEnterExit && (other.gameObject.CompareTag(triggerObjectTagName) || other.gameObject == triggerGameObject))
// {
//#if UNITY_EDITOR
// DebugMessage($"Player detected in {gameObject.name} audio trigger area", 0);
//#endif
// PlayLoopEvent();
// }
// }
// private void OnTriggerExit(Collider other)
// {
// if (akTriggerEnterExit && (other.gameObject.tag == triggerObjectTagName || other.gameObject == triggerGameObject))
// {
//#if UNITY_EDITOR
// DebugMessage($"Player left the {gameObject.name} audio trigger area", 0);
//#endif
// StopLoopEvent();
// }
// }
// public void PlayLoopEvent()
// {
// if (!playLoopEvent.IsValid())
// {
// return;
// }
// playLoopEvent.Post(gameObject);
//#if UNITY_EDITOR
// DebugMessage($"Wwise Play event posted at {gameObject.name}", 0);
//#endif
// }
// public void StopLoopEvent()
// {
// if (!playLoopEvent.IsValid())
// {
// return;
// }
// if (!stopLoopEvent.IsValid())
// {
//#if UNITY_EDITOR
// DebugMessage($"No Wwise Stop Loop Event assigned at {gameObject.name}, stopping Play Loop Event directly", 1);
//#endif
// playLoopEvent.Stop(gameObject);
// }
// else
// {
// stopLoopEvent.Post(gameObject);
//#if UNITY_EDITOR
// DebugMessage($"Wwise Stop event posted at {gameObject.name}", 0);
//#endif
// }
// }
// // Call method to stop looping forever if a condition is met in-game
// public void DestroySphereColliderAndStopEvent()
// {
// StopLoopEvent();
// Destroy(sphereCollider);
//#if UNITY_EDITOR
// DebugMessage($"Destroyed sphere collider and stopped event at {gameObject.name}", 0);
//#endif
// Destroy(this);
// }
//#if UNITY_EDITOR
// /// <summary>
// /// Allows easy customization of the different types of debug messages
// /// </summary>
// /// <param name="debugMessage"> Pass in string </param>
// /// <param name="warningType"> Assign 0 for log, 1 for warning, 2 for error </param>
// private void DebugMessage(string debugMessage, byte warningType)
// {
// if (!DebugScript)
// return;
// switch (warningType)
// {
// case 0:
// Debug.Log(debugMessage);
// return;
// case 1:
// Debug.LogWarning(debugMessage);
// return;
// case 2:
// Debug.LogError(debugMessage);
// return;
// default:
// return;
// }
// }
//#endif
//}
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment