/// <summary>
        /// Remove the asset mapping from the table entry and also cleans up the Addressables if necessary.
        /// </summary>
        /// <param name="table"></param>
        /// <param name="entryReference"></param>
        /// <param name="createUndo"></param>
        public void RemoveAssetFromTable(AssetTable table, TableEntryReference entryReference, bool createUndo = false)
        {
            using (new UndoScope("Remove asset from table", createUndo))
            {
                // Clear the asset but keep the key
                var tableEntry = table.GetEntryFromReference(entryReference);
                if (tableEntry == null)
                {
                    return;
                }

                var removedAssetGuid = tableEntry.Guid;
                tableEntry.Guid = string.Empty;

                var aaSettings = LocalizationEditorSettings.Instance.GetAddressableAssetSettings(false);
                if (aaSettings == null)
                {
                    return;
                }

                EditorUtility.SetDirty(table);
                EditorUtility.SetDirty(table.SharedData);

                RemoveEntryAssetType(tableEntry.KeyId, table.LocaleIdentifier.Code);

                // If the entry has metadata then we will leave an empty entry otherwise we just remove the whole thing.
                if (tableEntry.MetadataEntries.Count == 0)
                {
                    table.RemoveEntry(tableEntry.KeyId);
                }

                // Determine if the asset is being referenced by any entries or tables with the same locale, if not then we can
                // remove the locale label and if no other labels exist also remove the asset from the Addressables system.
                var assetTableCollections = LocalizationEditorSettings.GetAssetTableCollections();
                foreach (var collection in assetTableCollections)
                {
                    if (collection.GetTable(table.LocaleIdentifier) is AssetTable tableWithMatchingLocaleId && tableWithMatchingLocaleId.ContainsValue(removedAssetGuid))
                    {
                        // The asset is referenced elsewhere by a table with the same Locale so we can not remove the locale label or asset.
                        return;
                    }
                }

                // Remove the locale label for this asset
                var assetEntry = aaSettings.FindAssetEntry(removedAssetGuid);
                if (assetEntry != null)
                {
                    if (createUndo)
                    {
                        Undo.RecordObject(assetEntry.parentGroup, "Remove asset from table");
                    }

                    var assetLabel = AddressHelper.FormatAssetLabel(table.LocaleIdentifier);
                    assetEntry.SetLabel(assetLabel, false);
                    UpdateAssetGroup(aaSettings, assetEntry, createUndo);
                }

                LocalizationEditorSettings.EditorEvents.RaiseAssetTableEntryRemoved(this, table, tableEntry, removedAssetGuid);
            }
        }
示例#2
0
        /// <summary>
        /// Remove the asset mapping from the table entry and also cleans up the Addressables if necessary.
        /// </summary>
        /// <param name="table"></param>
        /// <param name="entryReference"></param>
        /// <param name="createUndo"></param>
        public void RemoveAssetFromTable(AssetTable table, TableEntryReference entryReference, bool createUndo = false)
        {
            var undoGroup = Undo.GetCurrentGroup();

            if (createUndo)
            {
                Undo.RecordObject(table, "Remove asset from table");            // We modify the table entry.
                Undo.RecordObject(table.SharedData, "Remove asset from table"); // We modify the shared table metadata.
            }
            //else // Asset changes are not being saved correctly at the moment when using Undo. (LOC-82)
            {
                EditorUtility.SetDirty(table);
                EditorUtility.SetDirty(table.SharedData);
            }

            // Clear the asset but keep the key
            var tableEntry = table.GetEntryFromReference(entryReference);

            if (tableEntry == null)
            {
                return;
            }

            var removedAssetGuid = tableEntry.Guid;

            tableEntry.Guid = string.Empty;

            var aaSettings = LocalizationEditorSettings.Instance.GetAddressableAssetSettings(false);

            if (aaSettings == null)
            {
                return;
            }

            // Update type metadata
            // We cant use a foreach here as we are sometimes inside of a loop and exceptions will be thrown (Collection was modified).
            for (int i = 0; i < table.SharedData.Metadata.MetadataEntries.Count; ++i)
            {
                var md = table.SharedData.Metadata.MetadataEntries[i];
                if (md is AssetTypeMetadata at)
                {
                    if (at.Contains(tableEntry.KeyId))
                    {
                        tableEntry.RemoveSharedMetadata(at);
                    }
                }
            }

            // If the entry has metadata then we will leave an empty entry otherwise we just remove the whole thing.
            if (tableEntry.MetadataEntries.Count == 0)
            {
                table.RemoveEntry(tableEntry.KeyId);
            }

            // Determine if the asset is being referenced by any entries or tables with the same locale, if not then we can
            // remove the locale label and if no other labels exist also remove the asset from the Addressables system.
            var assetTableCollections = LocalizationEditorSettings.GetAssetTableCollections();

            foreach (var collection in assetTableCollections)
            {
                var tableWithMatchingLocaleId = collection.GetTable(table.LocaleIdentifier) as AssetTable;
                if (tableWithMatchingLocaleId == null)
                {
                    continue;
                }

                if (tableWithMatchingLocaleId.ContainsValue(removedAssetGuid))
                {
                    // The asset is referenced elsewhere so we can not remove the label or asset.
                    return;
                }
            }

            // Remove the locale label for this asset
            var assetEntry = aaSettings.FindAssetEntry(removedAssetGuid);

            if (assetEntry != null)
            {
                if (createUndo)
                {
                    Undo.RecordObject(assetEntry.parentGroup, "Remove asset from table");
                }

                var assetLabel = AddressHelper.FormatAssetLabel(table.LocaleIdentifier);
                assetEntry.SetLabel(assetLabel, false);
                UpdateAssetGroup(aaSettings, assetEntry, createUndo);
            }

            if (createUndo)
            {
                Undo.CollapseUndoOperations(undoGroup);
            }

            LocalizationEditorSettings.EditorEvents.RaiseAssetTableEntryRemoved(this, table, tableEntry, removedAssetGuid);
        }