Пример #1
0
        static void SetAaEntry(AddressableAssetSettings aaSettings, Object[] targets, bool create)
        {
            if (create && aaSettings.DefaultGroup.ReadOnly)
            {
                Debug.LogError("Current default group is ReadOnly.  Cannot add addressable assets to it");
                return;
            }

            Undo.RecordObject(aaSettings, "AddressableAssetSettings");

            var targetInfos = new List <TargetInfo>();

            foreach (var t in targets)
            {
                if (AddressableAssetUtility.GetPathAndGUIDFromTarget(t, out var path, out var guid, out var mainAssetType))
                {
                    targetInfos.Add(new TargetInfo()
                    {
                        Guid = guid, Path = path, MainAssetType = mainAssetType
                    });
                }
            }

            if (!create)
            {
                targetInfos.ForEach(ti =>
                {
                    AddressableAssetGroup group = aaSettings.FindAssetEntry(ti.Guid).parentGroup;
                    aaSettings.RemoveAssetEntry(ti.Guid);
                    AddressableAssetUtility.OpenAssetIfUsingVCIntegration(group);
                });
            }
            else
            {
                var resourceTargets = targetInfos.Where(ti => AddressableAssetUtility.IsInResources(ti.Path));
                var resourcePaths   = resourceTargets.Select(t => t.Path).ToList();
                var resourceGuids   = resourceTargets.Select(t => t.Guid).ToList();
                AddressableAssetUtility.SafeMoveResourcesToGroup(aaSettings, aaSettings.DefaultGroup, resourcePaths, resourceGuids);

                var entriesAdded     = new List <AddressableAssetEntry>();
                var modifiedGroups   = new HashSet <AddressableAssetGroup>();
                var otherTargetInfos = targetInfos.Except(resourceTargets);
                foreach (var info in otherTargetInfos)
                {
                    var e = aaSettings.CreateOrMoveEntry(info.Guid, aaSettings.DefaultGroup, false, false);
                    entriesAdded.Add(e);
                    modifiedGroups.Add(e.parentGroup);
                }

                foreach (var g in modifiedGroups)
                {
                    g.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entriesAdded, false, true);
                    AddressableAssetUtility.OpenAssetIfUsingVCIntegration(g);
                }

                aaSettings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entriesAdded, true, false);
            }
        }
Пример #2
0
        static void SetAaEntry(AddressableAssetSettings aaSettings, Object[] targets, bool create)
        {
            if (create && aaSettings.DefaultGroup.ReadOnly)
            {
                Debug.LogError("Current default group is ReadOnly.  Cannot add addressable assets to it");
                return;
            }

            Undo.RecordObject(aaSettings, "AddressableAssetSettings");
            string path;
            var    guid = string.Empty;
            //if (create || EditorUtility.DisplayDialog("Remove Addressable Asset Entries", "Do you want to remove Addressable Asset entries for " + targets.Length + " items?", "Yes", "Cancel"))
            {
                var entriesAdded   = new List <AddressableAssetEntry>();
                var modifiedGroups = new HashSet <AddressableAssetGroup>();

                Type mainAssetType;
                foreach (var t in targets)
                {
                    if (AddressableAssetUtility.GetPathAndGUIDFromTarget(t, out path, ref guid, out mainAssetType))
                    {
                        if (create)
                        {
                            if (AddressableAssetUtility.IsInResources(path))
                            {
                                AddressableAssetUtility.SafeMoveResourcesToGroup(aaSettings, aaSettings.DefaultGroup, new List <string> {
                                    path
                                });
                            }
                            else
                            {
                                var e = aaSettings.CreateOrMoveEntry(guid, aaSettings.DefaultGroup, false, false);
                                entriesAdded.Add(e);
                                modifiedGroups.Add(e.parentGroup);
                            }
                        }
                        else
                        {
                            aaSettings.RemoveAssetEntry(guid);
                        }
                    }
                }

                if (create)
                {
                    foreach (var g in modifiedGroups)
                    {
                        g.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entriesAdded, false, true);
                    }
                    aaSettings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entriesAdded, true, false);
                }
            }
        }
 bool SafeMoveResourcesToGroup(AddressableAssetGroup targetGroup, List<AssetEntryTreeViewItem> itemList)
 {
     var guids = new List<string>();
     var paths = new List<string>();
     foreach (AssetEntryTreeViewItem child in itemList)
     {
         if (child != null)
         {
             guids.Add(child.entry.guid);
             paths.Add(child.entry.AssetPath);
         }
     }
     return AddressableAssetUtility.SafeMoveResourcesToGroup(m_Editor.settings, targetGroup, paths, guids);
 }
        public void SafeMoveResourcesToGroup_ResourcesMovedToNewFolderAndGroup()
        {
            var folderPath = AssetDatabase.GUIDToAssetPath(AssetDatabase.CreateFolder(TestFolder, "Resources"));
            var g1         = CreateTestPrefabAsset(folderPath + "/p1.prefab", "p1");
            var g2         = CreateTestPrefabAsset(folderPath + "/p2.prefab", "p2");

            Assert.AreEqual(0, Settings.DefaultGroup.entries.Count);
            var result = AddressableAssetUtility.SafeMoveResourcesToGroup(Settings, Settings.DefaultGroup, new List <string> {
                AssetDatabase.GUIDToAssetPath(g1), AssetDatabase.GUIDToAssetPath(g2)
            }, null, false);

            Assert.IsTrue(result);
            Assert.AreEqual(2, Settings.DefaultGroup.entries.Count);
            var ap = $"{TestFolder}_Resources_moved";

            Assert.IsTrue(AssetDatabase.IsValidFolder($"{TestFolder}/Resources_moved"));
        }
Пример #5
0
        static void SetAaEntry(AddressableAssetSettings aaSettings, Object[] targets, bool create)
        {
            Undo.RecordObject(aaSettings, "AddressableAssetSettings");
            string path;
            var    guid = string.Empty;
            //if (create || EditorUtility.DisplayDialog("Remove Addressable Asset Entries", "Do you want to remove Addressable Asset entries for " + targets.Length + " items?", "Yes", "Cancel"))
            {
                var entriesAdded = new List <AddressableAssetEntry>();

                foreach (var t in targets)
                {
                    if (AddressableAssetUtility.GetPathAndGUIDFromTarget(t, out path, ref guid))
                    {
                        if (create)
                        {
                            if (AddressableAssetUtility.IsInResources(path))
                            {
                                AddressableAssetUtility.SafeMoveResourcesToGroup(aaSettings, aaSettings.DefaultGroup, new List <string> {
                                    path
                                });
                            }
                            else
                            {
                                entriesAdded.Add(aaSettings.CreateOrMoveEntry(guid, aaSettings.DefaultGroup, false, false));
                            }
                        }
                        else
                        {
                            aaSettings.RemoveAssetEntry(guid);
                        }
                    }
                }

                if (create)
                {
                    aaSettings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entriesAdded, true);
                }
            }
        }
        static void SetAaEntry(AddressableAssetSettings aaSettings, List <TargetInfo> targetInfos, bool create)
        {
            if (create && aaSettings.DefaultGroup.ReadOnly)
            {
                Debug.LogError("Current default group is ReadOnly.  Cannot add addressable assets to it");
                return;
            }

            Undo.RecordObject(aaSettings, "AddressableAssetSettings");

            if (!create)
            {
                List <AddressableAssetEntry> removedEntries = new List <AddressableAssetEntry>(targetInfos.Count);
                for (int i = 0; i < targetInfos.Count; ++i)
                {
                    AddressableAssetEntry e = aaSettings.FindAssetEntry(targetInfos[i].Guid);
                    AddressableAssetUtility.OpenAssetIfUsingVCIntegration(e.parentGroup);
                    removedEntries.Add(e);
                    aaSettings.RemoveAssetEntry(removedEntries[i], false);
                }
                if (removedEntries.Count > 0)
                {
                    aaSettings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryRemoved, removedEntries, true, false);
                }
            }
            else
            {
                AddressableAssetGroup parentGroup = aaSettings.DefaultGroup;
                var resourceTargets = targetInfos.Where(ti => AddressableAssetUtility.IsInResources(ti.Path));
                if (resourceTargets.Any())
                {
                    var resourcePaths = resourceTargets.Select(t => t.Path).ToList();
                    var resourceGuids = resourceTargets.Select(t => t.Guid).ToList();
                    AddressableAssetUtility.SafeMoveResourcesToGroup(aaSettings, parentGroup, resourcePaths, resourceGuids);
                }

                var           otherTargetInfos = targetInfos.Except(resourceTargets);
                List <string> otherTargetGuids = new List <string>(targetInfos.Count);
                foreach (var info in otherTargetInfos)
                {
                    otherTargetGuids.Add(info.Guid);
                }

                var entriesCreated = new List <AddressableAssetEntry>();
                var entriesMoved   = new List <AddressableAssetEntry>();
                aaSettings.CreateOrMoveEntries(otherTargetGuids, parentGroup, entriesCreated, entriesMoved, false, false);

                bool openedInVC = false;
                if (entriesMoved.Count > 0)
                {
                    AddressableAssetUtility.OpenAssetIfUsingVCIntegration(parentGroup);
                    openedInVC = true;
                    aaSettings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entriesMoved, true, false);
                }

                if (entriesCreated.Count > 0)
                {
                    if (!openedInVC)
                    {
                        AddressableAssetUtility.OpenAssetIfUsingVCIntegration(parentGroup);
                    }
                    aaSettings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryAdded, entriesCreated, true, false);
                }
            }
        }
Пример #7
0
        protected override DragAndDropVisualMode HandleDragAndDrop(DragAndDropArgs args)
        {
            DragAndDropVisualMode visualMode = DragAndDropVisualMode.None;

            var target = args.parentItem as AssetEntryTreeViewItem;

            if (target == null)
            {
                return(DragAndDropVisualMode.None);
            }

            if (target.entry != null && target.entry.ReadOnly)
            {
                return(DragAndDropVisualMode.None);
            }

            if (target.group != null && target.group.ReadOnly)
            {
                return(DragAndDropVisualMode.None);
            }


            if (DragAndDrop.paths != null && DragAndDrop.paths.Length > 0)
            {
                if (!AddressableAssetUtility.IsPathValidForEntry(DragAndDrop.paths[0]))
                {
                    visualMode = DragAndDropVisualMode.Rejected;
                }
                else
                {
                    visualMode = DragAndDropVisualMode.Copy;
                }

                if (args.performDrop && visualMode != DragAndDropVisualMode.Rejected)
                {
                    AddressableAssetGroup parent = null;
                    if (target.group != null)
                    {
                        parent = target.group;
                    }
                    else if (target.entry != null)
                    {
                        parent = target.entry.parentGroup;
                    }

                    if (parent != null)
                    {
                        var resourcePaths    = new List <string>();
                        var nonResourcePaths = new List <string>();
                        foreach (var p in DragAndDrop.paths)
                        {
                            if (AddressableAssetUtility.IsInResources(p))
                            {
                                resourcePaths.Add(p);
                            }
                            else
                            {
                                nonResourcePaths.Add(p);
                            }
                        }
                        bool canMarkNonResources = true;
                        if (resourcePaths.Count > 0)
                        {
                            canMarkNonResources = AddressableAssetUtility.SafeMoveResourcesToGroup(m_Editor.settings, parent, resourcePaths);
                        }
                        if (canMarkNonResources)
                        {
                            var entries = new List <AddressableAssetEntry>();
                            foreach (var p in nonResourcePaths)
                            {
                                entries.Add(m_Editor.settings.CreateOrMoveEntry(AssetDatabase.AssetPathToGUID(p), parent, false, false));
                            }
                            m_Editor.settings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entries, true);
                        }
                    }
                }
            }
            else
            {
                var draggedNodes = DragAndDrop.GetGenericData("AssetEntryTreeViewItem") as List <AssetEntryTreeViewItem>;
                if (draggedNodes != null && draggedNodes.Count > 0)
                {
                    visualMode = DragAndDropVisualMode.Copy;
                    if (args.performDrop)
                    {
                        AddressableAssetGroup parent = null;
                        if (target.group != null)
                        {
                            parent = target.group;
                        }
                        else if (target.entry != null)
                        {
                            parent = target.entry.parentGroup;
                        }

                        if (parent != null)
                        {
                            if (draggedNodes.First().entry.IsInResources)
                            {
                                SafeMoveResourcesToGroup(parent, draggedNodes);
                            }
                            else
                            {
                                var entries = new List <AddressableAssetEntry>();
                                foreach (var node in draggedNodes)
                                {
                                    entries.Add(m_Editor.settings.CreateOrMoveEntry(node.entry.guid, parent, false, false));
                                }
                                m_Editor.settings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entries, true);
                            }
                        }
                    }
                }
            }

            return(visualMode);
        }
 public void SafeMoveResourcesToGroup_WithInvalidParameters_Fails()
 {
     Assert.IsFalse(AddressableAssetUtility.SafeMoveResourcesToGroup(Settings, null, null, null, false));
     Assert.IsFalse(AddressableAssetUtility.SafeMoveResourcesToGroup(Settings, Settings.DefaultGroup, null, null, false));
 }
        DragAndDropVisualMode HandleDragAndDropPaths(AssetEntryTreeViewItem target, DragAndDropArgs args)
        {
            DragAndDropVisualMode visualMode = DragAndDropVisualMode.None;
            
            if (target == null)
                return DragAndDropVisualMode.Rejected;

            foreach (String path in DragAndDrop.paths)
            {
                if (!AddressableAssetUtility.IsPathValidForEntry(path))
                    return DragAndDropVisualMode.Rejected;
            }
            visualMode = DragAndDropVisualMode.Copy;

            if (args.performDrop && visualMode != DragAndDropVisualMode.Rejected)
            {
                AddressableAssetGroup parent = null;
                bool targetIsGroup = false;
                if (target.group != null)
                {
                    parent = target.group;
                    targetIsGroup = true;
                }
                else if (target.entry != null)
                    parent = target.entry.parentGroup;

                if (parent != null)
                {
                    var resourcePaths = new List<string>();
                    var nonResourcePaths = new List<string>();
                    foreach (var p in DragAndDrop.paths)
                    {
                        if (AddressableAssetUtility.IsInResources(p))
                            resourcePaths.Add(p);
                        else
                            nonResourcePaths.Add(p);
                    }
                    bool canMarkNonResources = true;
                    if (resourcePaths.Count > 0)
                    {
                        canMarkNonResources = AddressableAssetUtility.SafeMoveResourcesToGroup(m_Editor.settings, parent, resourcePaths);
                    }
                    if (canMarkNonResources)
                    {
                        var entries = new List<AddressableAssetEntry>();
                        var modifiedGroups = new HashSet<AddressableAssetGroup>();
                        foreach (var p in nonResourcePaths)
                        {
                            var e = m_Editor.settings.CreateOrMoveEntry(AssetDatabase.AssetPathToGUID(p), parent, false, false);
                            entries.Add(e);
                            modifiedGroups.Add(e.parentGroup);
                        }
                        foreach (var g in modifiedGroups)
                            g.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entries, false, true);
                        m_Editor.settings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entries, true, false);

                        if (targetIsGroup)
                        {
                            SetExpanded(target.id, true);
                        }
                    }
                }
            }

            return visualMode;
        }
        static void SetAaEntry(AddressableAssetSettings aaSettings, List <TargetInfo> targetInfos, bool create)
        {
            /*
             * if (create && aaSettings.DefaultGroup.ReadOnly)
             * {
             *  Debug.LogError("Current default group is ReadOnly.  Cannot add addressable assets to it");
             *  return;
             * }
             */

            Undo.RecordObject(aaSettings, "AddressableAssetSettings");

            if (!create)
            {
                List <AddressableAssetEntry> removedEntries = new List <AddressableAssetEntry>(targetInfos.Count);
                for (int i = 0; i < targetInfos.Count; ++i)
                {
                    AddressableAssetEntry e = aaSettings.FindAssetEntry(targetInfos[i].Guid);
                    AddressableAssetUtility.OpenAssetIfUsingVCIntegration(e.parentGroup);
                    removedEntries.Add(e);
                    aaSettings.RemoveAssetEntry(removedEntries[i], false);
                }
                if (removedEntries.Count > 0)
                {
                    aaSettings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryRemoved, removedEntries, true, false);
                }
            }
            else
            {
                AddressableAssetGroup parentGroup = aaSettings.DefaultGroup;
                var resourceTargets = targetInfos.Where(ti => AddressableAssetUtility.IsInResources(ti.Path));
                if (resourceTargets.Any())
                {
                    var resourcePaths = resourceTargets.Select(t => t.Path).ToList();
                    var resourceGuids = resourceTargets.Select(t => t.Guid).ToList();
                    AddressableAssetUtility.SafeMoveResourcesToGroup(aaSettings, parentGroup, resourcePaths, resourceGuids);
                }

                var entriesCreated   = new List <AddressableAssetEntry>();
                var entriesMoved     = new List <AddressableAssetEntry>();
                var otherTargetInfos = targetInfos.Except(resourceTargets);
                foreach (var info in otherTargetInfos)
                {
                    var hook = aaSettings.hook;
                    AddressableAssetGroup assetGroup = default;
                    string customAddress             = default;
                    if (hook != null)
                    {
                        hook.BeforeSetEntryOnInspectorGUI(info.Path, out assetGroup, out customAddress);
                    }
                    if (assetGroup == null)
                    {
                        assetGroup = aaSettings.DefaultGroup;
                    }

                    string guid = info.Guid;
                    if (string.IsNullOrEmpty(guid))
                    {
                        continue;
                    }

                    AddressableAssetEntry e = aaSettings.FindAssetEntry(guid);
                    if (e != null) //move entry to where it should go...
                    {
                        aaSettings.MoveEntry(e, assetGroup, false, false);
                    }
                    else //create entry
                    {
                        e = aaSettings.CreateAndAddEntryToGroup_Custom(guid, assetGroup, false, false);
                    }

                    if (string.IsNullOrEmpty(customAddress) == false)
                    {
                        e.SetAddress(customAddress, false);
                    }
                    entriesCreated.Add(e);
                    entriesMoved.Add(e);
                }

                bool openedInVC = false;
                if (entriesMoved.Count > 0)
                {
                    AddressableAssetUtility.OpenAssetIfUsingVCIntegration(parentGroup);
                    openedInVC = true;
                    aaSettings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entriesMoved, true, false);
                }

                if (entriesCreated.Count > 0)
                {
                    if (!openedInVC)
                    {
                        AddressableAssetUtility.OpenAssetIfUsingVCIntegration(parentGroup);
                    }
                    aaSettings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryAdded, entriesCreated, true, false);
                }
            }
        }
Пример #11
0
        static void SetAaEntry(AddressableAssetSettings aaSettings, Object[] targets, bool create)
        {
            /*
             * if (create && aaSettings.DefaultGroup.ReadOnly)
             * {
             *  Debug.LogError("Current default group is ReadOnly.  Cannot add addressable assets to it");
             *  return;
             * }
             */

            Undo.RecordObject(aaSettings, "AddressableAssetSettings");
            string path;
            var    guid = string.Empty;
            //if (create || EditorUtility.DisplayDialog("Remove Addressable Asset Entries", "Do you want to remove Addressable Asset entries for " + targets.Length + " items?", "Yes", "Cancel"))
            {
                var  entriesAdded   = new List <AddressableAssetEntry>();
                var  modifiedGroups = new HashSet <AddressableAssetGroup>();
                Type mainAssetType;
                foreach (var t in targets)
                {
                    if (AddressableAssetUtility.GetPathAndGUIDFromTarget(t, out path, ref guid, out mainAssetType))
                    {
                        if (create)
                        {
                            var ruleSet = aaSettings.assetRegisterRuleSet;
                            if (!ruleSet.TryResolveRule(path, out var rule))
                            {
                                rule = new AddressableAssetRegisterRuleSet.Rule {
                                    AssetGroup = aaSettings.DefaultGroup
                                }
                            }
                            ;
                            var assetGroup = rule.AssetGroup;

                            if (AddressableAssetUtility.IsInResources(path))
                            {
                                AddressableAssetUtility.SafeMoveResourcesToGroup(aaSettings, assetGroup, new List <string> {
                                    path
                                });
                            }
                            else
                            {
                                var e = aaSettings.CreateOrMoveEntry(guid, assetGroup, false, false);
                                if (rule.SimplifyAddress)
                                {
                                    e.SetAddress(System.IO.Path.GetFileNameWithoutExtension(e.address), false);
                                }
                                entriesAdded.Add(e);
                                modifiedGroups.Add(e.parentGroup);
                            }
                        }
                        else
                        {
                            aaSettings.RemoveAssetEntry(guid);
                        }
                    }
                }

                if (create)
                {
                    foreach (var g in modifiedGroups)
                    {
                        g.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entriesAdded, false, true);
                    }
                    aaSettings.SetDirty(AddressableAssetSettings.ModificationEvent.EntryMoved, entriesAdded, true, false);
                }
            }
        }