/// <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 = ListPool <LocaleIdentifier> .Get(); foreach (var label in assetEntry.labels) { if (AddressHelper.TryGetLocaleLabelToId(label, out var id)) { localesUsingAsset.Add(id); } } // If no Locales depend on this asset then we can just remove it 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); } } ListPool <LocaleIdentifier> .Release(localesUsingAsset); return; } AddressableGroupRules.AddAssetToGroup(assetEntry.MainAsset, localesUsingAsset, settings, createUndo); }
/// <summary> /// Add a localized asset to the asset table. /// This function will ensure the localization system adds the asset to the Addressables system and sets the asset up for use. /// </summary> /// <param name="table">The table to add the asset to, must be part of the collection.</param> /// <param name="entryReference">The table entry Key or Key Id.</param> /// <param name="asset">The asset to add.</param> /// <param name="createUndo">Should an undo operation be created?</param> public virtual void AddAssetToTable(AssetTable table, TableEntryReference entryReference, Object asset, bool createUndo = false) { if (table == null) { throw new ArgumentNullException(nameof(table), "Can not add asset to null table"); } if (asset == null) { throw new ArgumentNullException(nameof(asset), "Can not add null asset to table"); } if (!ContainsTable(table)) { throw new Exception("The table does not belong to this collection."); } if (!EditorUtility.IsPersistent(table)) { throw new AssetNotPersistentException(table); } if (!EditorUtility.IsPersistent(asset)) { throw new AssetNotPersistentException(asset); } // Add the asset to the Addressables system and setup the table with the key to guid mapping. var aaSettings = LocalizationEditorSettings.Instance.GetAddressableAssetSettings(true); if (aaSettings == null) { return; } using (new UndoScope("Add asset to table", createUndo)) { if (createUndo) { Undo.RecordObject(aaSettings, "Add asset to table"); } // Remove the old asset first var assetGuid = LocalizationEditorSettings.Instance.GetAssetGuid(asset); var tableEntry = table.GetEntryFromReference(entryReference); if (tableEntry != null) { if (tableEntry.Guid != assetGuid) { RemoveAssetFromTable(table, entryReference, createUndo); } } // Has the asset already been added? Perhaps it is being used by multiple tables or the user has added it manually. var entry = aaSettings.FindAssetEntry(assetGuid); var entryLabel = AddressHelper.FormatAssetLabel(table.LocaleIdentifier.Code); aaSettings.AddLabel(entryLabel); if (entry == null) { entry = AddressableGroupRules.AddAssetToGroup(asset, new[] { table.LocaleIdentifier }, aaSettings, createUndo); entry.SetLabel(entryLabel, true, true); entry.address = LocalizationEditorSettings.Instance.FindUniqueAssetAddress(asset.name); } else { if (createUndo) { Undo.RecordObject(entry.parentGroup, "Add asset to table"); } entry.SetLabel(entryLabel, true, true); UpdateAssetGroup(aaSettings, entry, createUndo); } if (createUndo) { Undo.RecordObjects(new Object[] { table, table.SharedData }, "Add asset to table"); } EditorUtility.SetDirty(table); EditorUtility.SetDirty(table.SharedData); tableEntry = table.AddEntryFromReference(entryReference, assetGuid); SetEntryAssetType(tableEntry.KeyId, asset.GetType(), table.LocaleIdentifier.Code); LocalizationEditorSettings.EditorEvents.RaiseAssetTableEntryAdded(this, table, tableEntry); } }