public void IsInResourcesHandlesResourcesInWrongContext()
 {
     Assert.IsFalse(AddressableAssetUtility.IsInResources("resources/"));
     Assert.IsFalse(AddressableAssetUtility.IsInResources("/resources"));
     Assert.IsFalse(AddressableAssetUtility.IsInResources("path/resourcesOther/path"));
     Assert.IsFalse(AddressableAssetUtility.IsInResources("/path/res/ources/path"));
 }
예제 #2
0
            protected override TreeViewItem BuildRoot()
            {
                var root = new TreeViewItem(-1, -1);

                var aaSettings = AddressableAssetSettingsDefaultObject.Settings;

                if (aaSettings == null)
                {
                    var message = "Use 'Window->Addressables' to initialize.";
                    root.AddChild(new AssetRefTreeViewItem(message.GetHashCode(), 0, message, string.Empty, string.Empty));
                }
                else
                {
                    if (!string.IsNullOrEmpty(m_NonAddressedAsset))
                    {
                        var item = new AssetRefTreeViewItem(m_NonAddressedAsset.GetHashCode(), 0, "Make Addressable - " + m_NonAddressedAsset, m_GUID, string.Empty);
                        item.icon = m_WarningIcon;
                        root.AddChild(item);
                    }
                    root.AddChild(new AssetRefTreeViewItem(AssetReferenceDrawer.noAssetString.GetHashCode(), 0, AssetReferenceDrawer.noAssetString, string.Empty, string.Empty));
                    var allAssets = new List <AddressableAssetEntry>();
                    aaSettings.GetAllAssets(allAssets, false);
                    foreach (var entry in allAssets)
                    {
                        if (!AddressableAssetUtility.IsInResources(entry.AssetPath) &&
                            m_Drawer.ValidateAsset(entry.AssetPath))
                        {
                            var child = new AssetRefTreeViewItem(entry.address.GetHashCode(), 0, entry.address, entry.guid, entry.AssetPath);
                            root.AddChild(child);
                        }
                    }
                }

                return(root);
            }
예제 #3
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);
            }
        }
예제 #4
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);
                }
            }
        }
예제 #5
0
        internal void DragAndDropNotFromAddressableGroupWindow(string path, string guid, SerializedProperty property, AddressableAssetSettings aaSettings)
        {
            if (AddressableAssetUtility.IsInResources(path))
            {
                Addressables.LogWarning("Cannot use an AssetReference on an asset in Resources. Move asset out of Resources first. ");
            }
            else if (!AddressableAssetUtility.IsPathValidForEntry(path))
            {
                Addressables.LogWarning("Dragged asset is not valid as an Asset Reference. " + path);
            }
            else
            {
                Object obj;
                if (DragAndDrop.objectReferences != null && DragAndDrop.objectReferences.Length == 1)
                {
                    obj = DragAndDrop.objectReferences[0];
                }
                else
                {
                    obj = AssetDatabase.LoadAssetAtPath <Object>(path);
                }

                if (AssetReferenceDrawerUtilities.SetObject(ref m_AssetRefObject, ref m_ReferencesSame, property, obj, fieldInfo, m_label.text, out guid))
                {
                    TriggerOnValidate(property);
                    aaSettings = AddressableAssetSettingsDefaultObject.GetSettings(true);
                    var entry = aaSettings.FindAssetEntry(guid);
                    if (entry == null && !string.IsNullOrEmpty(guid))
                    {
                        string assetName;
                        if (!aaSettings.IsAssetPathInAddressableDirectory(path, out assetName))
                        {
                            aaSettings.CreateOrMoveEntry(guid, aaSettings.DefaultGroup);
                            newGuid = guid;
                        }
                    }
                }
            }
        }
예제 #6
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);
                }
            }
        }
예제 #8
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (property == null || label == null)
            {
                Debug.LogError("Error rendering drawer for AssetReference property.");
                return;
            }
            string labelText = label.text;

            m_AssetRefObject = property.GetActualObjectForSerializedProperty <AssetReference>(fieldInfo, ref labelText);
            label.text       = labelText;
            if (m_AssetRefObject == null)
            {
                return;
            }

            EditorGUI.BeginProperty(position, label, property);

            GatherFilters(property);
            string guid       = m_AssetRefObject.AssetGUID;
            var    aaSettings = AddressableAssetSettingsDefaultObject.Settings;

            var checkToForceAddressable = string.Empty;

            if (!string.IsNullOrEmpty(newGuid) && newGuidPropertyPath == property.propertyPath)
            {
                if (newGuid == noAssetString)
                {
                    SetObject(property, null, out guid);
                    newGuid = string.Empty;
                }
                else
                {
                    if (SetObject(property, AssetDatabase.LoadAssetAtPath <Object>(AssetDatabase.GUIDToAssetPath(newGuid)), out guid))
                    {
                        checkToForceAddressable = newGuid;
                    }
                    newGuid = string.Empty;
                }
            }

            bool isNotAddressable = false;

            m_AssetName = noAssetString;
            Texture2D icon = null;

            if (aaSettings != null && !string.IsNullOrEmpty(guid))
            {
                var entry = aaSettings.FindAssetEntry(guid);
                if (entry != null)
                {
                    m_AssetName = entry.address;
                    icon        = AssetDatabase.GetCachedIcon(entry.AssetPath) as Texture2D;
                }
                else
                {
                    var path = AssetDatabase.GUIDToAssetPath(guid);
                    if (!string.IsNullOrEmpty(path))
                    {
                        var  dir       = Path.GetDirectoryName(path);
                        bool foundAddr = false;
                        while (!string.IsNullOrEmpty(dir))
                        {
                            var dirEntry = aaSettings.FindAssetEntry(AssetDatabase.AssetPathToGUID(dir));
                            if (dirEntry != null)
                            {
                                foundAddr   = true;
                                m_AssetName = dirEntry.address + path.Remove(0, dir.Length);
                                break;
                            }
                            dir = Path.GetDirectoryName(dir);
                        }

                        if (!foundAddr)
                        {
                            m_AssetName = path;
                            if (!string.IsNullOrEmpty(checkToForceAddressable))
                            {
                                var newEntry = aaSettings.CreateOrMoveEntry(guid, aaSettings.DefaultGroup);
                                Addressables.LogFormat("Created AddressableAsset {0} in group {1}.", newEntry.address, aaSettings.DefaultGroup.Name);
                            }
                            else
                            {
                                if (!File.Exists(path))
                                {
                                    m_AssetName = "Missing File!";
                                }
                                else
                                {
                                    isNotAddressable = true;
                                }
                            }
                        }
                        icon = AssetDatabase.GetCachedIcon(path) as Texture2D;
                    }
                    else
                    {
                        m_AssetName = "Missing File!";
                    }
                }
            }

            assetDropDownRect = EditorGUI.PrefixLabel(position, label);
            var nameToUse = m_AssetName;

            if (isNotAddressable)
            {
                nameToUse = "Not Addressable - " + nameToUse;
            }
            if (m_AssetRefObject.editorAsset != null)
            {
                var subAssets = new List <Object>();
                subAssets.Add(null);
                var assetPath = AssetDatabase.GUIDToAssetPath(m_AssetRefObject.AssetGUID);
                subAssets.AddRange(AssetDatabase.LoadAllAssetRepresentationsAtPath(assetPath));
                var mainType = AssetDatabase.GetMainAssetTypeAtPath(assetPath);
                if (mainType == typeof(SpriteAtlas))
                {
                    var atlas   = AssetDatabase.LoadAssetAtPath <SpriteAtlas>(assetPath);
                    var sprites = new Sprite[atlas.spriteCount];
                    atlas.GetSprites(sprites);
                    subAssets.AddRange(sprites);
                }

                if (subAssets.Count > 1)
                {
                    assetDropDownRect = new Rect(assetDropDownRect.position, new Vector2(assetDropDownRect.width / 2, assetDropDownRect.height));
                    var objRect  = new Rect(assetDropDownRect.xMax, assetDropDownRect.y, assetDropDownRect.width, assetDropDownRect.height);
                    var objNames = new string[subAssets.Count];
                    var selIndex = 0;
                    for (int i = 0; i < subAssets.Count; i++)
                    {
                        var s       = subAssets[i];
                        var objName = s == null ? "<none>" : s.name;
                        if (objName.EndsWith("(Clone)"))
                        {
                            objName = objName.Replace("(Clone)", "");
                        }
                        objNames[i] = objName;
                        if (m_AssetRefObject.SubObjectName == objName)
                        {
                            selIndex = i;
                        }
                    }
                    //TODO: handle large amounts of sprites with a custom popup
                    var newIndex = EditorGUI.Popup(objRect, selIndex, objNames);
                    if (newIndex != selIndex)
                    {
                        Undo.RecordObject(property.serializedObject.targetObject, "Assign Asset Reference Sub Object");
                        var success = m_AssetRefObject.SetEditorSubObject(subAssets[newIndex]);
                        if (success)
                        {
                            EditorUtility.SetDirty(property.serializedObject.targetObject);
                            var comp = property.serializedObject.targetObject as Component;
                            if (comp != null && comp.gameObject != null && comp.gameObject.activeInHierarchy)
                            {
                                EditorSceneManager.MarkSceneDirty(comp.gameObject.scene);
                            }
                        }
                    }
                }
            }
            if (EditorGUI.DropdownButton(assetDropDownRect, new GUIContent(nameToUse, icon, m_AssetName), FocusType.Keyboard))
            {
                newGuidPropertyPath = property.propertyPath;
                var nonAddressedOption = isNotAddressable ? m_AssetName : string.Empty;
                PopupWindow.Show(assetDropDownRect, new AssetReferencePopup(this, guid, nonAddressedOption));
            }


            //During the drag, doing a light check on asset validity.  The in-depth check happens during a drop, and should include a log if it fails.
            var rejectedDrag = false;

            if (Event.current.type == EventType.DragUpdated && position.Contains(Event.current.mousePosition))
            {
                if (aaSettings == null)
                {
                    rejectedDrag = true;
                }
                else
                {
                    var aaEntries = DragAndDrop.GetGenericData("AssetEntryTreeViewItem") as List <AssetEntryTreeViewItem>;
                    if (aaEntries != null)
                    {
                        if (aaEntries.Count != 1)
                        {
                            rejectedDrag = true;
                        }
                        else
                        {
                            if (aaEntries[0] == null || aaEntries[0].entry == null || aaEntries[0].entry.IsInResources || !ValidateAsset(aaEntries[0].entry.AssetPath))
                            {
                                rejectedDrag = true;
                            }
                        }
                    }
                    else
                    {
                        if (DragAndDrop.paths.Length != 1)
                        {
                            rejectedDrag = true;
                        }
                        else
                        {
                            if (!ValidateAsset(DragAndDrop.paths[0]))
                            {
                                rejectedDrag = true;
                            }
                        }
                    }
                }
                DragAndDrop.visualMode = rejectedDrag ? DragAndDropVisualMode.Rejected : DragAndDropVisualMode.Copy;
            }

            if (!rejectedDrag && Event.current.type == EventType.DragPerform && position.Contains(Event.current.mousePosition))
            {
                var aaEntries = DragAndDrop.GetGenericData("AssetEntryTreeViewItem") as List <AssetEntryTreeViewItem>;
                if (aaEntries != null)
                {
                    if (aaEntries.Count == 1)
                    {
                        var item = aaEntries[0];
                        if (item.entry != null)
                        {
                            if (item.entry.IsInResources)
                            {
                                Addressables.LogWarning("Cannot use an AssetReference on an asset in Resources. Move asset out of Resources first.");
                            }
                            else
                            {
                                SetObject(property, item.entry.TargetAsset, out guid);
                            }
                        }
                    }
                }
                else
                {
                    if (DragAndDrop.paths != null && DragAndDrop.paths.Length == 1)
                    {
                        var path = DragAndDrop.paths[0];
                        if (AddressableAssetUtility.IsInResources(path))
                        {
                            Addressables.LogWarning("Cannot use an AssetReference on an asset in Resources. Move asset out of Resources first. ");
                        }
                        else if (!AddressableAssetUtility.IsPathValidForEntry(path))
                        {
                            Addressables.LogWarning("Dragged asset is not valid as an Asset Reference. " + path);
                        }
                        else
                        {
                            Object obj;
                            if (DragAndDrop.objectReferences != null && DragAndDrop.objectReferences.Length == 1)
                            {
                                obj = DragAndDrop.objectReferences[0];
                            }
                            else
                            {
                                obj = AssetDatabase.LoadAssetAtPath <Object>(path);
                            }

                            if (SetObject(property, obj, out guid))
                            {
                                aaSettings = AddressableAssetSettingsDefaultObject.GetSettings(true);
                                var entry = aaSettings.FindAssetEntry(guid);
                                if (entry == null && !string.IsNullOrEmpty(guid))
                                {
                                    aaSettings.CreateOrMoveEntry(guid, aaSettings.DefaultGroup);
                                    newGuid = guid;
                                }
                            }
                        }
                    }
                }
            }

            EditorGUI.EndProperty();
        }
예제 #9
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 IsInResourcesHandlesExtraPathing()
 {
     Assert.IsTrue(AddressableAssetUtility.IsInResources("path/path/resources/path"));
     Assert.IsTrue(AddressableAssetUtility.IsInResources("path/path/resources/"));
     Assert.IsTrue(AddressableAssetUtility.IsInResources("/resources/path"));
 }
 public void IsInResourcesProperlyHandlesCase()
 {
     Assert.IsTrue(AddressableAssetUtility.IsInResources("/rEsOurces/"));
     Assert.IsTrue(AddressableAssetUtility.IsInResources("/resources/"));
     Assert.IsTrue(AddressableAssetUtility.IsInResources("/RESOURCES/"));
 }
예제 #12
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (property == null || label == null)
            {
                Debug.LogError("Error rendering drawer for AssetReference property.");
                return;
            }
            string labelText = label.text;

            m_AssetRefObject = property.GetActualObjectForSerializedProperty <AssetReference>(fieldInfo, ref labelText);
            label.text       = labelText;
            if (m_AssetRefObject == null)
            {
                return;
            }

            EditorGUI.BeginProperty(position, label, property);

            GatherFilters(property);
            string guid       = m_AssetRefObject.RuntimeKey.ToString();
            var    aaSettings = AddressableAssetSettingsDefaultObject.Settings;

            var checkToForceAddressable = string.Empty;

            if (!string.IsNullOrEmpty(newGuid) && newGuidPropertyPath == property.propertyPath)
            {
                if (newGuid == noAssetString)
                {
                    SetObject(property, null, out guid);
                    newGuid = string.Empty;
                }
                else
                {
                    if (SetObject(property, AssetDatabase.LoadAssetAtPath <Object>(AssetDatabase.GUIDToAssetPath(newGuid)), out guid))
                    {
                        checkToForceAddressable = newGuid;
                    }
                    newGuid = string.Empty;
                }
            }

            bool isNotAddressable = false;

            m_AssetName = noAssetString;
            Texture2D icon = null;

            if (aaSettings != null && !string.IsNullOrEmpty(guid))
            {
                var entry = aaSettings.FindAssetEntry(guid);
                if (entry != null)
                {
                    m_AssetName = entry.address;
                    icon        = AssetDatabase.GetCachedIcon(entry.AssetPath) as Texture2D;
                }
                else
                {
                    var path = AssetDatabase.GUIDToAssetPath(guid);
                    if (!string.IsNullOrEmpty(path))
                    {
                        var  dir       = Path.GetDirectoryName(path);
                        bool foundAddr = false;
                        while (!string.IsNullOrEmpty(dir))
                        {
                            var dirEntry = aaSettings.FindAssetEntry(AssetDatabase.AssetPathToGUID(dir));
                            if (dirEntry != null)
                            {
                                foundAddr   = true;
                                m_AssetName = dirEntry.address + path.Remove(0, dir.Length);
                                break;
                            }
                            dir = Path.GetDirectoryName(dir);
                        }

                        if (!foundAddr)
                        {
                            m_AssetName = path;
                            if (!string.IsNullOrEmpty(checkToForceAddressable))
                            {
                                var newEntry = aaSettings.CreateOrMoveEntry(guid, aaSettings.DefaultGroup);
                                Addressables.LogFormat("Created AddressableAsset {0} in group {1}.", newEntry.address, aaSettings.DefaultGroup.Name);
                            }
                            else
                            {
                                if (!File.Exists(path))
                                {
                                    m_AssetName = "Missing File!";
                                }
                                else
                                {
                                    isNotAddressable = true;
                                }
                            }
                        }
                        icon = AssetDatabase.GetCachedIcon(path) as Texture2D;
                    }
                    else
                    {
                        m_AssetName = "Missing File!";
                    }
                }
            }

            smallPos = EditorGUI.PrefixLabel(position, label);
            var nameToUse = m_AssetName;

            if (isNotAddressable)
            {
                nameToUse = "Not Addressable - " + nameToUse;
            }

            if (EditorGUI.DropdownButton(smallPos, new GUIContent(nameToUse, icon, m_AssetName), FocusType.Keyboard))
            {
                newGuidPropertyPath = property.propertyPath;
                var nonAddressedOption = isNotAddressable ? m_AssetName : string.Empty;
                PopupWindow.Show(smallPos, new AssetReferencePopup(this, guid, nonAddressedOption));
            }


            //During the drag, doing a light check on asset validity.  The in-depth check happens during a drop, and should include a log if it fails.
            var rejectedDrag = false;

            if (Event.current.type == EventType.DragUpdated && position.Contains(Event.current.mousePosition))
            {
                if (aaSettings == null)
                {
                    rejectedDrag = true;
                }
                else
                {
                    var aaEntries = DragAndDrop.GetGenericData("AssetEntryTreeViewItem") as List <AssetEntryTreeViewItem>;
                    if (aaEntries != null)
                    {
                        if (aaEntries.Count != 1)
                        {
                            rejectedDrag = true;
                        }
                        else
                        {
                            if (aaEntries[0] != null &&
                                aaEntries[0].entry != null &&
                                aaEntries[0].entry.IsInResources)
                            {
                                rejectedDrag = true;
                            }
                        }
                    }
                    else
                    {
                        if (DragAndDrop.paths.Length != 1)
                        {
                            rejectedDrag = true;
                        }
                    }
                }
                DragAndDrop.visualMode = rejectedDrag ? DragAndDropVisualMode.Rejected : DragAndDropVisualMode.Copy;
            }

            if (!rejectedDrag && Event.current.type == EventType.DragPerform && position.Contains(Event.current.mousePosition))
            {
                var aaEntries = DragAndDrop.GetGenericData("AssetEntryTreeViewItem") as List <AssetEntryTreeViewItem>;
                if (aaEntries != null)
                {
                    if (aaEntries.Count == 1)
                    {
                        var item = aaEntries[0];
                        if (item.entry != null)
                        {
                            if (item.entry.IsInResources)
                            {
                                Addressables.LogWarning("Cannot use an AssetReference on an asset in Resources. Move asset out of Resources first.");
                            }
                            else
                            {
                                SetObject(property, AssetDatabase.LoadAssetAtPath <Object>(item.entry.AssetPath), out guid);
                            }
                        }
                    }
                }
                else
                {
                    if (DragAndDrop.paths != null && DragAndDrop.paths.Length == 1)
                    {
                        var path = DragAndDrop.paths[0];
                        if (AddressableAssetUtility.IsInResources(path))
                        {
                            Addressables.LogWarning("Cannot use an AssetReference on an asset in Resources. Move asset out of Resources first. ");
                        }
                        else if (!AddressableAssetUtility.IsPathValidForEntry(path))
                        {
                            Addressables.LogWarning("Dragged asset is not valid as an Asset Reference. " + path);
                        }
                        else
                        {
                            Object obj;
                            if (DragAndDrop.objectReferences != null && DragAndDrop.objectReferences.Length == 1)
                            {
                                obj = DragAndDrop.objectReferences[0];
                            }
                            else
                            {
                                obj = AssetDatabase.LoadAssetAtPath <Object>(path);
                            }

                            if (SetObject(property, obj, out guid))
                            {
                                aaSettings = AddressableAssetSettingsDefaultObject.GetSettings(true);
                                var entry = aaSettings.FindAssetEntry(guid);
                                if (entry == null && !string.IsNullOrEmpty(guid))
                                {
                                    aaSettings.CreateOrMoveEntry(guid, aaSettings.DefaultGroup);
                                    newGuid = guid;
                                }
                            }
                        }
                    }
                }
            }

            EditorGUI.EndProperty();
        }
        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);
                }
            }
        }
        private void HandleDragAndDrop(SerializedProperty property, bool isDragging, bool isDropping, string guid)
        {
            var aaSettings = AddressableAssetSettingsDefaultObject.Settings;
            //During the drag, doing a light check on asset validity.  The in-depth check happens during a drop, and should include a log if it fails.
            var rejectedDrag = false;

            if (isDragging)
            {
                if (aaSettings == null)
                {
                    rejectedDrag = true;
                }
                else
                {
                    var aaEntries = DragAndDrop.GetGenericData("AssetEntryTreeViewItem") as List <AssetEntryTreeViewItem>;
                    if (aaEntries != null)
                    {
                        if (aaEntries.Count != 1)
                        {
                            rejectedDrag = true;
                        }
                        else
                        {
                            if (aaEntries[0] == null || aaEntries[0].entry == null || aaEntries[0].entry.IsInResources || !ValidateAsset(aaEntries[0].entry.AssetPath))
                            {
                                rejectedDrag = true;
                            }
                        }
                    }
                    else
                    {
                        if (DragAndDrop.paths.Length != 1)
                        {
                            rejectedDrag = true;
                        }
                        else
                        {
                            if (!ValidateAsset(DragAndDrop.paths[0]))
                            {
                                rejectedDrag = true;
                            }
                        }
                    }
                }
                DragAndDrop.visualMode = rejectedDrag ? DragAndDropVisualMode.Rejected : DragAndDropVisualMode.Copy;
            }

            if (!rejectedDrag && isDropping)
            {
                var aaEntries = DragAndDrop.GetGenericData("AssetEntryTreeViewItem") as List <AssetEntryTreeViewItem>;
                if (aaEntries != null)
                {
                    if (aaEntries.Count == 1)
                    {
                        var item = aaEntries[0];
                        if (item.entry != null)
                        {
                            if (item.entry.IsInResources)
                            {
                                Addressables.LogWarning("Cannot use an AssetReference on an asset in Resources. Move asset out of Resources first.");
                            }
                            else
                            {
                                SetObject(property, item.entry.TargetAsset, out guid);
                            }
                        }
                    }
                }
                else
                {
                    if (DragAndDrop.paths != null && DragAndDrop.paths.Length == 1)
                    {
                        var path = DragAndDrop.paths[0];
                        if (AddressableAssetUtility.IsInResources(path))
                        {
                            Addressables.LogWarning("Cannot use an AssetReference on an asset in Resources. Move asset out of Resources first. ");
                        }
                        else if (!AddressableAssetUtility.IsPathValidForEntry(path))
                        {
                            Addressables.LogWarning("Dragged asset is not valid as an Asset Reference. " + path);
                        }
                        else
                        {
                            Object obj;
                            if (DragAndDrop.objectReferences != null && DragAndDrop.objectReferences.Length == 1)
                            {
                                obj = DragAndDrop.objectReferences[0];
                            }
                            else
                            {
                                obj = AssetDatabase.LoadAssetAtPath <Object>(path);
                            }

                            if (SetObject(property, obj, out guid))
                            {
                                aaSettings = AddressableAssetSettingsDefaultObject.GetSettings(true);
                                var entry = aaSettings.FindAssetEntry(guid);
                                if (entry == null && !string.IsNullOrEmpty(guid))
                                {
                                    aaSettings.CreateOrMoveEntry(guid, aaSettings.DefaultGroup);
                                    newGuid = guid;
                                }
                            }
                        }
                    }
                }
            }
        }
예제 #16
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);
                }
            }
        }