Ejemplo n.º 1
0
        static void GatherEntryLocations(AddressableAssetEntry entry, Type type, IList <IResourceLocation> locations, AddressableAssetTree assetTree)
        {
            if (!string.IsNullOrEmpty(entry.address) && entry.address.Contains("[") && entry.address.Contains("]"))
            {
                Debug.LogErrorFormat("Address '{0}' cannot contain '[ ]'.", entry.address);
                return;
            }
            using (new AddressablesFileEnumerationScope(assetTree))
            {
                entry.GatherAllAssets(null, true, true, false, e =>
                {
                    if (e.IsScene)
                    {
                        if (type == null || type == typeof(SceneInstance) || AddressableAssetUtility.MapEditorTypeToRuntimeType(e.MainAssetType, false) == type)
                        {
                            locations.Add(new ResourceLocationBase(e.address, e.AssetPath, typeof(SceneProvider).FullName, typeof(SceneInstance)));
                        }
                    }
                    else if (type == null || type.IsAssignableFrom(e.MainAssetType))
                    {
                        locations.Add(new ResourceLocationBase(e.address, e.AssetPath, typeof(AssetDatabaseProvider).FullName, e.MainAssetType));
                        return(true);
                    }
                    else
                    {
                        ObjectIdentifier[] ids = ContentBuildInterface.GetPlayerObjectIdentifiersInAsset(new GUID(e.guid), EditorUserBuildSettings.activeBuildTarget);
                        if (ids.Length > 1)
                        {
                            foreach (var t in AddressableAssetEntry.GatherSubObjectTypes(ids, e.guid))
                            {
                                if (type.IsAssignableFrom(t))
                                {
                                    locations.Add(new ResourceLocationBase(e.address, e.AssetPath, typeof(AssetDatabaseProvider).FullName, t));
                                }
                            }

                            return(true);
                        }
                    }
                    return(false);
                });
            }
        }
Ejemplo n.º 2
0
        public bool Locate(object key, Type type, out IList <IResourceLocation> locations)
        {
            CacheKey cacheKey = new CacheKey()
            {
                m_key = key, m_type = type
            };

            if (m_Cache.TryGetValue(cacheKey, out locations))
            {
                return(locations != null);
            }

            locations = new List <IResourceLocation>();
            if (m_keyToEntries.TryGetValue(key, out HashSet <AddressableAssetEntry> entries))
            {
                foreach (AddressableAssetEntry e in entries)
                {
                    if (AssetDatabase.IsValidFolder(e.AssetPath) && !e.labels.Contains(key as string))
                    {
                        continue;
                    }

                    if (type == null)
                    {
                        if (e.MainAssetType != typeof(SceneAsset))
                        {
                            ObjectIdentifier[] ids =
                                ContentBuildInterface.GetPlayerObjectIdentifiersInAsset(new GUID(e.guid),
                                                                                        EditorUserBuildSettings.activeBuildTarget);
                            IEnumerable <Type> subObjectTypes = AddressableAssetEntry.GatherSubObjectTypes(ids, e.guid);

                            if (subObjectTypes.Any())
                            {
                                foreach (Type t in subObjectTypes)
                                {
                                    GatherEntryLocations(e, t, locations, m_AddressableAssetTree);
                                }
                            }
                            else
                            {
                                GatherEntryLocations(e, null, locations, m_AddressableAssetTree);
                            }
                        }
                        else
                        {
                            GatherEntryLocations(e, null, locations, m_AddressableAssetTree);
                        }
                    }
                    else
                    {
                        GatherEntryLocations(e, type, locations, m_AddressableAssetTree);
                    }
                }
            }

            if (type == null)
            {
                type = typeof(UnityEngine.Object);
            }

            string keyStr = key as string;

            if (!string.IsNullOrEmpty(keyStr))
            {
                //check if the key is a guid first
                var keyPath = AssetDatabase.GUIDToAssetPath(keyStr);
                if (!string.IsNullOrEmpty(keyPath))
                {
                    //only look for folders from GUID if no locations have been found
                    if (locations.Count == 0)
                    {
                        var slash = keyPath.LastIndexOf('/');
                        while (slash > 0)
                        {
                            keyPath = keyPath.Substring(0, slash);
                            var parentFolderKey = AssetDatabase.AssetPathToGUID(keyPath);
                            if (string.IsNullOrEmpty(parentFolderKey))
                            {
                                break;
                            }

                            if (m_keyToEntries.ContainsKey(parentFolderKey))
                            {
                                AddLocations(locations, type, keyPath, AssetDatabase.GUIDToAssetPath(keyStr));
                                break;
                            }
                            slash = keyPath.LastIndexOf('/');
                        }
                    }
                }
                else
                {
                    //if the key is not a GUID, see if it is contained in a folder entry
                    keyPath = keyStr;
                    int slash = keyPath.LastIndexOf('/');
                    while (slash > 0)
                    {
                        keyPath = keyPath.Substring(0, slash);
                        if (m_keyToEntries.TryGetValue(keyPath, out var entry))
                        {
                            foreach (var e in entry)
                            {
                                AddLocations(locations, type, keyStr, GetInternalIdFromFolderEntry(keyStr, e));
                            }
                            break;
                        }
                        slash = keyPath.LastIndexOf('/');
                    }
                }

                //check resources folders
                if (m_includeResourcesFolders)
                {
                    string             resPath = keyStr;
                    UnityEngine.Object obj     = Resources.Load(resPath, type);
                    if (obj == null && keyStr.Length == 32)
                    {
                        resPath = AssetDatabase.GUIDToAssetPath(keyStr);
                        if (!string.IsNullOrEmpty(resPath))
                        {
                            int index = resPath.IndexOf("Resources/", StringComparison.Ordinal);
                            if (index >= 0)
                            {
                                int start  = index + 10;
                                int length = resPath.Length - (start + System.IO.Path.GetExtension(resPath).Length);
                                resPath = resPath.Substring(index + 10, length);
                                obj     = Resources.Load(resPath, type);
                            }
                        }
                    }
                    if (obj != null)
                    {
                        locations.Add(new ResourceLocationBase(keyStr, resPath, typeof(LegacyResourcesProvider).FullName, type));
                    }
                }
            }

            if (locations.Count == 0)
            {
                locations = null;
                m_Cache.Add(cacheKey, locations);
                return(false);
            }

            m_Cache.Add(cacheKey, locations);
            return(true);
        }