Example #1
0
        private UAsset GetAsset(string assetPath, bool concrete, Type type)
        {
            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 (_assetPath2Bundle.TryGetValue(transformedAssetPath, out bundleName))
            {
                var bundle = this.GetBundle(bundleName);
                if (bundle != null)
                {
                    bundle.AddRef();
                    var assetBundleUBundle = bundle as UAssetBundleBundle;
                    if (assetBundleUBundle != null)
                    {
                        ResourceManager.GetAnalyzer().OnAssetOpen(assetPath);
                        if (concrete)
                        {
                            asset = new UAssetBundleConcreteAsset(assetBundleUBundle, assetPath, type);
                        }
                        else
                        {
                            asset = new UAssetBundleAsset(assetBundleUBundle, assetPath);
                        }
                        _assets[TransformAssetPath(assetPath)] = new WeakReference(asset);
                    }
                    else
                    {
                        var zipArchiveBundle = bundle as UZipArchiveBundle;
                        if (zipArchiveBundle != null)
                        {
                            ResourceManager.GetAnalyzer().OnAssetOpen(assetPath);
                            asset = new UZipArchiveBundleAsset(zipArchiveBundle, assetPath);
                            _assets[TransformAssetPath(assetPath)] = new WeakReference(asset);
                        }
                    }
                    bundle.RemoveRef();
                    return(asset);
                }
                // 不是 Unity 资源包, 不能实例化 AssetBundleUAsset
            }
            var invalid = new UFailureAsset(assetPath);

            _assets[TransformAssetPath(assetPath)] = new WeakReference(invalid);
            return(invalid);
        }
        private UAsset GetAsset(string assetPath, bool concrete)
        {
            UAsset        asset = null;
            WeakReference assetRef;

            if (_assets.TryGetValue(assetPath, out assetRef) && assetRef.IsAlive)
            {
                asset = assetRef.Target as UAsset;
                if (asset != null)
                {
                    return(asset);
                }
            }
            string bundleName;

            if (_assetPath2Bundle.TryGetValue(assetPath, out bundleName))
            {
                var bundle = this.GetBundle(bundleName);
                if (bundle != null)
                {
                    bundle.AddRef();
                    var assetBundleUBundle = bundle as UAssetBundleBundle;
                    if (assetBundleUBundle != null)
                    {
                        if (concrete)
                        {
                            asset = new UAssetBundleConcreteAsset(assetBundleUBundle, assetPath);
                        }
                        else
                        {
                            asset = new UAssetBundleAsset(assetBundleUBundle, assetPath);
                        }
                        _assets[assetPath] = new WeakReference(asset);
                    }
                    bundle.RemoveRef();
                    return(asset);
                }
                // 不是 Unity 资源包, 不能实例化 AssetBundleUAsset
            }
            var invalid = new UFailureAsset(assetPath);

            _assets[assetPath] = new WeakReference(invalid);
            return(invalid);
        }