在使用 Wwise Addressables 素材时,要想确保更新 WwiseAddressableBank 素材,最简单的方法是删除生成的 SoundBanks 文件夹并重新生成 SoundBank。在删除 Addressables 素材时,会清除 Addressables 分组的内容。在重新导入时,其父组和标签将会丢失。Wwise Unity Addressables Package 中包含的这个示例脚本提供相应功能来保留 Addressables 素材元数据(如父组和标签)。
#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;
}
为每个 Wwise Addressables 素材创建 AddressableMetadata 素材来追踪分组和标签。
[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