49 lines
2.0 KiB
C#
49 lines
2.0 KiB
C#
|
|
#if ADDRESSABLES_INSTALLED
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using SubAssetsToolbox.Editor;
|
|||
|
|
using UnityEditor.AddressableAssets;
|
|||
|
|
using UnityEditor.AddressableAssets.Settings;
|
|||
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
namespace SubAssetsToolbox.Utilities
|
|||
|
|
{
|
|||
|
|
internal static class AddressableEntryMigrator
|
|||
|
|
{
|
|||
|
|
internal static void MigrateOrRemoveEntry(string oldGuid, string parentGuid, string parentName)
|
|||
|
|
{
|
|||
|
|
AddressableAssetSettings settings = AddressableAssetSettingsDefaultObject.Settings;
|
|||
|
|
if (settings == null) return;
|
|||
|
|
|
|||
|
|
AddressableAssetEntry oldEntry = settings.FindAssetEntry(oldGuid);
|
|||
|
|
if (oldEntry == null) return;
|
|||
|
|
|
|||
|
|
string oldName = oldEntry.MainAsset != null ? oldEntry.MainAsset.name : oldGuid;
|
|||
|
|
string address = oldEntry.address;
|
|||
|
|
AddressableAssetGroup group = oldEntry.parentGroup;
|
|||
|
|
HashSet<string> labels = new(oldEntry.labels);
|
|||
|
|
|
|||
|
|
AddressableAssetEntry parentEntry = settings.FindAssetEntry(parentGuid);
|
|||
|
|
|
|||
|
|
if (parentEntry != null)
|
|||
|
|
{
|
|||
|
|
// New host asset was already an Addressable, so the new sub-asset
|
|||
|
|
// is now addressable thanks to its parent – and the old entry can be removed.
|
|||
|
|
settings.RemoveAssetEntry(oldGuid);
|
|||
|
|
Debug.Log(string.Format(Constants.AddressableEntryRemoved, oldName, address, parentName));
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// The new host asset wasn't an Addressable, so the new host needs to become one
|
|||
|
|
settings.RemoveAssetEntry(oldGuid);
|
|||
|
|
AddressableAssetEntry newEntry = settings.CreateOrMoveEntry(parentGuid, group, false, false);
|
|||
|
|
newEntry.address = address;
|
|||
|
|
foreach (string label in labels)
|
|||
|
|
newEntry.SetLabel(label, true, true, false);
|
|||
|
|
settings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, newEntry, true);
|
|||
|
|
|
|||
|
|
Debug.Log(string.Format(Constants.AddressableEntryMigrated, oldName, address, parentName));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endif
|