/// <summary> /// 移动资源 /// </summary> public static void MoveAssetEntry(AddressableAssetEntry entry, AddressableAssetGroup targetParent, bool readOnly = false, bool postEvent = true) { AddressableAssetSettings Settings = AddressableAssetSettingsDefaultObject.Settings; Settings.MoveEntry(entry, targetParent, readOnly, postEvent); }
/// <summary> /// Updates the group the asset should belong to. /// Assets will be stored in groups for the Locale they are used by unless they are used /// by more than 1 <see cref="Locale"/>, then they will be moved to the shared group. /// </summary> /// <param name="settings"></param> /// <param name="assetEntry"></param> /// <param name="createUndo">Used to indicate if an Undo operation should be created.</param> protected virtual void UpdateAssetGroup(AddressableAssetSettings settings, AddressableAssetEntry assetEntry, bool createUndo) { if (settings == null || assetEntry == null) { return; } // Find all the locales that are using the asset using the Addressable labels. var localesUsingAsset = assetEntry.labels.Where(AddressHelper.IsLocaleLabel); if (localesUsingAsset.Count() == 0) { var oldGroup = assetEntry.parentGroup; settings.RemoveAssetEntry(assetEntry.guid); if (oldGroup.entries.Count == 0) { if (createUndo) { // We cant use undo asset deletion so we will leave an empty group instead of deleting it. Undo.RecordObject(oldGroup, "Remove group"); } else { settings.RemoveGroup(oldGroup); } } return; } AddressableAssetGroup newGroup; if (localesUsingAsset.Count() == 1) { // If only 1 locale is using the asset then we will add it to a locale specific group. var localeId = AddressHelper.LocaleLabelToId(localesUsingAsset.First()); newGroup = LocalizationEditorSettings.Instance.GetGroup(settings, FormatAssetTableCollectionName(localeId), true, createUndo); } else { // More than one locale uses the asset so it goes to the shared assets group. newGroup = LocalizationEditorSettings.Instance.GetGroup(settings, LocalizationEditorSettings.SharedAssetGroupName, true, createUndo); } // Do we need to change the asset's group? if (newGroup != assetEntry.parentGroup) { if (createUndo) { Undo.RecordObject(newGroup, "Update asset group"); Undo.RecordObject(assetEntry.parentGroup, "Update asset group"); } var oldGroup = assetEntry.parentGroup; settings.MoveEntry(assetEntry, newGroup, true); if (oldGroup.entries.Count == 0) { // We only delete the asset when not creating an undo as we can not undo asset deletion. if (!createUndo) { settings.RemoveGroup(oldGroup); } } } }
static void SetAaEntry(AddressableAssetSettings aaSettings, List <TargetInfo> targetInfos, bool create) { /* * if (create && aaSettings.DefaultGroup.ReadOnly) * { * Debug.LogError("Current default group is ReadOnly. Cannot add addressable assets to it"); * return; * } */ Undo.RecordObject(aaSettings, "AddressableAssetSettings"); if (!create) { List <AddressableAssetEntry> removedEntries = new List <AddressableAssetEntry>(targetInfos.Count); for (int i = 0; i < targetInfos.Count; ++i) { AddressableAssetEntry e = aaSettings.FindAssetEntry(targetInfos[i].Guid); AddressableAssetUtility.OpenAssetIfUsingVCIntegration(e.parentGroup); removedEntries.Add(e); aaSettings.RemoveAssetEntry(removedEntries[i], false); } if (removedEntries.Count > 0) { aaSettings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryRemoved, removedEntries, true, false); } } else { AddressableAssetGroup parentGroup = aaSettings.DefaultGroup; var resourceTargets = targetInfos.Where(ti => AddressableAssetUtility.IsInResources(ti.Path)); if (resourceTargets.Any()) { var resourcePaths = resourceTargets.Select(t => t.Path).ToList(); var resourceGuids = resourceTargets.Select(t => t.Guid).ToList(); AddressableAssetUtility.SafeMoveResourcesToGroup(aaSettings, parentGroup, resourcePaths, resourceGuids); } var entriesCreated = new List <AddressableAssetEntry>(); var entriesMoved = new List <AddressableAssetEntry>(); var otherTargetInfos = targetInfos.Except(resourceTargets); foreach (var info in otherTargetInfos) { var hook = aaSettings.hook; AddressableAssetGroup assetGroup = default; string customAddress = default; if (hook != null) { hook.BeforeSetEntryOnInspectorGUI(info.Path, out assetGroup, out customAddress); } if (assetGroup == null) { assetGroup = aaSettings.DefaultGroup; } string guid = info.Guid; if (string.IsNullOrEmpty(guid)) { continue; } AddressableAssetEntry e = aaSettings.FindAssetEntry(guid); if (e != null) //move entry to where it should go... { aaSettings.MoveEntry(e, assetGroup, false, false); } else //create entry { e = aaSettings.CreateAndAddEntryToGroup_Custom(guid, assetGroup, false, false); } if (string.IsNullOrEmpty(customAddress) == false) { e.SetAddress(customAddress, false); } entriesCreated.Add(e); entriesMoved.Add(e); } bool openedInVC = false; if (entriesMoved.Count > 0) { AddressableAssetUtility.OpenAssetIfUsingVCIntegration(parentGroup); openedInVC = true; aaSettings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entriesMoved, true, false); } if (entriesCreated.Count > 0) { if (!openedInVC) { AddressableAssetUtility.OpenAssetIfUsingVCIntegration(parentGroup); } aaSettings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryAdded, entriesCreated, true, false); } } }