public async void LoadBankAsync(WwiseAddressableSoundBank bank, AssetReferenceWwiseBankData bankData)
        {
            var AsyncHandle = bankData.LoadAssetAsync();
            await AsyncHandle.Task;

            if (AsyncHandle.IsValid() && AsyncHandle.Status == AsyncOperationStatus.Succeeded)
            {
                bank.eventNames = new HashSet <string>(AsyncHandle.Result.eventNames);
                var data = AsyncHandle.Result.RawData;
                bank.GCHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
                var result = AkSoundEngine.LoadBankMemoryCopy(bank.GCHandle.AddrOfPinnedObject(), (uint)data.Length, out uint bankID);
                if (result == AKRESULT.AK_Success)
                {
                    bank.soundbankId = bankID;
                    bank.loadState   = BankLoadState.Loaded;
                }
                else
                {
                    bank.soundbankId = INVALID_SOUND_BANK_ID;
                    bank.loadState   = BankLoadState.LoadFailed;
                    UnityEngine.Debug.Log($"Wwise Addressable Bank Manager : Sound Engine failed to load {bank.name} SoundBank");
                }
                bank.GCHandle.Free();
                Addressables.Release(AsyncHandle);

                string destinationDir;
                if (bank.StreamingMedia != null)
                {
                    foreach (var language in bank.StreamingMedia.Keys)
                    {
                        if (language == "SFX")
                        {
                            destinationDir = UnityEngine.Application.persistentDataPath;;
                        }
                        else
                        {
                            destinationDir = Path.Combine(UnityEngine.Application.persistentDataPath, language);
                        }

                        if (!Directory.Exists(destinationDir))
                        {
                            Directory.CreateDirectory(destinationDir);
                        }

                        foreach (var streamedAsset in bank.StreamingMedia[language].media)
                        {
                            if (streamedAsset == null)
                            {
                                UnityEngine.Debug.LogError($"Wwise Addressable Bank Manager: Streaming media asset referenced in {bank.name} SoundBank is null");
                                continue;
                            }
                            var loadHandle = streamedAsset.LoadAssetAsync();
                            await loadHandle.Task;

                            if (loadHandle.IsValid() && loadHandle.Status == AsyncOperationStatus.Succeeded)
                            {
                                AkAssetUtilities.UpdateWwiseFileIfNecessary(destinationDir, loadHandle.Result);
                                Addressables.Release(loadHandle);
                            }
                            else
                            {
                                UnityEngine.Debug.LogError($"Wwise Addressable Bank Manager: Failed to load streaming media asset {streamedAsset.id} in {bank.name} SoundBank");
                            }
                        }
                    }
                }
            }
            else
            {
                UnityEngine.Debug.Log($"Wwise Addressable Bank Manager : Failed to load {bank.name} SoundBank");
                bank.loadState = BankLoadState.LoadFailed;
                Addressables.Release(AsyncHandle);
            }

            //Make sure the asset is cleared from memory
            UnityEngine.Resources.UnloadUnusedAssets();
            OnBankLoaded(bank);
        }
Ejemplo n.º 2
0
        public void LoadBankEditor(WwiseAddressableSoundBank bank, AssetReferenceWwiseBankData bankData)
        {
            var p          = UnityEditor.AssetDatabase.GetAssetPath(bankData.editorAsset);
            var loadedBank = UnityEditor.AssetDatabase.LoadAssetAtPath <WwiseSoundBankAsset>(p);

            bank.eventNames = new HashSet <string>(loadedBank.eventNames);
            var data = loadedBank.RawData;

            bank.GCHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
            var result = AkSoundEngine.LoadBankMemoryCopy(bank.GCHandle.AddrOfPinnedObject(), (uint)data.Length, out uint bankID);

            if (result == AKRESULT.AK_Success)
            {
                bank.soundbankId = bankID;
            }
            else
            {
                bank.soundbankId = INVALID_SOUND_BANK_ID;
            }
            bank.GCHandle.Free();

            string outFolder = UnityEngine.Application.persistentDataPath;

            if (!Directory.Exists(outFolder))
            {
                Directory.CreateDirectory(outFolder);
            }

            string destinationDir;

            if (bank.StreamingMedia != null)
            {
                foreach (var language in bank.StreamingMedia.Keys)
                {
                    if (language == "SFX")
                    {
                        destinationDir = UnityEngine.Application.persistentDataPath;
                    }
                    else
                    {
                        destinationDir = Path.Combine(UnityEngine.Application.persistentDataPath, language);
                    }

                    if (!Directory.Exists(destinationDir))
                    {
                        Directory.CreateDirectory(destinationDir);
                    }

                    foreach (var streamedAsset in bank.StreamingMedia[language].media)
                    {
                        if (streamedAsset == null)
                        {
                            UnityEngine.Debug.LogError($"Wwise Addressable Bank Manager: Streaming media asset referenced in {bank.name} soundbank is null");
                            continue;
                        }

                        var mediaPath = UnityEditor.AssetDatabase.GetAssetPath(streamedAsset.editorAsset);
                        var media     = UnityEditor.AssetDatabase.LoadAssetAtPath <WwiseStreamingMediaAsset>(mediaPath);
                        AkAssetUtilities.UpdateWwiseFileIfNecessary(destinationDir, media);
                    }
                }
            }

            //Make sure the asset is cleared from memory
            UnityEngine.Resources.UnloadUnusedAssets();

            OnBankLoaded(bank);
        }