// private void _LoadAssetBundleInternal(string abPath, bool isDep) { AssetBundleReference abRef; if (_loadedAssetBundles.TryGetValue(abPath, out abRef)) { //已经加载了,持有一个依赖引用 if (isDep) { abRef.Depend(); } return; } AssetBundle ab = AssetBundle.LoadFromFile(GetFullPath(abPath)); if (ab == null) { return; } abRef = new AssetBundleReference(); abRef._assetbundle = ab; abRef._path = abPath; _loadedAssetBundles.Add(abPath, abRef); if (isDep) { abRef.Depend(); } }
//尝试加载一个AB //加载过了直接返回,没有加载过,会执行异步加载 //如果加载的是依赖,会增加依赖计数 private IEnumerator _LoadAssetBundleInternalAsync(string abPath, bool isDep) { AssetBundleReference abRef; if (_loadedAssetBundles.TryGetValue(abPath, out abRef)) { //已经加载了,持有一个依赖引用 if (isDep) { abRef.Depend(); } yield break; } //如果在加载队列里 if (_loadingAssetBundles.Contains(abPath)) { //等待加载完成 while (_loadingAssetBundles.Contains(abPath)) { yield return(null); } //加载完成了,但是可能加载失败,获取一次 if (isDep) { if (_loadedAssetBundles.TryGetValue(abPath, out abRef)) { //持有一个依赖引用 abRef.Depend(); } } yield break; } _loadingAssetBundles.Add(abPath); AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(GetFullPath(abPath)); yield return(request); //加载完成s if (request.assetBundle == null) { if (_loadingAssetBundles.Contains(abPath)) { _loadingAssetBundles.Remove(abPath); } yield break; } abRef = new AssetBundleReference(); abRef._assetbundle = request.assetBundle; abRef._path = abPath; _loadedAssetBundles.Add(abPath, abRef); //如果还在加载队列里,则记录依赖引用;不在加载队列里,说明提交底层加载后,AB却被Unload了 if (_loadingAssetBundles.Contains(abPath)) { if (isDep) { abRef.Depend(); } _loadingAssetBundles.Remove(abPath); } }