예제 #1
0
        protected virtual void RemoveAssetFromTableInternal <TObject>(AddressableAssetTableT <TObject> table, string key, TObject asset) where TObject : Object
        {
            // Clear the asset but keep the key
            table.AddAsset(key, string.Empty);

            var aaSettings = AddressableAssetSettingsDefaultObject.Settings;

            if (aaSettings == null)
            {
                return;
            }

            // Determine if the asset is being referenced by any other 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 assetGuid  = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(asset));
            var tableGuid  = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(table));
            var tableGroup = GetGroup(aaSettings, AssetTableGroupName);

            if (tableGroup != null)
            {
                foreach (var tableEntry in tableGroup.entries)
                {
                    var tableToCheck = tableEntry.guid == tableGuid ? table : AssetDatabase.LoadAssetAtPath <AddressableAssetTableT <TObject> >(AssetDatabase.GUIDToAssetPath(tableEntry.guid));
                    if (tableToCheck != null && tableToCheck.LocaleIdentifier == table.LocaleIdentifier)
                    {
                        var guidHash = Hash128.Parse(assetGuid);
                        foreach (var item in tableToCheck.AssetMap.Values)
                        {
                            // The asset is referenced elsewhere so we can not remove the label or asset.
                            if (item.GuidHash == guidHash)
                            {
                                return;
                            }
                        }
                    }
                }
            }

            // Remove the current locale
            var assetEntry = aaSettings.FindAssetEntry(assetGuid);

            if (assetEntry != null)
            {
                aaSettings.AddLabel(table.LocaleIdentifier.Code);
                assetEntry.SetLabel(table.LocaleIdentifier.Code, false);

                // No other references so safe to remove.
                if (assetEntry.labels.Count <= 1) // 1 for the Asset table label
                {
                    aaSettings.RemoveAssetEntry(assetEntry.guid);
                }
            }
        }
예제 #2
0
        protected virtual void AddAssetToTableInternal <TObject>(AddressableAssetTableT <TObject> table, string key, TObject asset) where TObject : Object
        {
            if (!EditorUtility.IsPersistent(table))
            {
                Debug.LogError("Only persistent assets can be addressable. The asset needs to be saved on disk.");
                return;
            }

            // Add the asset to the addressables system and setup the table with the key to guid mapping.
            var aaSettings = AddressableAssetSettingsDefaultObject.GetSettings(true);

            if (aaSettings == null)
            {
                return;
            }

            // Has the asset already been added? Perhaps it is being used by multiple tables or the user has added it manually.
            var guid  = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(asset));
            var entry = aaSettings.FindAssetEntry(guid);

            if (entry == null)
            {
                var groupName = string.Format(AssetTableTypeGroupName, typeof(TObject).ToString());
                var group     = GetGroup(aaSettings, groupName, true);
                entry         = aaSettings.CreateOrMoveEntry(guid, group, true);
                entry.address = FindUniqueAssetAddress(asset.name);
            }

            // TODO: A better way? Can assets have dependencies?
            // We need a way to mark that the asset has a dependency on this table. So we add a label with the table guid.
            var tableGuid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(table));

            Debug.Assert(!string.IsNullOrEmpty(tableGuid));

            aaSettings.AddLabel(table.LocaleIdentifier.Code);
            entry.SetLabel(table.LocaleIdentifier.Code, true);
            table.AddAsset(key, guid);
        }