2025-06-03 02:42:28 -04:00
#if ! ( UNITY_DASHBOARD_WIDGET | | UNITY_WEBPLAYER | | UNITY_WII | | UNITY_WIIU | | UNITY_NACL | | UNITY_FLASH | | UNITY_BLACKBERRY ) // Disable under unsupported platforms.
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
The content of this file includes portions of the proprietary AUDIOKINETIC Wwise
Technology released in source code form as part of the game integration package .
The content of this file may not be used without valid licenses to the
AUDIOKINETIC Wwise Technology .
Note that the use of the game engine is subject to the Unity ( R ) Terms of
Service at https : //unity3d.com/legal/terms-of-service
License Usage
Licensees holding valid licenses to the AUDIOKINETIC Wwise Technology may use
this file in accordance with the end user license agreement provided with the
software or , alternatively , in accordance with the terms contained
in a written agreement between you and Audiokinetic Inc .
2026-03-14 03:13:10 -04:00
Copyright ( c ) 2025 Audiokinetic Inc .
2025-06-03 02:42:28 -04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
#if AK_WWISE_ADDRESSABLES & & UNITY_ADDRESSABLES
using AK.Wwise.Unity.WwiseAddressables ;
#endif
public class AkSoundEngineController
{
private static AkSoundEngineController ms_Instance ;
public static AkSoundEngineController Instance
{
get
{
if ( ms_Instance = = null )
2026-03-14 03:13:10 -04:00
{
2025-06-03 02:42:28 -04:00
ms_Instance = new AkSoundEngineController ( ) ;
2026-03-14 03:13:10 -04:00
}
2025-06-03 02:42:28 -04:00
return ms_Instance ;
}
}
private AkSoundEngineController ( )
{
#if UNITY_EDITOR
UnityEditor . EditorApplication . pauseStateChanged + = OnPauseStateChanged ;
2026-03-14 03:13:10 -04:00
UnityEditor . EditorApplication . playModeStateChanged + = OnPlayModeStateChanged ;
AkUnitySoundEngineInitialization . Instance . terminationDelegate + = OnDisableEditorListener ;
AkUnitySoundEngineInitialization . Instance . initializationDelegate + = OnEnableEditorListener ;
2025-06-03 02:42:28 -04:00
#endif
}
~ AkSoundEngineController ( )
{
if ( ms_Instance = = this )
{
#if UNITY_EDITOR
UnityEditor . EditorApplication . pauseStateChanged - = OnPauseStateChanged ;
2026-03-14 03:13:10 -04:00
UnityEditor . EditorApplication . playModeStateChanged - = OnPlayModeStateChanged ;
AkUnitySoundEngineInitialization . Instance . terminationDelegate - = OnDisableEditorListener ;
AkUnitySoundEngineInitialization . Instance . initializationDelegate - = OnEnableEditorListener ;
2025-06-03 02:42:28 -04:00
DisableEditorLateUpdate ( ) ;
#endif
ms_Instance = null ;
}
}
#if UNITY_EDITOR
public void EnableEditorLateUpdate ( )
{
UnityEditor . EditorApplication . update + = LateUpdate ;
}
public void DisableEditorLateUpdate ( )
{
UnityEditor . EditorApplication . update - = LateUpdate ;
}
#endif
public void LateUpdate ( )
{
//Execute callbacks that occurred in last frame (not the current update)
AkRoomManager . Update ( ) ;
AkRoomAwareManager . UpdateRoomAwareObjects ( ) ;
AkCallbackManager . PostCallbacks ( ) ;
#if ! ( AK_WWISE_ADDRESSABLES & & UNITY_ADDRESSABLES )
AkBankManager . DoUnloadBanks ( ) ;
2026-03-14 03:13:10 -04:00
#elif WWISE_ADDRESSABLES_24_1_OR_LATER
AkAddressableBankManager . Instance . DoUnloadBank ( ) ;
2025-06-03 02:42:28 -04:00
#endif
#if UNITY_WEBGL & & ! UNITY_EDITOR
2026-03-14 03:13:10 -04:00
AkUnitySoundEngine . PerformStreamMgrIO ( ) ;
2025-06-03 02:42:28 -04:00
#endif
2026-03-14 03:13:10 -04:00
AkUnitySoundEngine . RenderAudio ( ) ;
2025-06-03 02:42:28 -04:00
}
#if AK_WWISE_ADDRESSABLES & & UNITY_ADDRESSABLES
private AkWwiseAddressablesInitializationSettings GetInitSettingsInstance ( )
{
return AkWwiseAddressablesInitializationSettings . Instance ;
}
#else
private AkWwiseInitializationSettings GetInitSettingsInstance ( )
{
return AkWwiseInitializationSettings . Instance ;
}
#endif
public void Init ( AkInitializer akInitializer )
{
2026-03-14 03:13:10 -04:00
#if UNITY_EDITOR
var arguments = System . Environment . GetCommandLineArgs ( ) ;
if ( UnityEngine . Application . isBatchMode & & System . Array . IndexOf ( arguments , "-wwiseEnableWithNoGraphics" ) < 0 )
{
UnityEngine . Debug . LogWarning ( "WwiseUnity: Sound engine will not be initialized in batch/nographics mode. To override, specify -wwiseEnableWithNoGraphics" ) ;
return ;
}
#endif
2025-06-03 02:42:28 -04:00
// Only initialize the room manager during play.
bool initRoomManager = true ;
#if UNITY_EDITOR
if ( ! UnityEditor . EditorApplication . isPlaying )
{
initRoomManager = false ;
}
#endif
if ( initRoomManager )
{
AkRoomManager . Init ( ) ;
}
if ( akInitializer = = null )
{
UnityEngine . Debug . LogError ( "WwiseUnity: AkInitializer must not be null. Sound engine will not be initialized." ) ;
return ;
}
2026-03-14 03:13:10 -04:00
var isInitialized = AkUnitySoundEngine . IsInitialized ( ) ;
2025-06-03 02:42:28 -04:00
AkLogger . Instance . Init ( ) ;
if ( isInitialized )
{
#if UNITY_EDITOR
2026-03-14 03:13:10 -04:00
if ( ! UnityEditor . EditorApplication . isPlaying )
2025-06-03 02:42:28 -04:00
{
OnEnableEditorListener ( akInitializer . gameObject ) ;
2026-03-14 03:13:10 -04:00
}
if ( AkUnitySoundEngineInitialization . Instance . ResetSoundEngine ( UnityEngine . Application . isPlaying | | UnityEditor . BuildPipeline . isBuildingPlayer ) )
{
2025-06-03 02:42:28 -04:00
EnableEditorLateUpdate ( ) ;
}
if ( UnityEditor . EditorApplication . isPaused & & UnityEngine . Application . isPlaying )
{
2026-03-14 03:13:10 -04:00
AkUnitySoundEngine . Suspend ( true ) ;
2025-06-03 02:42:28 -04:00
}
#else
UnityEngine . Debug . LogError ( "WwiseUnity: Sound engine is already initialized." ) ;
#endif
return ;
}
#if UNITY_EDITOR
if ( UnityEditor . BuildPipeline . isBuildingPlayer )
2026-03-14 03:13:10 -04:00
{
2025-06-03 02:42:28 -04:00
return ;
2026-03-14 03:13:10 -04:00
}
2025-06-03 02:42:28 -04:00
#endif
2026-03-14 03:13:10 -04:00
if ( ! AkUnitySoundEngineInitialization . Instance . InitializeSoundEngine ( ) )
{
2025-06-03 02:42:28 -04:00
return ;
2026-03-14 03:13:10 -04:00
}
2025-06-03 02:42:28 -04:00
#if UNITY_EDITOR
2026-03-14 03:13:10 -04:00
if ( ! UnityEditor . EditorApplication . isPlaying )
{
OnEnableEditorListener ( akInitializer . gameObject ) ;
}
2025-06-03 02:42:28 -04:00
EnableEditorLateUpdate ( ) ;
#endif
}
public void OnDisable ( )
{
#if UNITY_EDITOR
2026-03-14 03:13:10 -04:00
if ( UnityEditor . EditorApplication . isPlayingOrWillChangePlaymode )
{
OnDisableEditorListener ( ) ;
}
if ( ! AkUnitySoundEngineInitialization . Instance . ShouldKeepSoundEngineEnabled ( ) )
{
Terminate ( ) ;
}
2025-06-03 02:42:28 -04:00
#endif
}
public void Terminate ( )
{
2026-03-14 03:13:10 -04:00
AkUnitySoundEngineInitialization . Instance . TerminateSoundEngine ( ) ;
2025-06-03 02:42:28 -04:00
AkRoomManager . Terminate ( ) ;
}
// In the Editor, the sound needs to keep playing when switching windows (remote debugging in Wwise, for example).
// On iOS, application interruptions are handled in the sound engine already.
#if UNITY_EDITOR | | UNITY_IOS
public void OnApplicationPause ( bool pauseStatus )
{
}
public void OnApplicationFocus ( bool focus )
{
}
#elif UNITY_WEBGL
// On WebGL, allow background audio when browser is un-focused in development builds to make the Wwise Profiler usable.
public void OnApplicationPause ( bool pauseStatus )
{
if ( ! UnityEngine . Debug . isDebugBuild )
2026-03-14 03:13:10 -04:00
{
2025-06-03 02:42:28 -04:00
ActivateAudio ( ! pauseStatus ) ;
2026-03-14 03:13:10 -04:00
}
2025-06-03 02:42:28 -04:00
}
public void OnApplicationFocus ( bool focus )
{
if ( ! UnityEngine . Debug . isDebugBuild )
2026-03-14 03:13:10 -04:00
{
ActivateAudio ( focus , AkWwiseInitializationSettings . Instance . RenderDuringFocusLoss ) ;
}
2025-06-03 02:42:28 -04:00
}
#else
public void OnApplicationPause ( bool pauseStatus )
{
ActivateAudio ( ! pauseStatus ) ;
}
public void OnApplicationFocus ( bool focus )
{
#if ! UNITY_ANDROID
2026-03-14 03:13:10 -04:00
ActivateAudio ( focus , AkWwiseInitializationSettings . Instance . RenderDuringFocusLoss ) ;
2025-06-03 02:42:28 -04:00
#endif
}
#endif
#if UNITY_EDITOR
// Enable/Disable the audio when pressing play/pause in the editor.
private void OnPauseStateChanged ( UnityEditor . PauseState pauseState )
{
if ( UnityEngine . Application . isPlaying )
{
ActivateAudio ( pauseState ! = UnityEditor . PauseState . Paused ) ;
}
}
2026-03-14 03:13:10 -04:00
private void OnPlayModeStateChanged ( UnityEditor . PlayModeStateChange state )
{
if ( state . HasFlag ( UnityEditor . PlayModeStateChange . ExitingEditMode ) | | state . HasFlag ( UnityEditor . PlayModeStateChange . ExitingPlayMode ) )
{
AkUnitySoundEngine . StopAll ( ) ;
}
}
2025-06-03 02:42:28 -04:00
#endif
#if UNITY_EDITOR | | ! UNITY_IOS
private void ActivateAudio ( bool activate , bool renderAnyway = false )
{
2026-03-14 03:13:10 -04:00
if ( AkUnitySoundEngine . IsInitialized ( ) & & AkWwiseInitializationSettings . Instance . SuspendAudioDuringFocusLoss )
2025-06-03 02:42:28 -04:00
{
if ( activate )
2026-03-14 03:13:10 -04:00
{
AkUnitySoundEngine . WakeupFromSuspend ( ) ;
}
2025-06-03 02:42:28 -04:00
else
2026-03-14 03:13:10 -04:00
{
AkUnitySoundEngine . Suspend ( renderAnyway ) ;
}
2025-06-03 02:42:28 -04:00
2026-03-14 03:13:10 -04:00
AkUnitySoundEngine . RenderAudio ( ) ;
2025-06-03 02:42:28 -04:00
}
}
#endif
#if UNITY_EDITOR
#region Editor Listener
private UnityEngine . GameObject editorListenerGameObject ;
private bool IsPlayingOrIsNotInitialized
{
2026-03-14 03:13:10 -04:00
get { return UnityEngine . Application . isPlaying | | ! AkUnitySoundEngine . IsInitialized ( ) ; }
}
public bool EditorListenerIsInitialized ( )
{
return editorListenerGameObject ! = null ;
}
private void OnEnableEditorListener ( )
{
OnEnableEditorListener ( AkInitializer . GetAkInitializerGameObject ( ) ) ;
2025-06-03 02:42:28 -04:00
}
private void OnEnableEditorListener ( UnityEngine . GameObject gameObject )
{
2026-03-14 03:13:10 -04:00
if ( editorListenerGameObject ! = null | | IsPlayingOrIsNotInitialized )
{
return ;
}
if ( gameObject = = null )
{
2025-06-03 02:42:28 -04:00
return ;
2026-03-14 03:13:10 -04:00
}
2025-06-03 02:42:28 -04:00
editorListenerGameObject = gameObject ;
2026-03-14 03:13:10 -04:00
AkUnitySoundEngine . RegisterGameObj ( editorListenerGameObject , editorListenerGameObject . name ) ;
2025-06-03 02:42:28 -04:00
// Do not create AkGameObj component when adding this listener
2026-03-14 03:13:10 -04:00
var id = AkUnitySoundEngine . GetAkGameObjectID ( editorListenerGameObject ) ;
AkUnitySoundEngine . AddDefaultListener ( id ) ;
2025-06-03 02:42:28 -04:00
UnityEditor . EditorApplication . update + = UpdateEditorListenerPosition ;
}
private void OnDisableEditorListener ( )
{
if ( IsPlayingOrIsNotInitialized | | editorListenerGameObject = = null )
2026-03-14 03:13:10 -04:00
{
2025-06-03 02:42:28 -04:00
return ;
2026-03-14 03:13:10 -04:00
}
2025-06-03 02:42:28 -04:00
UnityEditor . EditorApplication . update - = UpdateEditorListenerPosition ;
2026-03-14 03:13:10 -04:00
var id = AkUnitySoundEngine . GetAkGameObjectID ( editorListenerGameObject ) ;
AkUnitySoundEngine . RemoveDefaultListener ( id ) ;
2025-06-03 02:42:28 -04:00
2026-03-14 03:13:10 -04:00
AkUnitySoundEngine . UnregisterGameObj ( editorListenerGameObject ) ;
2025-06-03 02:42:28 -04:00
editorListenerGameObject = null ;
2026-03-14 03:13:10 -04:00
editorListenerForward = UnityEngine . Vector3 . zero ;
editorListenerPosition = UnityEngine . Vector3 . zero ;
editorListenerUp = UnityEngine . Vector3 . zero ;
2025-06-03 02:42:28 -04:00
}
private UnityEngine . Vector3 editorListenerPosition = UnityEngine . Vector3 . zero ;
private UnityEngine . Vector3 editorListenerForward = UnityEngine . Vector3 . zero ;
private UnityEngine . Vector3 editorListenerUp = UnityEngine . Vector3 . zero ;
private void UpdateEditorListenerPosition ( )
{
if ( IsPlayingOrIsNotInitialized | | editorListenerGameObject = = null )
2026-03-14 03:13:10 -04:00
{
2025-06-03 02:42:28 -04:00
return ;
2026-03-14 03:13:10 -04:00
}
2025-06-03 02:42:28 -04:00
if ( UnityEditor . SceneView . lastActiveSceneView = = null )
2026-03-14 03:13:10 -04:00
{
2025-06-03 02:42:28 -04:00
return ;
2026-03-14 03:13:10 -04:00
}
2025-06-03 02:42:28 -04:00
var sceneViewCamera = UnityEditor . SceneView . lastActiveSceneView . camera ;
if ( sceneViewCamera = = null )
2026-03-14 03:13:10 -04:00
{
2025-06-03 02:42:28 -04:00
return ;
2026-03-14 03:13:10 -04:00
}
2025-06-03 02:42:28 -04:00
var sceneViewTransform = sceneViewCamera . transform ;
if ( sceneViewTransform = = null )
2026-03-14 03:13:10 -04:00
{
2025-06-03 02:42:28 -04:00
return ;
2026-03-14 03:13:10 -04:00
}
2025-06-03 02:42:28 -04:00
if ( editorListenerPosition = = sceneViewTransform . position & &
editorListenerForward = = sceneViewTransform . forward & &
editorListenerUp = = sceneViewTransform . up )
2026-03-14 03:13:10 -04:00
{
2025-06-03 02:42:28 -04:00
return ;
2026-03-14 03:13:10 -04:00
}
2025-06-03 02:42:28 -04:00
2026-03-14 03:13:10 -04:00
AkUnitySoundEngine . SetObjectPosition ( editorListenerGameObject , sceneViewTransform ) ;
2025-06-03 02:42:28 -04:00
editorListenerPosition = sceneViewTransform . position ;
editorListenerForward = sceneViewTransform . forward ;
editorListenerUp = sceneViewTransform . up ;
}
#endregion
#endif // UNITY_EDITOR
}
#endif // #if ! (UNITY_DASHBOARD_WIDGET || UNITY_WEBPLAYER || UNITY_WII || UNITY_WIIU || UNITY_NACL || UNITY_FLASH || UNITY_BLACKBERRY) // Disable under unsupported platforms.