When working with addressable Wwise assets, the easiest way to ensure that WwiseAddressableBank assets are up to date is to delete the generated soundbanks folder and regenerate soundbanks. Deleting addressable assets will clear the contents of addressables groups as the assets are deleted, so when they are reimported, their parent group and labels will be lost. This sample script included in the Wwise Unity Addressables package provides the functionality to preserve addressable asset metadata such as parent groups and labels.
We have added an optional setting that can be enabled to help restore lost groups and labels. It can be found in the Unity project settings, under Wwise Addressables. Once enabled, a target directory for the metadata can be specified. This directory should be within the Unity project's assets folder.
With these two mechanisms, asset metadata can be serialized before doing a cleanup, and will be reapplied to the assets when they are reimported after generating soundbanks.
#if AK_WWISE_ADDRESSABLES && UNITY_ADDRESSABLES
using UnityEditor.AddressableAssets;
using UnityEditor.AddressableAssets.Settings;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using System.IO;
namespace AK.Wwise.Unity.WwiseAddressables
{
[InitializeOnLoad]
class WwiseAddressableAssetMetadataPreserver
{
static WwiseAddressableAssetMetadataPreserver()
{
if (AkAddressablesEditorSettings.Instance.UseSampleMetadataPreserver)
{
BindMetadataDelegate();
}
}
public static void BindMetadataDelegate()
{
WwiseBankPostProcess.GetAssetMetadataDelegate += GetMetadata;
}
public static void UnbindMetaDataDelegate()
{
WwiseBankPostProcess.GetAssetMetadataDelegate -= GetMetadata;
}
[UnityEditor.MenuItem("Assets/Wwise/Addressables/Serialize addressable asset metadata")]
public static void PreserveAllWwiseAssetMetadata()
{
AddressableAssetSettings addressableSettings = AddressableAssetSettingsDefaultObject.Settings;
foreach (AddressableAssetGroup group in addressableSettings.groups)
{
foreach (AddressableAssetEntry assetEntry in group.entries)
{
if (assetEntry.MainAsset)
{
if (assetEntry.MainAsset.GetType().IsSubclassOf(typeof(WwiseAsset)))
{
CreateMetadataAsset(assetEntry.AssetPath, assetEntry.labels, group.name);
}
}
}
}
}
[UnityEditor.MenuItem("Assets/Wwise/Addressables/Preserve addressable asset metadata", true)]
public static bool ValidatePreserveAllWwiseAssetMetadata()
{
return AkAddressablesEditorSettings.Instance.UseSampleMetadataPreserver;
}
public static void CreateMetadataAsset(string assetPath, HashSet<string> assetLabels, string groupName)
{
string soundbankPath = assetPath.Replace(AkAssetUtilities.GetSoundbanksPath(), "");
var splitBankPath = soundbankPath.Split('/');
AddressableMetadata metaDataAsset = ScriptableObject.CreateInstance<AddressableMetadata>();
metaDataAsset.labels = new List<string>(assetLabels);
metaDataAsset.groupName = groupName;
var rootPath = AkAddressablesEditorSettings.Instance.MetadataPath;
if (!Directory.Exists(Path.Combine(Application.dataPath, rootPath)))
{
UnityEditor.AssetDatabase.CreateFolder("Assets", rootPath);
}
for (int i = 1; i < splitBankPath.Length - 1; i++)
{
if (!Directory.Exists(Path.Combine(Application.dataPath, Path.Combine(rootPath, splitBankPath[i]))))
{
AssetDatabase.CreateFolder(Path.Combine("Assets", rootPath), splitBankPath[i]);
}
rootPath = Path.Combine(rootPath, splitBankPath[i]);
}
string assetMetadataPath = Path.Combine("Assets", rootPath, Path.GetFileNameWithoutExtension(assetPath) + ".asset");
AddressableMetadata oldAsset = AssetDatabase.LoadAssetAtPath<AddressableMetadata>(assetMetadataPath);
if (oldAsset)
{
if (!metaDataAsset.IsDifferent(oldAsset))
{
return;
}
}
UnityEditor.AssetDatabase.CreateAsset(metaDataAsset, assetMetadataPath);
}
public static AddressableMetadata FindMetadataAsset(string assetName, string platformName, string languageName)
{
string MetadataAssetPath;
if (languageName !="SFX")
{
MetadataAssetPath = Path.Combine("Assets", AkAddressablesEditorSettings.Instance.MetadataPath, platformName, languageName, Path.GetFileNameWithoutExtension(assetName) + ".asset");
}
else
{
MetadataAssetPath = Path.Combine("Assets", AkAddressablesEditorSettings.Instance.MetadataPath, platformName, Path.GetFileNameWithoutExtension(assetName) + ".asset");
}
return AssetDatabase.LoadAssetAtPath<AddressableMetadata>(MetadataAssetPath);
}
public static bool GetMetadata(string assetName, string platformName, string languageName, ref AddressableMetadata metaData )
{
AddressableMetadata asset = FindMetadataAsset(assetName, platformName, languageName);
if ( asset )
{
metaData = asset;
return true;
}
return false;
}
}
}
#endif