Exemplo n.º 1
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);
            }
        }