public void DedupeEntries_WhenGroupsHaveOverlappingAssetEntries_RemovesEntries()
        {
            const string          guid    = "0000";
            const string          address = "not/a/real/address";
            AddressableAssetGroup group1  = m_Settings.CreateGroup("group1", false, false, true, null, new Type[] { });
            AddressableAssetGroup group2  = m_Settings.CreateGroup("group2", false, false, true, null, new Type[] { });

            //We're making 2 identical enteries.  This is to simulate each group having it's own copy of an AA Entry that references the same object.
            //If we use the same object the call to AddAssetEntry won't give us the state we're looking for.
            AddressableAssetEntry entry  = new AddressableAssetEntry(guid, address, group1, false);
            AddressableAssetEntry entry2 = new AddressableAssetEntry(guid, address, group2, false);

            group1.AddAssetEntry(entry);
            group2.AddAssetEntry(entry2);

            //Ensuring our setup is correct
            Assert.IsNotNull(group1.GetAssetEntry(guid));
            Assert.IsNotNull(group2.GetAssetEntry(guid));

            group1.DedupeEnteries(); //We setup our entry with group1 so it should retain its reference
            group2.DedupeEnteries(); //The entry was added to group2 afterwards and should lose its reference

            Assert.IsNotNull(group1.GetAssetEntry(guid));
            Assert.IsNull(group2.GetAssetEntry(guid));

            //Cleanup
            m_Settings.RemoveGroup(group1);
            m_Settings.RemoveGroup(group2);
        }
Exemple #2
0
        internal static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
        {
            var  aa       = AddressableAssetSettingsDefaultObject.Settings;
            bool modified = false;

            foreach (string str in importedAssets)
            {
                var assetType = AssetDatabase.GetMainAssetTypeAtPath(str);

                if (typeof(AddressableAssetGroup).IsAssignableFrom(assetType))
                {
                    AddressableAssetGroup group = aa.FindGroup(Path.GetFileNameWithoutExtension(str));
                    if (group == null)
                    {
                        var foundGroup = AssetDatabase.LoadAssetAtPath <AddressableAssetGroup>(str);
                        if (!aa.groups.Contains(foundGroup))
                        {
                            aa.groups.Add(foundGroup);
                            group    = aa.FindGroup(Path.GetFileNameWithoutExtension(str));
                            modified = true;
                        }
                    }
                    if (group != null)
                    {
                        group.DedupeEnteries();
                    }
                }

                if (typeof(AddressableAssetEntryCollection).IsAssignableFrom(assetType))
                {
                    aa.CreateOrMoveEntry(AssetDatabase.AssetPathToGUID(str), aa.DefaultGroup);
                    modified = true;
                }

                var guid = AssetDatabase.AssetPathToGUID(str);
                if (aa.FindAssetEntry(guid) != null)
                {
                    modified = true;
                }

                if (AddressableAssetUtility.IsInResources(str))
                {
                    modified = true;
                }
            }

            if (deletedAssets.Length > 0)
            {
                // if any directly referenced assets were deleted while Unity was closed, the path isn't useful, so Remove(null) is our only option
                //  this can lead to orphaned schema files.
                if (aa.groups.Remove(null) ||
                    aa.DataBuilders.Remove(null) ||
                    aa.GroupTemplateObjects.Remove(null) ||
                    aa.InitializationObjects.Remove(null))
                {
                    modified = true;
                }
            }

            foreach (string str in deletedAssets)
            {
                if (AddressableAssetUtility.IsInResources(str))
                {
                    modified = true;
                }
                else
                {
                    if (aa.CheckForGroupDataDeletion(str))
                    {
                        modified = true;
                        continue;
                    }

                    // アセットが削除された時にグループを更新しない
                    //var guidOfDeletedAsset = AssetDatabase.AssetPathToGUID(str);
                    //if (aa.RemoveAssetEntry(guidOfDeletedAsset))
                    //{
                    //    modified = true;
                    //}
                }
            }
            for (int i = 0; i < movedAssets.Length; i++)
            {
                var str       = movedAssets[i];
                var assetType = AssetDatabase.GetMainAssetTypeAtPath(str);
                if (typeof(AddressableAssetGroup).IsAssignableFrom(assetType))
                {
                    var oldGroupName = Path.GetFileNameWithoutExtension(movedFromAssetPaths[i]);
                    var group        = aa.FindGroup(oldGroupName);
                    if (group != null)
                    {
                        var newGroupName = Path.GetFileNameWithoutExtension(str);
                        group.Name = newGroupName;
                    }
                }
                else
                {
                    var guid = AssetDatabase.AssetPathToGUID(str);
                    AddressableAssetEntry entry = aa.FindAssetEntry(guid);

                    bool isAlreadyAddressable = entry != null;
                    bool startedInResources   = AddressableAssetUtility.IsInResources(movedFromAssetPaths[i]);
                    bool endedInResources     = AddressableAssetUtility.IsInResources(str);
                    bool inEditorSceneList    = BuiltinSceneCache.Contains(new GUID(guid));

                    //update entry cached path
                    entry?.SetCachedPath(str);

                    //move to Resources
                    if (isAlreadyAddressable && endedInResources)
                    {
                        var fileName = Path.GetFileNameWithoutExtension(str);
                        Addressables.Log("You have moved addressable asset " + fileName + " into a Resources directory.  It has been unmarked as addressable, but can still be loaded via the Addressables API via its Resources path.");
                        aa.RemoveAssetEntry(guid, false);
                    }
                    else if (inEditorSceneList)
                    {
                        BuiltinSceneCache.ClearState();
                    }

                    //any addressables move or resources move (even resources to within resources) needs to refresh the UI.
                    modified = isAlreadyAddressable || startedInResources || endedInResources || inEditorSceneList;
                }
            }

            if (modified)
            {
                aa.SetDirty(AddressableAssetSettings.ModificationEvent.BatchModification, null, true, true);
            }
        }