public void Init(AkInitializer akInitializer) { if (akInitializer == null) { UnityEngine.Debug.LogError("WwiseUnity: AkInitializer must not be null. Sound engine will not be initialized."); return; } #if UNITY_EDITOR if (UnityEngine.Application.isPlaying && !IsTheSingleOwningInitializer(akInitializer)) { UnityEngine.Debug.LogError("WwiseUnity: Sound engine is already initialized."); return; } var arguments = System.Environment.GetCommandLineArgs(); if (System.Array.IndexOf(arguments, "-nographics") >= 0 && System.Array.IndexOf(arguments, "-wwiseEnableWithNoGraphics") < 0) { return; } var isInitialized = false; try { isInitialized = AkSoundEngine.IsInitialized(); IsSoundEngineLoaded = true; } catch (System.DllNotFoundException) { IsSoundEngineLoaded = false; UnityEngine.Debug.LogWarning("WwiseUnity: AkSoundEngine is not loaded."); return; } #else var isInitialized = AkSoundEngine.IsInitialized(); #endif engineLogging = akInitializer.engineLogging; AkLogger.Instance.Init(); AKRESULT result; uint BankID; if (isInitialized) { #if UNITY_EDITOR if (UnityEngine.Application.isPlaying || UnityEditor.BuildPipeline.isBuildingPlayer) { AkSoundEngine.ClearBanks(); AkBankManager.Reset(); result = AkSoundEngine.LoadBank("Init.bnk", AkSoundEngine.AK_DEFAULT_POOL_ID, out BankID); if (result != AKRESULT.AK_Success) { UnityEngine.Debug.LogError("WwiseUnity: Failed load Init.bnk with result: " + result); } } result = AkCallbackManager.Init(akInitializer.callbackManagerBufferSize * 1024); if (result != AKRESULT.AK_Success) { UnityEngine.Debug.LogError("WwiseUnity: Failed to initialize Callback Manager. Terminate sound engine."); AkSoundEngine.Term(); return; } OnEnableEditorListener(akInitializer.gameObject); UnityEditor.EditorApplication.update += LateUpdate; #else UnityEngine.Debug.LogError("WwiseUnity: Sound engine is already initialized."); #endif return; } #if UNITY_EDITOR if (UnityEditor.BuildPipeline.isBuildingPlayer) { return; } #endif UnityEngine.Debug.Log("WwiseUnity: Initialize sound engine ..."); var basePath = s_DefaultBasePath; language = akInitializer.language; //Use default properties for most SoundEngine subsystem. //The game programmer should modify these when needed. See the Wwise SDK documentation for the initialization. //These settings may very well change for each target platform. var memSettings = new AkMemSettings(); memSettings.uMaxNumPools = 20; var deviceSettings = new AkDeviceSettings(); AkSoundEngine.GetDefaultDeviceSettings(deviceSettings); var streamingSettings = new AkStreamMgrSettings(); streamingSettings.uMemorySize = (uint)akInitializer.streamingPoolSize * 1024; var initSettings = new AkInitSettings(); AkSoundEngine.GetDefaultInitSettings(initSettings); initSettings.uDefaultPoolSize = (uint)akInitializer.defaultPoolSize * 1024; initSettings.uMonitorPoolSize = (uint)akInitializer.monitorPoolSize * 1024; initSettings.uMonitorQueuePoolSize = (uint)akInitializer.monitorQueuePoolSize * 1024; #if (!UNITY_ANDROID && !PLATFORM_LUMIN && !UNITY_WSA) || UNITY_EDITOR // Exclude WSA. It only needs the name of the DLL, and no path. initSettings.szPluginDLLPath = System.IO.Path.Combine(UnityEngine.Application.dataPath, "Plugins" + System.IO.Path.DirectorySeparatorChar); #elif PLATFORM_LUMIN && !UNITY_EDITOR initSettings.szPluginDLLPath = UnityEngine.Application.dataPath.Replace("Data", "bin") + System.IO.Path.DirectorySeparatorChar; #endif var platformSettings = new AkPlatformInitSettings(); AkSoundEngine.GetDefaultPlatformInitSettings(platformSettings); platformSettings.uLEngineDefaultPoolSize = (uint)akInitializer.lowerPoolSize * 1024; platformSettings.fLEngineDefaultPoolRatioThreshold = akInitializer.memoryCutoffThreshold; var musicSettings = new AkMusicSettings(); AkSoundEngine.GetDefaultMusicSettings(musicSettings); var spatialAudioSettings = new AkSpatialAudioInitSettings(); spatialAudioSettings.uPoolSize = (uint)akInitializer.spatialAudioPoolSize * 1024; spatialAudioSettings.uMaxSoundPropagationDepth = akInitializer.maxSoundPropagationDepth; spatialAudioSettings.uDiffractionFlags = (uint)akInitializer.diffractionFlags; #if UNITY_EDITOR AkSoundEngine.SetGameName(UnityEngine.Application.productName + " (Editor)"); #else AkSoundEngine.SetGameName(UnityEngine.Application.productName); #endif result = AkSoundEngine.Init(memSettings, streamingSettings, deviceSettings, initSettings, platformSettings, musicSettings, spatialAudioSettings, (uint)akInitializer.preparePoolSize * 1024); if (result != AKRESULT.AK_Success) { UnityEngine.Debug.LogError("WwiseUnity: Failed to initialize the sound engine. Abort."); AkSoundEngine.Term(); return; //AkSoundEngine.Init should have logged more details. } var basePathToSet = AkBasePathGetter.GetSoundbankBasePath(); if (string.IsNullOrEmpty(basePathToSet)) { UnityEngine.Debug.LogError("WwiseUnity: Couldn't find soundbanks base path. Terminate sound engine."); AkSoundEngine.Term(); return; } result = AkSoundEngine.SetBasePath(basePathToSet); if (result != AKRESULT.AK_Success) { UnityEngine.Debug.LogError("WwiseUnity: Failed to set soundbanks base path. Terminate sound engine."); AkSoundEngine.Term(); return; } #if !UNITY_SWITCH // Calling Application.persistentDataPath crashes Switch var decodedBankFullPath = GetDecodedBankFullPath(); // AkSoundEngine.SetDecodedBankPath creates the folders for writing to (if they don't exist) AkSoundEngine.SetDecodedBankPath(decodedBankFullPath); #endif AkSoundEngine.SetCurrentLanguage(language); #if !UNITY_SWITCH // Calling Application.persistentDataPath crashes Switch // AkSoundEngine.AddBasePath is currently only implemented for iOS and Android; No-op for all other platforms. AkSoundEngine.AddBasePath(UnityEngine.Application.persistentDataPath + System.IO.Path.DirectorySeparatorChar); // Adding decoded bank path last to ensure that it is the first one used when writing decoded banks. AkSoundEngine.AddBasePath(decodedBankFullPath); #endif result = AkCallbackManager.Init(akInitializer.callbackManagerBufferSize * 1024); if (result != AKRESULT.AK_Success) { UnityEngine.Debug.LogError("WwiseUnity: Failed to initialize Callback Manager. Terminate sound engine."); AkSoundEngine.Term(); return; } AkBankManager.Reset(); UnityEngine.Debug.Log("WwiseUnity: Sound engine initialized."); //Load the init bank right away. Errors will be logged automatically. result = AkSoundEngine.LoadBank("Init.bnk", AkSoundEngine.AK_DEFAULT_POOL_ID, out BankID); if (result != AKRESULT.AK_Success) { UnityEngine.Debug.LogError("WwiseUnity: Failed load Init.bnk with result: " + result); } #if UNITY_EDITOR OnEnableEditorListener(akInitializer.gameObject); UnityEditor.EditorApplication.update += LateUpdate; #endif }
public void Initialize() { if (ms_Instance != null) { //Don't init twice //Check if there are 2 objects with this script. If yes, remove this component. if (ms_Instance != this) { UnityEngine.Object.DestroyImmediate(this.gameObject); } return; } Debug.Log("WwiseUnity: Initialize sound engine ..."); //Use default properties for most SoundEngine subsystem. //The game programmer should modify these when needed. See the Wwise SDK documentation for the initialization. //These settings may very well change for each target platform. AkMemSettings memSettings = new AkMemSettings(); memSettings.uMaxNumPools = 20; AkDeviceSettings deviceSettings = new AkDeviceSettings(); AkSoundEngine.GetDefaultDeviceSettings(deviceSettings); AkStreamMgrSettings streamingSettings = new AkStreamMgrSettings(); streamingSettings.uMemorySize = (uint)streamingPoolSize * 1024; AkInitSettings initSettings = new AkInitSettings(); AkSoundEngine.GetDefaultInitSettings(initSettings); initSettings.uDefaultPoolSize = (uint)defaultPoolSize * 1024; initSettings.uMonitorPoolSize = (uint)monitorPoolSize * 1024; initSettings.uMonitorQueuePoolSize = (uint)monitorQueuePoolSize * 1024; #if (!UNITY_ANDROID && !UNITY_WSA) || UNITY_EDITOR // Exclude WSA. It only needs the name of the DLL, and no path. initSettings.szPluginDLLPath = Path.Combine(Application.dataPath, "Plugins" + Path.DirectorySeparatorChar); #endif AkPlatformInitSettings platformSettings = new AkPlatformInitSettings(); AkSoundEngine.GetDefaultPlatformInitSettings(platformSettings); platformSettings.uLEngineDefaultPoolSize = (uint)lowerPoolSize * 1024; platformSettings.fLEngineDefaultPoolRatioThreshold = memoryCutoffThreshold; AkMusicSettings musicSettings = new AkMusicSettings(); AkSoundEngine.GetDefaultMusicSettings(musicSettings); #if UNITY_EDITOR AkSoundEngine.SetGameName(Application.productName + " (Editor)"); #else AkSoundEngine.SetGameName(Application.productName); #endif AKRESULT result = AkSoundEngine.Init(memSettings, streamingSettings, deviceSettings, initSettings, platformSettings, musicSettings, (uint)preparePoolSize * 1024); if (result != AKRESULT.AK_Success) { Debug.LogError("WwiseUnity: Failed to initialize the sound engine. Abort."); return; //AkSoundEngine.Init should have logged more details. } ms_Instance = this; string basePathToSet = AkBasePathGetter.GetSoundbankBasePath(); if (string.IsNullOrEmpty(basePathToSet)) { return; } result = AkSoundEngine.SetBasePath(basePathToSet); if (result != AKRESULT.AK_Success) { return; } #if !UNITY_SWITCH // Calling Application.persistentDataPath crashes Switch AkSoundEngine.SetDecodedBankPath(GetDecodedBankFullPath()); #endif AkSoundEngine.SetCurrentLanguage(language); #if !UNITY_SWITCH // Calling Application.persistentDataPath crashes Switch // AkSoundEngine.AddBasePath is currently only implemented for iOS and Android; No-op for all other platforms. AkSoundEngine.AddBasePath(Application.persistentDataPath + Path.DirectorySeparatorChar); #endif result = AkCallbackManager.Init(callbackManagerBufferSize * 1024); if (result != AKRESULT.AK_Success) { Debug.LogError("WwiseUnity: Failed to initialize Callback Manager. Terminate sound engine."); AkSoundEngine.Term(); ms_Instance = null; return; } AkBankManager.Reset(); Debug.Log("WwiseUnity: Sound engine initialized."); //The sound engine should not be destroyed once it is initialized. DontDestroyOnLoad(this); #if UNITY_EDITOR //Redirect Wwise error messages into Unity console. AkCallbackManager.SetMonitoringCallback(ErrorLevel.ErrorLevel_All, CopyMonitoringInConsole); #endif //Load the init bank right away. Errors will be logged automatically. uint BankID; result = AkSoundEngine.LoadBank("Init.bnk", AkSoundEngine.AK_DEFAULT_POOL_ID, out BankID); if (result != AKRESULT.AK_Success) { Debug.LogError("WwiseUnity: Failed load Init.bnk with result: " + result.ToString()); } #if UNITY_EDITOR EditorApplication.playmodeStateChanged += OnEditorPlaymodeStateChanged; #endif }
public void Init(AkInitializer akInitializer) { engineLogging = akInitializer.engineLogging; AkLogger.Instance.Init(); AKRESULT result; uint BankID; if (AkSoundEngine.IsInitialized()) { #if UNITY_EDITOR if (Application.isPlaying) { AkSoundEngine.ClearBanks(); AkBankManager.Reset(); result = AkSoundEngine.LoadBank("Init.bnk", AkSoundEngine.AK_DEFAULT_POOL_ID, out BankID); if (result != AKRESULT.AK_Success) { Debug.LogError("WwiseUnity: Failed load Init.bnk with result: " + result.ToString()); } } result = AkCallbackManager.Init(akInitializer.callbackManagerBufferSize * 1024); if (result != AKRESULT.AK_Success) { Debug.LogError("WwiseUnity: Failed to initialize Callback Manager. Terminate sound engine."); AkSoundEngine.Term(); return; } EditorApplication.update += LateUpdate; #endif return; } #if UNITY_EDITOR if (BuildPipeline.isBuildingPlayer) { return; } #endif Debug.Log("WwiseUnity: Initialize sound engine ..."); basePath = akInitializer.basePath; language = akInitializer.language; //Use default properties for most SoundEngine subsystem. //The game programmer should modify these when needed. See the Wwise SDK documentation for the initialization. //These settings may very well change for each target platform. AkMemSettings memSettings = new AkMemSettings(); memSettings.uMaxNumPools = 20; AkDeviceSettings deviceSettings = new AkDeviceSettings(); AkSoundEngine.GetDefaultDeviceSettings(deviceSettings); AkStreamMgrSettings streamingSettings = new AkStreamMgrSettings(); streamingSettings.uMemorySize = (uint)akInitializer.streamingPoolSize * 1024; AkInitSettings initSettings = new AkInitSettings(); AkSoundEngine.GetDefaultInitSettings(initSettings); initSettings.uDefaultPoolSize = (uint)akInitializer.defaultPoolSize * 1024; initSettings.uMonitorPoolSize = (uint)akInitializer.monitorPoolSize * 1024; initSettings.uMonitorQueuePoolSize = (uint)akInitializer.monitorQueuePoolSize * 1024; #if (!UNITY_ANDROID && !UNITY_WSA) || UNITY_EDITOR // Exclude WSA. It only needs the name of the DLL, and no path. initSettings.szPluginDLLPath = Path.Combine(Application.dataPath, "Plugins" + Path.DirectorySeparatorChar); #endif AkPlatformInitSettings platformSettings = new AkPlatformInitSettings(); AkSoundEngine.GetDefaultPlatformInitSettings(platformSettings); platformSettings.uLEngineDefaultPoolSize = (uint)akInitializer.lowerPoolSize * 1024; platformSettings.fLEngineDefaultPoolRatioThreshold = akInitializer.memoryCutoffThreshold; AkMusicSettings musicSettings = new AkMusicSettings(); AkSoundEngine.GetDefaultMusicSettings(musicSettings); AkSpatialAudioInitSettings spatialAudioSettings = new AkSpatialAudioInitSettings(); spatialAudioSettings.uPoolSize = (uint)akInitializer.spatialAudioPoolSize * 1024; spatialAudioSettings.uMaxSoundPropagationDepth = akInitializer.maxSoundPropagationDepth; spatialAudioSettings.uDiffractionFlags = (uint)akInitializer.diffractionFlags; #if UNITY_EDITOR AkSoundEngine.SetGameName(Application.productName + " (Editor)"); #else AkSoundEngine.SetGameName(Application.productName); #endif result = AkSoundEngine.Init(memSettings, streamingSettings, deviceSettings, initSettings, platformSettings, musicSettings, spatialAudioSettings, (uint)akInitializer.preparePoolSize * 1024); if (result != AKRESULT.AK_Success) { Debug.LogError("WwiseUnity: Failed to initialize the sound engine. Abort."); AkSoundEngine.Term(); return; //AkSoundEngine.Init should have logged more details. } string basePathToSet = AkBasePathGetter.GetSoundbankBasePath(); if (string.IsNullOrEmpty(basePathToSet)) { Debug.LogError("WwiseUnity: Couldn't find soundbanks base path. Terminate sound engine."); AkSoundEngine.Term(); return; } result = AkSoundEngine.SetBasePath(basePathToSet); if (result != AKRESULT.AK_Success) { Debug.LogError("WwiseUnity: Failed to set soundbanks base path. Terminate sound engine."); AkSoundEngine.Term(); return; } #if !UNITY_SWITCH // Calling Application.persistentDataPath crashes Switch string decodedBankFullPath = GetDecodedBankFullPath(); // AkSoundEngine.SetDecodedBankPath creates the folders for writing to (if they don't exist) AkSoundEngine.SetDecodedBankPath(decodedBankFullPath); #endif AkSoundEngine.SetCurrentLanguage(language); #if !UNITY_SWITCH // Calling Application.persistentDataPath crashes Switch // AkSoundEngine.AddBasePath is currently only implemented for iOS and Android; No-op for all other platforms. AkSoundEngine.AddBasePath(Application.persistentDataPath + Path.DirectorySeparatorChar); // Adding decoded bank path last to ensure that it is the first one used when writing decoded banks. AkSoundEngine.AddBasePath(decodedBankFullPath); #endif result = AkCallbackManager.Init(akInitializer.callbackManagerBufferSize * 1024); if (result != AKRESULT.AK_Success) { Debug.LogError("WwiseUnity: Failed to initialize Callback Manager. Terminate sound engine."); AkSoundEngine.Term(); return; } AkBankManager.Reset(); Debug.Log("WwiseUnity: Sound engine initialized."); //Load the init bank right away. Errors will be logged automatically. result = AkSoundEngine.LoadBank("Init.bnk", AkSoundEngine.AK_DEFAULT_POOL_ID, out BankID); if (result != AKRESULT.AK_Success) { Debug.LogError("WwiseUnity: Failed load Init.bnk with result: " + result.ToString()); } #if UNITY_EDITOR #if UNITY_2017_2_OR_NEWER EditorApplication.pauseStateChanged += OnPauseStateChanged; #else EditorApplication.playmodeStateChanged += OnEditorPlaymodeStateChanged; #endif EditorApplication.update += LateUpdate; #endif }