/// <summary> /// 内部加载bundle /// </summary> /// <param name="url">asset路径</param> /// <param name="async">是否异步</param> /// <returns>bundle对象</returns> private ABundle LoadInternal(string url, bool async) { ABundle bundle = null; if (m_BundleDic.TryGetValue(url, out bundle)) { //从缓存中取并引用+1 bundle.AddReference(); return(bundle); } //创建ab if (async) { bundle = new BundleAsync(); bundle.url = url; m_AsyncList.Add(bundle as ABundleAsync); } else { bundle = new Bundle(); bundle.url = url; } m_BundleDic.Add(url, bundle); bundle.AddReference(); bundle.Load(); return(bundle); }
/// <summary> /// 卸载bundle /// </summary> /// <param name="bundle">要卸载的bundle</param> internal void UnLoad(ABundle bundle) { if (bundle == null) { throw new ArgumentException($"{nameof(BundleManager)}.{nameof(UnLoad)}() bundle is null."); } //引用-1 bundle.ReduceReference(); //引用为0,直接释放 if (bundle.reference == 0) { if (!bundle.done && bundle is BundleAsync) { BundleAsync bundleAsync = bundle as BundleAsync; if (m_AsyncList.Contains(bundleAsync)) { m_AsyncList.Remove(bundleAsync); } } m_BundleDic.Remove(bundle.url); bundle.UnLoad(); } }
/// <summary> /// 内部加载bundle /// </summary> /// <param name="url">asset路径</param> /// <param name="async">是否异步</param> /// <returns>bundle对象</returns> private ABundle LoadInternal(string url, bool async) { ABundle bundle; if (m_BundleDic.TryGetValue(url, out bundle)) { if (bundle.reference == 0) { m_NeedUnloadList.Remove(bundle); } //从缓存中取并引用+1 bundle.AddReference(); return(bundle); } //创建ab if (async) { bundle = new BundleAsync(); bundle.url = url; m_AsyncList.Add(bundle as ABundleAsync); } else { bundle = new Bundle(); bundle.url = url; } m_BundleDic.Add(url, bundle); //加载依赖 string[] dependencies = m_AssetBundleManifest.GetDirectDependencies(url); if (dependencies.Length > 0) { bundle.dependencies = new ABundle[dependencies.Length]; for (int i = 0; i < dependencies.Length; i++) { string dependencyUrl = dependencies[i]; ABundle dependencyBundle = LoadInternal(dependencyUrl, async); bundle.dependencies[i] = dependencyBundle; } } bundle.AddReference(); bundle.Load(); return(bundle); }
public void LateUpdate() { if (m_NeedUnloadList.Count == 0) { return; } while (m_NeedUnloadList.Count > 0) { ABundle bundle = m_NeedUnloadList.First.Value; m_NeedUnloadList.RemoveFirst(); if (bundle == null) { continue; } m_BundleDic.Remove(bundle.url); if (!bundle.done && bundle is BundleAsync) { BundleAsync bundleAsync = bundle as BundleAsync; if (m_AsyncList.Contains(bundleAsync)) { m_AsyncList.Remove(bundleAsync); } } bundle.UnLoad(); //依赖引用-1 if (bundle.dependencies != null) { for (int i = 0; i < bundle.dependencies.Length; i++) { ABundle temp = bundle.dependencies[i]; UnLoad(temp); } } } }