예제 #1
0
    public static void GenerateAssetTable()
    {
        BuildAssets();

        string[] assetBundleNames = AssetDatabase.GetAllAssetBundleNames();

        AssetTable assetTable = new AssetTable();

        foreach (string assetBundleName in assetBundleNames)
        {
            AssetBundleInfo assetBundleInfo = new AssetBundleInfo();
            assetBundleInfo.AssetBundleName = assetBundleName;

            string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName);
            foreach (string assetPath in assetPaths)
            {
                AssetInfo assetInfo = new AssetInfo();
                assetInfo.AssetName            = GetFileName(assetPath);
                assetInfo.OwnerAssetBundleName = assetBundleName;

                assetBundleInfo.AssetInfos.Add(assetInfo);
            }

            assetTable.AssetBundleInfos.Add(assetBundleInfo);
        }

        Chenlin.SerializeUtil.XmlHelper.SerializeToFile(assetTable, CreateAssetBundlePathIfNotExist() + "AssetTable.xml");
        AssetDatabase.Refresh();
    }
        /// <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);
            }
        }
예제 #3
0
    public static IEnumerable Load(string path)
    {
        WWW www = new WWW(path);

        yield return(www);

        if (www.error != null)
        {
            Debug.LogError(www.error);
        }
        else
        {
            instance = Chenlin.SerializeUtil.XmlHelper.Deserialize <AssetTable>(www.text);
        }

        www.Dispose();

        if (instance == null)
        {
            Debug.LogError("AssetTable 读取失败");
        }
    }
예제 #4
0
        /// <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;
            }

            var undoGroup = Undo.GetCurrentGroup();

            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)
                {
                    return;
                }

                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)
            {
                var group = LocalizationEditorSettings.Instance.GetGroup(aaSettings, FormatAssetTableCollectionName(table.LocaleIdentifier), true, createUndo);

                if (createUndo)
                {
                    Undo.RecordObject(group, "Add asset to table");
                }

                entry = aaSettings.CreateOrMoveEntry(assetGuid, group, true);
                entry.SetLabel(entryLabel, true);
                entry.address = LocalizationEditorSettings.Instance.FindUniqueAssetAddress(asset.name);
            }
            else
            {
                Undo.RecordObject(entry.parentGroup, "Add asset to table");
                entry.SetLabel(entryLabel, true);
                UpdateAssetGroup(aaSettings, entry, createUndo);
            }

            // Update the table
            if (createUndo)
            {
                Undo.RecordObject(table, "Add asset to table");
                Undo.RecordObject(table.SharedData, "Add asset to table");
            }
            //else // Asset changes are not being saved correctly at the moment when using Undo. (LOC-82)
            {
                EditorUtility.SetDirty(table);
                EditorUtility.SetDirty(table.SharedData);
            }

            tableEntry = table.AddEntryFromReference(entryReference, assetGuid);

            // Update type metadata
            AssetTypeMetadata entryMetadata = null;
            AssetTypeMetadata typeMetadata  = null;
            var assetType = asset.GetType();

            // 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))
                    {
                        if (!at.Type.IsAssignableFrom(assetType))
                        {
                            tableEntry.RemoveSharedMetadata(at);

                            // Are other tables still using the type for the same id?
                            if (at.Contains(tableEntry.KeyId))
                            {
                                var name = SharedData.GetEntry(tableEntry.KeyId);
                                Debug.LogWarning($"Table entry {name}({tableEntry.KeyId}) contains mixed types. Both {at.Type} and {assetType} are used.");
                            }
                        }
                        else
                        {
                            entryMetadata = at;
                            break;
                        }
                    }

                    if (at.Type == assetType)
                    {
                        typeMetadata = at;
                        break;
                    }
                }
            }
            var foundMetadata = entryMetadata ?? typeMetadata;

            if (foundMetadata == null)
            {
                foundMetadata = new AssetTypeMetadata()
                {
                    Type = assetType
                };
            }
            tableEntry.AddSharedMetadata(foundMetadata);

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

            LocalizationEditorSettings.EditorEvents.RaiseAssetTableEntryAdded(this, table, tableEntry);
        }
예제 #5
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);
        }
예제 #6
0
    public static List <Res> SharedLoadedReses = new List <Res>();//Resource里的资源和AssetBundle的资源重名的话,还没有做区分

    public static void Init()
    {
        AssetTable.Load();
    }
예제 #7
0
 internal virtual void RaiseAssetTableEntryRemoved(AssetTableCollection collection, AssetTable table, AssetTableEntry entry, string assetGuid) => AssetTableEntryRemoved?.Invoke(collection, table, entry, assetGuid);
예제 #8
0
 internal virtual void RaiseAssetTableEntryAdded(AssetTableCollection collection, AssetTable table, AssetTableEntry entry) => AssetTableEntryAdded?.Invoke(collection, table, entry);
        /// <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);
            }
        }