예제 #1
0
        public static List <string> EnumerateAddressableFolder(string path, AddressableAssetSettings settings, bool recurseAll, IBuildLogger logger = null)
        {
            if (!AssetDatabase.IsValidFolder(path))
            {
                throw new Exception($"Path {path} cannot be enumerated because it does not exist");
            }

            AddressableAssetTree tree = m_PrecomputedTree != null ? m_PrecomputedTree : BuildAddressableTree(settings, logger);

            if (tree == null)
            {
                return(new List <string>());
            }

            AddLocalFilesToTreeIfNotEnumerated(tree, path, logger);

            List <string> files = new List <string>();

            using (logger.ScopedStep(LogLevel.Info, $"Enumerating Addressables Tree {path}"))
            {
                foreach (string file in tree.Enumerate(path, recurseAll))
                {
                    if (BuiltinSceneCache.Contains(file))
                    {
                        continue;
                    }
                    files.Add(file);
                }
            }
            return(files);
        }
예제 #2
0
        IEnumerable <string> GetAllValidAssetPathsFromDirectory(string path, bool recurseAll)
        {
            var files = from file in Directory.EnumerateFiles(path, "*.*", recurseAll ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
                        where AddressableAssetUtility.IsPathValidForEntry(file)
                        let convertedPath = file.Replace('\\', '/')
                                            where !BuiltinSceneCache.Contains(convertedPath)
                                            select convertedPath;

            return(files);
        }
예제 #3
0
        /// <summary>
        /// Gathers all asset entries.  Each explicit entry may contain multiple sub entries. For example, addressable folders create entries for each asset contained within.
        /// </summary>
        /// <param name="assets">The generated list of entries.  For simple entries, this will contain just the entry itself if specified.</param>
        /// <param name="includeSelf">Determines if the entry should be contained in the result list or just sub entries.</param>
        /// <param name="recurseAll">Determines if full recursion should be done when gathering entries.</param>
        /// <param name="includeSubObjects">Determines if sub objects such as sprites should be included.</param>
        /// <param name="entryFilter">Optional predicate to run against each entry, only returning those that pass.  A null filter will return all entries</param>
        public void GatherAllAssets(List <AddressableAssetEntry> assets, bool includeSelf, bool recurseAll, bool includeSubObjects, Func <AddressableAssetEntry, bool> entryFilter = null)
        {
            var settings = parentGroup.Settings;

            if (guid == EditorSceneListName)
            {
                foreach (var s in BuiltinSceneCache.scenes)
                {
                    if (s.enabled)
                    {
                        var entry = settings.CreateSubEntryIfUnique(s.guid.ToString(), Path.GetFileNameWithoutExtension(s.path), this);
                        if (entry != null) //TODO - it's probably really bad if this is ever null. need some error detection
                        {
                            entry.IsInSceneList = true;
                            entry.m_Labels      = m_Labels;
                            if (entryFilter == null || entryFilter(entry))
                            {
                                assets.Add(entry);
                            }
                        }
                    }
                }
            }
            else if (guid == ResourcesName)
            {
                foreach (var resourcesDir in Directory.GetDirectories("Assets", "Resources", SearchOption.AllDirectories))
                {
                    foreach (var file in Directory.GetFiles(resourcesDir, "*.*", recurseAll ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly))
                    {
                        if (AddressableAssetUtility.IsPathValidForEntry(file))
                        {
                            var g     = AssetDatabase.AssetPathToGUID(file);
                            var addr  = GetResourcesPath(file);
                            var entry = settings.CreateSubEntryIfUnique(g, addr, this);
                            if (entry != null) //TODO - it's probably really bad if this is ever null. need some error detection
                            {
                                entry.IsInResources = true;
                                entry.m_Labels      = m_Labels;
                                if (entryFilter == null || entryFilter(entry))
                                {
                                    assets.Add(entry);
                                }
                            }
                        }
                    }
                    if (!recurseAll)
                    {
                        foreach (var folder in Directory.GetDirectories(resourcesDir))
                        {
                            if (AssetDatabase.IsValidFolder(folder))
                            {
                                var entry = settings.CreateSubEntryIfUnique(AssetDatabase.AssetPathToGUID(folder), GetResourcesPath(folder), this);
                                if (entry != null) //TODO - it's probably really bad if this is ever null. need some error detection
                                {
                                    entry.IsInResources = true;
                                    entry.m_Labels      = m_Labels;
                                    if (entryFilter == null || entryFilter(entry))
                                    {
                                        assets.Add(entry);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                var path = AssetPath;
                if (string.IsNullOrEmpty(path))
                {
                    return;
                }

                if (AssetDatabase.IsValidFolder(path))
                {
                    foreach (var fi in Directory.GetFiles(path, "*.*", recurseAll ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly))
                    {
                        var file = fi.Replace('\\', '/');
                        if (AddressableAssetUtility.IsPathValidForEntry(file))
                        {
                            var subGuid = AssetDatabase.AssetPathToGUID(file);
                            if (!BuiltinSceneCache.Contains(new GUID(subGuid)))
                            {
                                var entry = settings.CreateSubEntryIfUnique(subGuid, address + GetRelativePath(file, path), this);
                                if (entry != null)
                                {
                                    entry.IsInResources = IsInResources; //if this is a sub-folder of Resources, copy it on down
                                    entry.m_Labels      = m_Labels;
                                    if (entryFilter == null || entryFilter(entry))
                                    {
                                        assets.Add(entry);
                                    }
                                }
                            }
                        }
                    }
                    if (!recurseAll)
                    {
                        foreach (var fo in Directory.GetDirectories(path, "*.*", SearchOption.TopDirectoryOnly))
                        {
                            var folder = fo.Replace('\\', '/');
                            if (AssetDatabase.IsValidFolder(folder))
                            {
                                var entry = settings.CreateSubEntryIfUnique(AssetDatabase.AssetPathToGUID(folder), address + GetRelativePath(folder, path), this);
                                if (entry != null)
                                {
                                    entry.IsInResources = IsInResources; //if this is a sub-folder of Resources, copy it on down
                                    entry.m_Labels      = m_Labels;
                                    if (entryFilter == null || entryFilter(entry))
                                    {
                                        assets.Add(entry);
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (MainAssetType == typeof(AddressableAssetEntryCollection))
                    {
                        var col = AssetDatabase.LoadAssetAtPath <AddressableAssetEntryCollection>(AssetPath);
                        if (col != null)
                        {
                            foreach (var e in col.Entries)
                            {
                                var entry = settings.CreateSubEntryIfUnique(e.guid, e.address, this);
                                if (entry != null)
                                {
                                    entry.IsInResources = e.IsInResources;
                                    foreach (var l in e.labels)
                                    {
                                        entry.SetLabel(l, true, false);
                                    }
                                    foreach (var l in m_Labels)
                                    {
                                        entry.SetLabel(l, true, false);
                                    }
                                    if (entryFilter == null || entryFilter(entry))
                                    {
                                        assets.Add(entry);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        if (includeSelf)
                        {
                            if (entryFilter == null || entryFilter(this))
                            {
                                assets.Add(this);
                            }
                        }
                        if (includeSubObjects)
                        {
                            var mainType = AssetDatabase.GetMainAssetTypeAtPath(AssetPath);
                            if (mainType == typeof(SpriteAtlas))
                            {
                                var atlas   = AssetDatabase.LoadAssetAtPath <SpriteAtlas>(AssetPath);
                                var sprites = new Sprite[atlas.spriteCount];
                                atlas.GetSprites(sprites);

                                for (int i = 0; i < atlas.spriteCount; i++)
                                {
                                    var spriteName = sprites[i].name;
                                    if (spriteName.EndsWith("(Clone)"))
                                    {
                                        spriteName = spriteName.Replace("(Clone)", "");
                                    }

                                    var namedAddress = string.Format("{0}[{1}]", address, spriteName);
                                    var newEntry     = new AddressableAssetEntry("", namedAddress, parentGroup, true);
                                    newEntry.IsSubAsset    = true;
                                    newEntry.ParentEntry   = this;
                                    newEntry.IsInResources = IsInResources;
                                    assets.Add(newEntry);
                                }
                            }
                            var objs = AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetPath);
                            for (int i = 0; i < objs.Length; i++)
                            {
                                var o            = objs[i];
                                var t            = o.GetType();
                                var namedAddress = string.Format("{0}[{1}]", address, o.name);
                                var newEntry     = new AddressableAssetEntry("", namedAddress, parentGroup, true);
                                newEntry.IsSubAsset    = true;
                                newEntry.ParentEntry   = this;
                                newEntry.IsInResources = IsInResources;
                                assets.Add(newEntry);
                            }
                        }
                    }
                }
            }
        }
예제 #4
0
        public void GatherAllAssets(List <AddressableAssetEntry> assets, bool includeSelf, bool recurseAll, Func <AddressableAssetEntry, bool> entryFilter = null)
        {
            var settings = parentGroup.Settings;

            if (guid == EditorSceneListName)
            {
                foreach (var s in BuiltinSceneCache.scenes)
                {
                    if (s.enabled)
                    {
                        var entry = settings.CreateSubEntryIfUnique(s.guid.ToString(), Path.GetFileNameWithoutExtension(s.path), this);
                        if (entry != null) //TODO - it's probably really bad if this is ever null. need some error detection
                        {
                            entry.IsInSceneList = true;
                            entry.m_Labels      = m_Labels;
                            if (entryFilter == null || entryFilter(entry))
                            {
                                assets.Add(entry);
                            }
                        }
                    }
                }
            }
            else if (guid == ResourcesName)
            {
                foreach (var resourcesDir in Directory.GetDirectories("Assets", "Resources", SearchOption.AllDirectories))
                {
                    foreach (var file in Directory.GetFiles(resourcesDir, "*.*", recurseAll ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly))
                    {
                        if (AddressableAssetUtility.IsPathValidForEntry(file))
                        {
                            var g     = AssetDatabase.AssetPathToGUID(file);
                            var addr  = GetResourcesPath(file);
                            var entry = settings.CreateSubEntryIfUnique(g, addr, this);
                            if (entry != null) //TODO - it's probably really bad if this is ever null. need some error detection
                            {
                                entry.IsInResources = true;
                                entry.m_Labels      = m_Labels;
                                if (entryFilter == null || entryFilter(entry))
                                {
                                    assets.Add(entry);
                                }
                            }
                        }
                    }
                    if (!recurseAll)
                    {
                        foreach (var folder in Directory.GetDirectories(resourcesDir))
                        {
                            if (AssetDatabase.IsValidFolder(folder))
                            {
                                var entry = settings.CreateSubEntryIfUnique(AssetDatabase.AssetPathToGUID(folder), GetResourcesPath(folder), this);
                                if (entry != null) //TODO - it's probably really bad if this is ever null. need some error detection
                                {
                                    entry.IsInResources = true;
                                    entry.m_Labels      = m_Labels;
                                    if (entryFilter == null || entryFilter(entry))
                                    {
                                        assets.Add(entry);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                var path = AssetDatabase.GUIDToAssetPath(guid);
                if (string.IsNullOrEmpty(path))
                {
                    return;
                }

                if (AssetDatabase.IsValidFolder(path))
                {
                    foreach (var fi in Directory.GetFiles(path, "*.*", recurseAll ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly))
                    {
                        var file = fi.Replace('\\', '/');
                        if (AddressableAssetUtility.IsPathValidForEntry(file))
                        {
                            var subGuid = AssetDatabase.AssetPathToGUID(file);
                            if (!BuiltinSceneCache.Contains(new GUID(subGuid)))
                            {
                                var entry = settings.CreateSubEntryIfUnique(subGuid, address + GetRelativePath(file, path), this);
                                if (entry != null)
                                {
                                    entry.IsInResources = IsInResources; //if this is a sub-folder of Resources, copy it on down
                                    entry.m_Labels      = m_Labels;
                                    if (entryFilter == null || entryFilter(entry))
                                    {
                                        assets.Add(entry);
                                    }
                                }
                            }
                        }
                    }
                    if (!recurseAll)
                    {
                        foreach (var fo in Directory.GetDirectories(path, "*.*", SearchOption.TopDirectoryOnly))
                        {
                            var folder = fo.Replace('\\', '/');
                            if (AssetDatabase.IsValidFolder(folder))
                            {
                                var entry = settings.CreateSubEntryIfUnique(AssetDatabase.AssetPathToGUID(folder), address + GetRelativePath(folder, path), this);
                                if (entry != null)
                                {
                                    entry.IsInResources = IsInResources; //if this is a sub-folder of Resources, copy it on down
                                    entry.m_Labels      = m_Labels;
                                    if (entryFilter == null || entryFilter(entry))
                                    {
                                        assets.Add(entry);
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (AssetDatabase.GetMainAssetTypeAtPath(path) == typeof(AddressableAssetEntryCollection))
                    {
                        var col = AssetDatabase.LoadAssetAtPath <AddressableAssetEntryCollection>(AssetPath);
                        if (col != null)
                        {
                            foreach (var e in col.Entries)
                            {
                                var entry = settings.CreateSubEntryIfUnique(e.guid, e.address, this);
                                if (entry != null)
                                {
                                    entry.IsInResources = e.IsInResources;
                                    foreach (var l in e.labels)
                                    {
                                        entry.SetLabel(l, true, false);
                                    }
                                    foreach (var l in m_Labels)
                                    {
                                        entry.SetLabel(l, true, false);
                                    }
                                    if (entryFilter == null || entryFilter(entry))
                                    {
                                        assets.Add(entry);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        if (includeSelf)
                        {
                            if (entryFilter == null || entryFilter(this))
                            {
                                assets.Add(this);
                            }
                        }
                    }
                }
            }
        }