Exemple #1
0
        static AssetDatabaseLoader()
        {
#if UNITY_EDITOR
            IsAvailable   = true;
            GetAssetPaths = AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName;
            Load          = ToLoadDelegate(GetAssetPaths);
#endif
        }
        static AssetGraphLoader()
        {
#if UNITY_EDITOR
            var type = CompilationPipeline.GetAssemblies()
                       .Select(asm => Type.GetType("UnityEngine.AssetGraph.AssetBundleBuildMap, " + asm.name))
                       .FirstOrDefault(t => t != null);
            if (type == null)
            {
                return;
            }
            var map    = type.GetMethod("GetBuildMap")?.Invoke(null, null);
            var method = type.GetMethod("GetAssetPathsFromAssetBundleAndAssetName");
            if (map == null || method == null)
            {
                return;
            }
            IsAvailable   = true;
            GetAssetPaths = (GetAssetPathsDelegate)Delegate.CreateDelegate(typeof(GetAssetPathsDelegate), map, method);
            Load          = AssetDatabaseLoader.ToLoadDelegate(GetAssetPaths);
#endif
        }
Exemple #3
0
        public static LoadDelegate ToLoadDelegate(GetAssetPathsDelegate getAssetPaths)
        {
#if UNITY_EDITOR
            if (getAssetPaths == null)
            {
                throw new ArgumentNullException(nameof(getAssetPaths));
            }
            return(entry =>
            {
                string path = getAssetPaths(entry.NormBundleName, entry.AssetName)?.FirstOrDefault();
                if (string.IsNullOrEmpty(path))
                {
                    return Observable.Throw <UnityEngine.Object>(
                        new AssetNotFoundException(entry),
                        Scheduler.MainThreadIgnoreTimeScale);
                }
                switch (entry.LoadMethod)
                {
                case LoadMethod.Single:
                    return Observable.Return(AssetDatabase.LoadAssetAtPath(path, entry.AssetType),
                                             Scheduler.MainThreadIgnoreTimeScale);

                case LoadMethod.Multi:
                    return AssetDatabase.LoadMainAssetAtPath(path).ToSingleEnumerable()
                    .Concat(AssetDatabase.LoadAllAssetRepresentationsAtPath(path)
                            .Where(AssetDatabase.IsForeignAsset))
                    .Where(obj => entry.AssetType.IsInstanceOfType(obj))
                    .ToObservable(Scheduler.MainThreadIgnoreTimeScale);

                default: throw new ArgumentException("Unknown LoadMethod. " + entry.LoadMethod);
                }
            });
#else
            throw new NotSupportedException();
#endif
        }