public UAsset GetAsset(string assetPath, Type type, EAssetHints hints)
        {
            WeakReference assetRef;
            UAsset        asset = null;

            if (_assets.TryGetValue(assetPath, out assetRef) && assetRef.IsAlive)
            {
                asset = assetRef.Target as UAsset;
                if (asset != null)
                {
                    ResourceManager.GetAnalyzer()?.OnAssetAccess(assetPath);
                    return(asset);
                }
            }
            ResourceManager.GetAnalyzer()?.OnAssetOpen(assetPath);
            if (Directory.Exists(assetPath))
            {
                asset = new UAssetDatabaseFileListAsset(assetPath);
            }
            else if (IsFileExists(assetPath))
            {
                asset = new UAssetDatabaseAsset(assetPath, type, hints, Random.Range(_asyncSimMin, _asyncSimMax));
            }
            else
            {
                asset = new UFailureAsset(assetPath, type);
            }
            _assets[assetPath] = new WeakReference(asset);
            return(asset);
        }
Example #2
0
 public UAssetBundleAsset(UAssetBundleBundle bundle, string assetPath, Type type, EAssetHints hints)
     : base(assetPath, type)
 {
     _hints  = hints;
     _bundle = bundle;
     _bundle.AddRef();
     _bundle.completed += OnBundleLoaded;
 }
Example #3
0
        //TODO: hints
        private UAsset GetAsset(string assetPath, bool concrete, Type type, EAssetHints hints)
        {
            if (_closed)
            {
                return(null);
            }

            UAsset        asset = null;
            WeakReference assetRef;
            var           transformedAssetPath = TransformAssetPath(assetPath);

            if (_assets.TryGetValue(transformedAssetPath, out assetRef) && assetRef.IsAlive)
            {
                asset = assetRef.Target as UAsset;
                if (asset != null)
                {
                    ResourceManager.GetAnalyzer()?.OnAssetAccess(assetPath);
                    return(asset);
                }
            }

            string bundleName;

            if (TryGetBundleNameByAssetPath(transformedAssetPath, out bundleName))
            {
                var bundle = this.GetBundle(bundleName, hints);
                if (bundle != null)
                {
                    try
                    {
                        bundle.AddRef();
                        ResourceManager.GetAnalyzer()?.OnAssetOpen(assetPath);
                        asset = bundle.CreateAsset(assetPath, type, concrete, hints);
                        if (asset != null)
                        {
                            _assets[TransformAssetPath(assetPath)] = new WeakReference(asset);
                            return(asset);
                        }
                    }
                    finally
                    {
                        bundle.RemoveRef();
                    }
                }

                // 不是 Unity 资源包, 不能实例化 AssetBundleUAsset
            }

            var invalid = new UFailureAsset(assetPath, type);

            _assets[TransformAssetPath(assetPath)] = new WeakReference(invalid);
            return(invalid);
        }
Example #4
0
        //TODO: hints
        private UBundle GetBundle(Manifest.BundleInfo bundleInfo, EAssetHints hints)
        {
            if (_closed)
            {
                return(null);
            }

            UBundle bundle = null;

            if (bundleInfo != null)
            {
                var bundleName = bundleInfo.name;
                if (!_bundles.TryGetValue(bundleName, out bundle))
                {
                    switch (bundleInfo.type)
                    {
                    case Manifest.BundleType.AssetBundle:
                        bundle = new UAssetBundleBundle(this, bundleInfo, hints);
                        break;

                    case Manifest.BundleType.ZipArchive:
                        bundle = new UZipArchiveBundle(this, bundleInfo);
                        break;

                    case Manifest.BundleType.FileList:
                        bundle = new UFileListBundle(this, bundleInfo);
                        break;
                    }

                    if (bundle != null)
                    {
                        _bundles.Add(bundleName, bundle);
                        _AddDependencies(bundle, hints, bundle.bundleInfo.dependencies);
                        // 第一次访问 UBundle 时进入此处逻辑, 但这之前可能已经使用 EnsureBundles 等方式发起文件下载
                        // 优先检查是否已经存在下载中的任务, 如果已经存在下载任务, 任务完成时将自动调用 UBundle.Load(stream)
                        var bundleJob = _FindDownloadJob(bundle.name);
                        if (bundleJob == null)
                        {
                            if (!LoadBundleFile(bundle))
                            {
                                DownloadBundleFile(bundle.bundleInfo, null);
                            }
                        }
                    }
                }
            }

            return(bundle);
        }
 public UAssetDatabaseAsset(string assetPath, Type type, EAssetHints hints, float delay)
     : base(assetPath, type)
 {
     if ((hints & EAssetHints.Synchronized) == 0 && delay > 0f)
     {
         JobScheduler.DispatchMainAfter(() =>
         {
             _OnAssetLoaded();
         }, delay);
     }
     else
     {
         _OnAssetLoaded();
     }
 }
Example #6
0
 private void _AddDependencies(UBundle bundle, EAssetHints hints, string[] dependencies)
 {
     if (dependencies != null)
     {
         for (int i = 0, size = dependencies.Length; i < size; i++)
         {
             var depBundleInfo = dependencies[i];
             var depBundle     = GetBundle(depBundleInfo, hints);
             if (bundle.AddDependency(depBundle))
             {
                 _AddDependencies(bundle, hints, depBundle.bundleInfo.dependencies);
             }
         }
     }
 }
        public UAsset GetAsset(string assetPath, Type type, EAssetHints hints)
        {
            WeakReference assetRef;
            UAsset        asset = null;

            if (_assets.TryGetValue(assetPath, out assetRef) && assetRef.IsAlive)
            {
                asset = assetRef.Target as UAsset;
                if (asset != null)
                {
                    ResourceManager.GetAnalyzer()?.OnAssetAccess(assetPath);
                    return(asset);
                }
            }
            ResourceManager.GetAnalyzer()?.OnAssetOpen(assetPath);
            asset = new UBuiltinAsset(assetPath, type, hints);
            _assets[assetPath] = new WeakReference(asset);
            return(asset);
        }
            public UBuiltinAsset(string assetPath, Type type, EAssetHints hints)
                : base(assetPath, type)
            {
                var resPath = assetPath;
                var prefix  = "Assets/";

                if (resPath.StartsWith(prefix))
                {
                    resPath = resPath.Substring(prefix.Length);
                }

                if ((hints & EAssetHints.Synchronized) != 0)
                {
                    _object = type != null?Resources.Load(resPath, type) : Resources.Load(resPath);

                    Complete();
                }
                else
                {
                    var request = type != null?Resources.LoadAsync(resPath, type) : Resources.LoadAsync(resPath);

                    request.completed += OnResourceLoaded;
                }
            }
Example #9
0
 public UAssetBundleConcreteAsset(UAssetBundleBundle bundle, string assetPath, Type type, EAssetHints hints)
     : base(bundle, assetPath, type, hints)
 {
 }
Example #10
0
            public override UAsset CreateAsset(string assetPath, Type type, bool concrete, EAssetHints hints)
            {
                if (concrete)
                {
                    return(new UAssetBundleConcreteAsset(this, assetPath, type, hints));
                }

                return(new UAssetBundleAsset(this, assetPath, type, hints));
            }
Example #11
0
 public UAssetBundleBundle(BundleAssetProvider provider, Manifest.BundleInfo bundleInfo, EAssetHints hints)
     : base(bundleInfo)
 {
     _hints    = hints;
     _provider = provider;
 }
Example #12
0
            public override UAsset CreateAsset(string assetPath, Type type, bool concrete, EAssetHints hints)
            {
                if (_disposed)
                {
                    return(null);
                }

                return(new UFileListBundleAsset(this, assetPath));
            }
Example #13
0
 public UAsset GetAsset(string assetPath, Type type, EAssetHints hints)
 {
     return(GetAsset(assetPath, true, type, hints));
 }
Example #14
0
        public UBundle GetBundle(string bundleName, EAssetHints hints)
        {
            var bundleInfo = GetBundleInfo(bundleName);

            return(GetBundle(bundleInfo, hints));
        }
Example #15
0
 public abstract UAsset CreateAsset(string assetPath, Type type, bool concrete, EAssetHints hints);
Example #16
0
 public override UAsset CreateAsset(string assetPath, Type type, bool concrete, EAssetHints hints)
 {
     return(new UZipArchiveBundleAsset(this, assetPath));
 }
Example #17
0
 //TODO: hints
 public UAssetBundleConcreteAsset(UAssetBundleBundle bundle, string assetPath, Type type, EAssetHints hints)
     : base(bundle, assetPath, type)
 {
     _objects = _emptyObjects;
 }