Example #1
0
        public void SetAsset(TObject asset, AddressableAssetTableT <TObject> table)
        {
            var oldAsset = GetAsset(table);

            if (oldAsset != null)
            {
                LocalizationAddressableSettings.RemoveAssetFromTable(table, Key, asset);
            }

            if (asset != null)
            {
                LocalizationAddressableSettings.AddAssetToTable(table, Key, asset as TObject);
            }

            // Update cache
            var foundIndex = m_Assets.FindIndex(o => o.Key == table.LocaleIdentifier.Code);

            if (foundIndex == -1)
            {
                m_Assets.Add(new KeyValuePair <string, TObject>(table.LocaleIdentifier.Code, asset));
            }
            else
            {
                m_Assets[foundIndex] = new KeyValuePair <string, TObject>(table.LocaleIdentifier.Code, asset);
            }
        }
Example #2
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);
                }
            }
        }
Example #3
0
        public TObject GetAsset(AddressableAssetTableT <TObject> table)
        {
            var foundIndex = m_Assets.FindIndex(o => o.Key == table.LocaleIdentifier.Code);

            if (foundIndex == -1)
            {
                var     guid  = table.GetGuidFromKey(Key);
                TObject asset = null;
                if (!string.IsNullOrEmpty(guid))
                {
                    asset = GetAssetFromCache(guid);
                    m_Assets.Add(new KeyValuePair <string, TObject>(table.LocaleIdentifier.Code, asset));
                }
                return(asset);
            }
            return(m_Assets[foundIndex].Value);
        }
Example #4
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);
        }
Example #5
0
        protected override void DrawItemField(Rect cellRect, int col, LocalizedAssetTableListViewItem <T> item, AddressableAssetTableT <T> table)
        {
            EditorGUI.BeginChangeCheck();

            if (m_RowSize != RowPreviewSize.Single)
            {
                cellRect.width = rowHeight;
            }

            var asset    = item.GetAsset(table);
            var newAsset = EditorGUI.ObjectField(cellRect, asset, typeof(T), false);

            if (EditorGUI.EndChangeCheck())
            {
                item.SetAsset(newAsset as T, table);
                item.UpdateSearchString(Tables);
            }
        }
Example #6
0
 /// <summary>
 /// Remove the asset mapping from the table and also cleanup the Addressables if necessary.
 /// </summary>
 public static void RemoveAssetFromTable <TObject>(AddressableAssetTableT <TObject> table, string key, TObject asset) where TObject : Object
 {
     Instance.RemoveAssetFromTableInternal(table, key, asset);
 }