void InternalLoadAssetBundle(AssetBundleInfo abInfo) { abInfo.LoadFromFile(); _is_current_loader.Add(abInfo); List <string> dependencys = _dependency.GetDependency(abInfo.GetAssetBundleName()); for (int i = 0; i < dependencys.Count; i++) { AssetBundleInfo info = LoadAssetBundle(dependencys[i]); info.AddReferenceCount(); } }
IEnumerator LoadAsset(AssetBundleInfo assetBundle, string resName, Type type, OnResourceLoaded onLoaded) { if (assetBundle == null || assetBundle.GetAssetBundle() == null) { onLoaded(null); yield break; } AssetBundleRequest request = assetBundle.GetAssetBundle().LoadAssetAsync(resName, type); yield return(request); onLoaded(request.asset); assetBundle.AddReferenceCount(); }
Dictionary <string, int> m_LoadingAsset = new Dictionary <string, int>(); // 正在loading 的asset 这个主要处理dependence 的,如果像ullua 那样,a,b ,同时依赖c 并且同时开始load,会出现load c 两次的情况 --- IEnumerator RealLoadAsseBundle(string assetName, bool isDependence) { if (isDependence) { int refCount = 0; if (m_LoadingAsset.TryGetValue(assetName, out refCount))// 如果已经在load了,ref++ break; { refCount++; m_LoadingAsset[assetName] = refCount; yield break; // } else { m_LoadingAsset[assetName] = 1; } } // load depencences -- string[] dependencies = m_assetBundleManifest.GetAllDependencies(assetName); int count = dependencies.Length; if (count > 0) { m_dependencies.Add(assetName, dependencies); for (int i = 0; i < count; i++) { AssetBundleInfo bundleInfo = null; string depName = dependencies[i]; if (m_loadedAssetBundles.TryGetValue(depName, out bundleInfo)) { bundleInfo.AddReferenceCount(); } else { yield return(m_helper.TryStartCoroutine(RealLoadAsseBundle(depName, true))); } } } // wait for all dependences loaded------------ if (count > 0) { bool waitForDependence = true; while (waitForDependence) { waitForDependence = false; for (int i = 0; i < count; i++) { AssetBundleInfo bundleInfo = null; string depName = dependencies[i]; if (!m_loadedAssetBundles.TryGetValue(depName, out bundleInfo)) { waitForDependence = true; break; } } yield return(0); } } string url = m_pathConfig.getAssetDir() + assetName; // + add .unity maybe.. WWW load = WWW.LoadFromCacheOrDownload(url, m_assetBundleManifest.GetAssetBundleHash(assetName), 0); yield return(load); AssetBundle assetObj = null; if (string.IsNullOrEmpty(load.error)) // no error { assetObj = load.assetBundle; } if (assetObj != null) { AssetBundleInfo abInfo = m_assetBundleInfoPool.Get(); abInfo.SetAssetBundle(assetObj); m_loadedAssetBundles.Add(assetName, abInfo); if (isDependence) { int refCount = 0; if (m_LoadingAsset.TryGetValue(assetName, out refCount))// loading zhong -- 等待其他携程load成功--------- { abInfo.AddReferenceCount(refCount); } } } else { m_loadedAssetBundles.Add(assetName, m_badAssetBundleInfo); } if (isDependence) { m_LoadingAsset.Remove(assetName); } }