/// <summary>
    /// 释放单个AB包
    /// </summary>
    /// <param name="bundleName"></param>
    public void Dispose(string bundleName)
    {
        if (!relationDic.ContainsKey(bundleName))
        {
            Debug.LogError("LoadHelper Not Contains Key :  " + bundleName);
            return;
        }

        AssetBundleRelation abRelation = relationDic[bundleName];

        List <string> depenList = abRelation.GetDependenceBundle();        // 获取这个需要释放的AB包(a) 的所有依赖的包(b,c,d)

        for (int i = 0; i < depenList.Count; i++)
        {
            if (relationDic.ContainsKey(depenList[i]))
            {
                AssetBundleRelation depenABRlation = relationDic[depenList[i]];  // 获取b的依赖关系类
                if (depenABRlation.RemoveReference(bundleName))                  // 移除 b 和 a 的被依赖关系 , 如果b只有a一个依赖的话,就把b也删掉
                {
                    Dispose(depenABRlation.GetBundleName);
                }
            }
        }

        if (abRelation.GetReferBundleList().Count <= 0)
        {
            abRelation.Dispose();
            relationDic.Remove(bundleName);
        }
    }
    /// <summary>
    /// 卸载一个 bundle 包
    /// </summary>
    public void Dispose(string bundleName)
    {
        if (!nameBundleDic.ContainsKey(bundleName))
        {
            Debug.LogError("当前" + bundleName + "包没有被加载,无法获取资源!");
            return;
        }

        // 先得到当前的包
        AssetBundleRelation assetBundleRelation = nameBundleDic[bundleName];

        // 获取当前包所有的依赖关系
        string[] allDependences = assetBundleRelation.GetAllDependence();

        foreach (string item in allDependences)
        {
            // 移除 依赖的包里面的被依赖关系
            AssetBundleRelation tempassetBundleRelation = nameBundleDic[item];
            bool isDispose = tempassetBundleRelation.RemoveReference(bundleName);
            if (isDispose) // 没有其他依赖项了(可以释放了)
            {
                // 递归
                Dispose(tempassetBundleRelation.BundleName);
            }
        }

        // 开始卸载当前包
        if (assetBundleRelation.GetAllReference().Length <= 0)  //没有被依赖项了
        {
            nameBundleDic[bundleName].Dispose();
            nameBundleDic.Remove(bundleName);
        }
    }
    /// <summary>
    /// 卸载包
    /// </summary>
    public void UnloadAssetBundle(string path, bool isBundleName = false)
    {
        //获取包名
        string bundleName = path;

        if (!isBundleName)
        {
            bundleName = GetBundleName(path);
        }
        //安全校验
        if (!nameBundleDict.ContainsKey(bundleName))
        {
            Debug.Log(bundleName + "不在Dictionary中,可能未被加载或已被卸载");
            return;
        }
        //
        AssetBundleRelation relation = nameBundleDict[bundleName];

        string[] depedencies = relation.GetAllDependences();
        foreach (string depedence in depedencies)
        {
            nameBundleDict[depedence].RemoveReference(bundleName);
            UnloadAssetBundle(depedence, true);
        }
        if (relation.GetAllReferences().Length <= 0)
        {
            relation.UnloadAssetBundle();
            nameBundleDict.Remove(bundleName);
        }
    }
Beispiel #4
0
    /// <summary>
    /// 卸载内存中的单个AB包
    /// </summary>
    public void Dispose(string assetbundlename)
    {
        if (!NameAndBundleDict.ContainsKey(assetbundlename))
        {
            Debug.LogError("当前" + assetbundlename + "包没有加载,无法获取资源");
            return;
        }

        //先获取到当前需要卸载的包
        AssetBundleRelation abrelation = NameAndBundleDict[assetbundlename];

        //再获取当前包的所有依赖关系
        string[] allDependencies = abrelation.GetAllDependencies();
        //遍历每个依赖关系,处理当前包的依赖和被依赖关系
        foreach (string Dabname in allDependencies)
        {
            //获取当前包的依赖包的被依赖关系
            AssetBundleRelation referenceAB = NameAndBundleDict[Dabname];
            //如果当前包的依赖包,存在被依赖关系
            if (referenceAB.RemoveReference(assetbundlename))
            {
                //我们要先卸载被依赖包,并移除被依赖关系
                //递归
                Dispose(referenceAB.abName);
            }
        }
        //再次判断,确保当前包没有被依赖关系
        if (abrelation.GetAllReferences().Length <= 0)
        {
            //开始卸载当前包
            NameAndBundleDict[assetbundlename].Dispose();
            //移除当前包的字典映射
            NameAndBundleDict.Remove(assetbundlename);
        }
    }
    /// <summary>
    /// 卸载包
    /// </summary>
    public void Dispose(string bundleName)
    {
        if (!nameBundleDict.ContainsKey(bundleName))
        {
            Debug.LogError("当前 " + bundleName + " 包没有加载,无法获取资源");
            return;
        }

        //先获取到当前的包
        AssetBundleRelation assetBundleRelation = nameBundleDict[bundleName];

        //获取当前包的所有依赖关系
        string[] allDependences = assetBundleRelation.GetAllDependences();
        foreach (string dependenceBundleName in allDependences)
        {
            AssetBundleRelation tmpAssetBundle = nameBundleDict[dependenceBundleName];
            //首先 移除 依赖的包里面的被依赖关系
            if (tmpAssetBundle.RemoveReference(bundleName))
            {
                //递归
                Dispose(tmpAssetBundle.BundleName);
            }
        }

        //才开始卸载当前包
        if (assetBundleRelation.GetAllReferences().Length <= 0)
        {
            nameBundleDict[bundleName].Dispose();
            nameBundleDict.Remove(bundleName);
        }
    }
    /// <summary>
    /// 获取这个AB包下的所有资源名字
    /// </summary>
    /// <param name="bundleName"></param>
    /// <returns></returns>
    public string[] GetAllAssetNames(string bundleName)
    {
        if (!relationDic.ContainsKey(bundleName))
        {
            Debug.LogError("The loadHelper Not ContainsKey :" + bundleName);
            return(null);
        }

        AssetBundleRelation abRela = relationDic[bundleName];

        return(abRela.GetAllAssetNames());
    }
    /// <summary>
    /// 加载资源包
    /// </summary>
    /// <param name="path">xxx/xxx/xxx.assetbundle(.u3d)</param>
    private void LoadAssetBundle(string bundleName)
    {
        AssetBundleRelation assetBundleRelation = new AssetBundleRelation();

        nameBundleDict.Add(bundleName, assetBundleRelation);
        //加载依赖的包
        LoadDependenceAB(bundleName);
        //加载自己的包
        string bundlePath = PathUtil.ASSETBUNDLE_PLATFORM_PATH + bundleName;

        assetBundleRelation.LoadAssetBundle(bundlePath);
    }
    /// <summary>
    /// 这个AB包是否已经加载完成
    /// </summary>
    public bool IsLoadingFinished(string bundleName)
    {
        if (!relationDic.ContainsKey(bundleName))
        {
            Debug.LogError("The loadHelper Not ContainsKey :" + bundleName);
            return(false);
        }

        AssetBundleRelation abRela = relationDic[bundleName];

        return(abRela.IsBundleLoadFinished);
    }
    /// <summary>
    /// 释放所有AB包
    /// </summary>
    public void Dispose()
    {
        List <string> keyList = new List <string>();

        keyList.AddRange(relationDic.Keys);
        for (int i = 0; i < relationDic.Count; i++)
        {
            AssetBundleRelation abRela = relationDic[keyList[i]];
            abRela.Dispose();             // 释放所有AB包,就不用关心依赖关系了,都干掉
        }

        relationDic.Clear();
    }
    /// <summary>
    /// 加载资源包(给上层调用)
    /// </summary>
    /// <param name="bundleName"></param>
    /// <param name="lp"></param>
    /// <param name="labcb"></param>
    public void LoadAssetBundle(string bundleName, LoadProgress lp, LoadAssetBundleCallback labcb)
    {
        if (nameBundleDic.ContainsKey(bundleName))
        {
            Debug.LogError("此包已经加载了!" + bundleName);
            return;
        }
        else // 没有加载的话 , 就 new 一个
        {
            AssetBundleRelation assetBundleRelation = new AssetBundleRelation(bundleName, lp);
            nameBundleDic.Add(bundleName, assetBundleRelation);

            // 开始加载
            //StartCoroutine("Load", bundleName); 不继承monobehavior,不能用协程
            labcb(sceneName, bundleName); // 让上层去加载
        }
    }
    /// <summary>
    /// 加载资源包
    /// </summary>
    /// <param name="bundleName"></param>
    /// <param name="lp"></param>
    /// <param name="labcb"></param>
    public void LoadAssetBundle(string bundleName, LoadProgress lp, LoadAssetBundleCallback labcb)
    {
        if (nameBundleDict.ContainsKey(bundleName))
        {
            Debug.LogWarning("此包已经加载了 : " + bundleName);
            return;
        }
        else
        {
            //没有被加载,保存到字典里面
            AssetBundleRelation assetBundleRelation = new AssetBundleRelation(bundleName, lp);
            nameBundleDict.Add(bundleName, assetBundleRelation);

            //开始加载,由于没有继承子monobehaver,所以通过delegate来加载
            labcb(sceneName, bundleName);
        }
    }
    /// <summary>
    /// 加载AB包(供上层提供的API)
    /// </summary>
    /// <param name="bundleName">包名</param>
    /// <param name="lp">加载进度回调</param>
    /// <param name="labcb">开启协程的回调</param>
    public void LoadAB(string bundleName, LoadProgress lp, LoadAssetBundleCallBack labcb)
    {
        if (!relationDic.ContainsKey(bundleName))
        {
            AssetBundleRelation abRelation = new AssetBundleRelation(bundleName, lp);

            relationDic.Add(bundleName, abRelation);

            if (labcb != null)
            {
                labcb(sceneName, bundleName);                 // 回调给上层,上层收到这个回调,就开始启用协程加载这个AB包
            }
        }
        else
        {
            Debug.Log("The LoadHelper have Contains the bundleName  :" + bundleName);
        }
    }
Beispiel #13
0
 /// <summary>
 /// 添加加载AB包协程的对外接口
 /// </summary>
 public void LoadAssetBundle(string abname, LoadProgress lp, LoadAssetBundleCallBack labcb)
 {
     //如果这个包已经被加载了 输出一条提示信息
     if (NameAndBundleDict.ContainsKey(abname))
     {
         Debug.LogWarning("此包已经被加载了 : " + abname);
     }
     else
     {
         //没有被加载过  则进行加载
         AssetBundleRelation abrelation = new AssetBundleRelation(abname, lp);
         //将AB包和其对应的依赖关系保存到字典中
         NameAndBundleDict.Add(abname, abrelation);
         //开始进行加载
         //如果这个类继承了Mono的话,我们是通过开启协程来加载的。如 : StartCoroutine("Load",abname);
         //现在我们不想继承Mono,又想加载AB包的话,我们可以通过一个回调函数,把参数传递给上层,让上层来进行处理
         labcb(m_sceneName, abname);
     }
 }
 /// <summary>
 /// 加载依赖项的资源包
 /// </summary>
 /// <param name="bundleName"></param>
 /// <param name="referenceBundleName"></param>
 /// <param name="lp"></param>
 /// <returns></returns>
 private IEnumerator LoadDependence(string bundleName, string referenceBundleName, LoadProgress lp)
 {
     //当前资源已经加载,直接添加被依赖关系
     if (nameBundleDict.ContainsKey(bundleName))
     {
         AssetBundleRelation assetBundleRelation = nameBundleDict[bundleName];
         assetBundleRelation.AddReference(referenceBundleName);
         yield return(null);
     }
     //没有被加载,创建一个新的并加入字典
     else
     {
         AssetBundleRelation assetBundleRelation = new AssetBundleRelation(bundleName, lp);
         assetBundleRelation.AddReference(referenceBundleName);
         nameBundleDict.Add(bundleName, assetBundleRelation);
         //加载这个依赖项资源包
         yield return(Load(bundleName));
     }
 }
    /// <summary>
    /// 卸载包
    /// </summary>
    public void UnloadAssetBundle(string path, bool isBundleName = false)
    {
        //获取包名
        string bundleName;

        if (isBundleName)
        {
            bundleName = path;
        }
        else
        {
            bundleName = GetBundleName(path);
        }
        //安全校验
        if (!nameBundleDict.ContainsKey(bundleName))
        {
            Debug.Log(bundleName + "不在Dictionary中,可能未被加载或已被卸载");
            return;
        }
        AssetBundleRelation relation = nameBundleDict[bundleName];
        //如果该包被其他包依赖则无法卸载
        int referencesLength = relation.GetAllReferences().Length;

        if (referencesLength > 0)
        {
            Debug.LogError(string.Format(
                               "{0}正被{1}个包所依赖,无法卸载", bundleName, referencesLength));
            return;
        }
        //卸载该包
        relation.UnloadAssetBundle();
        nameBundleDict.Remove(bundleName);
        //移除依赖的包的被依赖关系,如果依赖的包不再被任何包依赖,也卸载
        string[] depedencies = relation.GetAllDependences();
        foreach (string depedence in depedencies)
        {
            nameBundleDict[depedence].RemoveReference(bundleName);
            if (nameBundleDict[depedence].GetAllReferences().Length <= 0)
            {
                UnloadAssetBundle(depedence, true);
            }
        }
    }
    /// <summary>
    /// 加载包
    /// </summary>
    /// <param name="bundleName"></param>
    /// <returns></returns>
    public IEnumerator Load(string bundleName)
    {
        while (!AssetBundleManifestLoader.Instance.Finish)
        {
            yield return(null);
        }
        AssetBundleRelation assetBundleRelation = nameBundleDict[bundleName];

        //获取当前资源包的所有依赖关系
        string[] dependenceBundles = AssetBundleManifestLoader.Instance.GetDependencies(bundleName);
        //添加对应的依赖关系
        foreach (var dependenceBundleName in dependenceBundles)
        {
            assetBundleRelation.AddDependence(dependenceBundleName);
            //添加被依赖关系并加载依赖项(必须先加载依赖项)
            yield return(LoadDependence(dependenceBundleName, bundleName, assetBundleRelation.LoadProgess));
        }
        //开始加载
        yield return(assetBundleRelation.Load());
    }
    /// <summary>
    /// 加载资源包
    /// </summary>
    /// <param name="bundleName"></param>
    /// <param name="lp"></param>
    /// <param name="labcb"></param>
    public void LoadAssetBundle(string bundleName, LoadProgress lp, LoadAssetBundleCallback labcb)
    {
        //如果这个包已经被加载了 就提示
        if (nameBundleDict.ContainsKey(bundleName))
        {
            Debug.LogWarning("此包已经加载了 : " + bundleName);
            return;
        }
        else
        {
            //没有被加载
            AssetBundleRelation assetBundleRelation = new AssetBundleRelation(bundleName, lp);
            //保存到字典里面
            nameBundleDict.Add(bundleName, assetBundleRelation);
            //StartCoroutine("Load", bundleName);


            //开始加载
            labcb(sceneName, bundleName);
        }
    }
    /// <summary>
    /// 加载依赖的包
    /// </summary>
    /// <param name="bundleName">包名</param>
    /// <param name="referenceBundleName">被依赖的包名</param>
    /// <param name="lp">进度回调</param>
    /// <returns></returns>
    private IEnumerator LoadDependence(string bundleName, string referenceBundleName, LoadProgress lp)
    {
        if (nameBundleDict.ContainsKey(bundleName))
        {
            //已经加载过 就直接添加他的被依赖关系
            AssetBundleRelation assetBundleRelation = nameBundleDict[bundleName];
            //添加这个包的被依赖关系
            assetBundleRelation.AddReference(referenceBundleName);
        }
        else
        {
            //没有加载过 就创建一个新的
            AssetBundleRelation assetBundleRelation = new AssetBundleRelation(bundleName, lp);
            //添加这个包的被依赖关系
            assetBundleRelation.AddReference(referenceBundleName);
            //保存到字典里面
            nameBundleDict.Add(bundleName, assetBundleRelation);

            //开始加载这个依赖的包
            yield return(Load(bundleName));
        }
    }
Beispiel #19
0
    /// <summary>
    /// 加载依赖的包
    /// </summary>
    /// <param name="bundleName">包名</param>
    /// <param name="referenceBundleName">被依赖的包名</param>
    /// <param name="lp">进度回调</param>
    /// <returns></returns>
    private IEnumerator LoadDependence(string bundleName, string referenceBundleName, LuaFunction lp)
    {
        if (nameBundleDic.ContainsKey(bundleName))
        {
            // 已经加载过,就直接添加他的 "被" 依赖关系
            AssetBundleRelation assetBundleRelation = nameBundleDic[bundleName];
            // 添加这个包的 "被" 依赖关系
            assetBundleRelation.AddReference(bundleName);
        }
        else
        {
            // 没加载过 ,就创建一个新的
            AssetBundleRelation assetBundleRelation = new AssetBundleRelation(bundleName, lp);
            // 添加这个包的 "被" 依赖关系
            assetBundleRelation.AddReference(referenceBundleName);
            // 保存到字典里
            nameBundleDic.Add(bundleName, assetBundleRelation);

            // 开始加载这个依赖的包
            yield return(Load(bundleName));
        }
    }
Beispiel #20
0
    /// <summary>
    /// 加载AB包
    /// </summary>
    /// <returns></returns>
    public IEnumerator Load(string assetbundlename)
    {
        //AssetBundleManifest文件加载完成后才能进行AssetBundle的加载操作
        while (!AssetBundleManifestLoader.instance.Finish)
        {
            yield return(null);
        }

        AssetBundleRelation assetBundleRelation = NameAndBundleDict[assetbundlename];

        //先获取这个包的所有依赖关系
        string[] dependencieBundles = AssetBundleManifestLoader.instance.GetDependencies(assetbundlename);
        //添加它的依赖关系
        foreach (var dependencieBundleName in dependencieBundles)
        {
            assetBundleRelation.AddDependencie(dependencieBundleName);
            //加载这个包的所有依赖关系(注意这里的参数)
            yield return(LoadDependencie(dependencieBundleName, assetbundlename, assetBundleRelation.lp));
        }
        //开始加载某个AB包
        yield return(assetBundleRelation.Load());
    }
    /// <summary>
    /// 获取这个AB包下的单个资源
    /// </summary>
    public UnityEngine.Object LoadAsset(string bundleName, string assetName)    // scene1/test.prefab
    {
        // 先看缓存的有没有
        if (resObj.ContainsKey(bundleName))
        {
            AssetResObj   tmpRes  = resObj[bundleName];
            List <Object> objList = tmpRes.GetResObj(assetName);
            if (objList != null)
            {
                return(objList[0]);
            }
        }

        // 缓存的没有,或者缓存里没有我们需要的Object, 就去底层加载一次
        if (!relationDic.ContainsKey(bundleName))
        {
            Debug.LogError("The loadHelper Not ContainsKey :" + bundleName);
            return(null);
        }

        AssetBundleRelation abRela = relationDic[bundleName];
        Object obj = abRela.LoadAsset(assetName);

        AssetObj tmpObj = new AssetObj(obj);

        if (resObj.ContainsKey(bundleName))         // 如果缓存里有这个bundle包,获取value,加到value里去;
        {
            AssetResObj tmpResObj = resObj[bundleName];
            tmpResObj.AddResObj(assetName, tmpObj);
        }
        else         // 缓存里没有这个 bundle包,直接加进去
        {
            AssetResObj assetRes = new AssetResObj(assetName, tmpObj);
            resObj.Add(bundleName, assetRes);
        }

        return(obj);
    }
    /// <summary>
    /// 获取这个AB包下的多个资源
    /// </summary>
    public Object[] LoadAssetWithSubAssets(string bundleName, string assetName)
    {
        if (resObj.ContainsKey(bundleName))
        {
            AssetResObj   tmpRes  = resObj[bundleName];
            List <Object> objList = tmpRes.GetResObj(assetName);
            if (objList != null)
            {
                return(objList.ToArray());
            }
        }

        if (!relationDic.ContainsKey(bundleName))
        {
            Debug.LogError("The loadHelper Not ContainsKey :" + bundleName);
            return(null);
        }

        AssetBundleRelation abRela = relationDic[bundleName];

        Object[] tmpObjs = abRela.LoadAssetWithSubAssets(assetName);

        AssetObj assetObj = new AssetObj(tmpObjs);

        if (resObj.ContainsKey(bundleName))
        {
            AssetResObj tmpAsset = resObj[bundleName];
            tmpAsset.AddResObj(assetName, assetObj);
        }
        else
        {
            AssetResObj assetRes = new AssetResObj(assetName, assetObj);
            resObj.Add(bundleName, assetRes);
        }


        return(tmpObjs);
    }
    /// <summary>
    /// 协程加载 AssetBundle
    /// </summary>
    /// <param name="bundleName"></param>
    /// <returns></returns>
    public IEnumerator LoadAssetBundle(string bundleName)
    {
        while (!LoadManifest.Instance.IsLoadFinished())         //必须先加载 AssetBundleMainfest 文件
        {
            yield return(null);
        }

        AssetBundleRelation relation = relationDic[bundleName];

        string[] dependences = LoadManifest.Instance.GetAllDependencies(bundleName); // 获取所有的依赖关系

        relation.SetDependenceBundle(dependences);                                   // 获取依赖关系之后,抓紧记录下来

        for (int i = 0; i < dependences.Length; i++)
        {
            // 先去获取这个AB包的所有的依赖关系,之后再加载这个AB包

            yield return(LoadAssetBundleDependences(dependences[i], bundleName, relation.GetProgress));
        }

        // 开启协程,开始加载
        yield return(relation.LoadAssetBundle());
    }
Beispiel #24
0
    /// <summary>
    /// 加载某个AB包依赖的包
    /// </summary>
    /// <param name="assetbundlename">需要加载的AB包所依赖的包</param>
    /// <param name="referencebundlename">AB包自身</param>
    /// <param name="lp"></param>
    /// <returns></returns>
    private IEnumerator LoadDependencie(string assetbundlename, string referencebundlename, LoadProgress lp)
    {
        //判断是否已经加载过它依赖的包
        if (NameAndBundleDict.ContainsKey(assetbundlename))
        {
            //已经加载过 直接添加他的被依赖关系
            AssetBundleRelation abrelation = NameAndBundleDict[assetbundlename];

            //添加这个包的被依赖关系
            abrelation.AddReference(referencebundlename);
        }
        else
        {
            //没有加载过 就创建一个新的依赖关系
            AssetBundleRelation abrelation = new AssetBundleRelation(assetbundlename, lp);
            //添加这个包的被依赖关系
            abrelation.AddReference(referencebundlename);
            //将AB包名和其对应的AB包的依赖关系保存到字典中
            NameAndBundleDict.Add(assetbundlename, abrelation);
            //加载依赖的包
            yield return(Load(assetbundlename));
        }
    }
    /// <summary>
    /// 加载目标AB包 依赖的包 (b被a依赖,要加载a必须先加载b)
    /// </summary>
    /// <param name="bundleName">目标AB包所依赖的包的名字 b</param>
    /// <param name="referName">目标AB包 a</param>
    /// <param name="lp">加载进度</param>
    /// <returns></returns>
    private IEnumerator LoadAssetBundleDependences(string bundleName, string referName, LoadProgress lp)
    {
        if (!relationDic.ContainsKey(bundleName))
        {
            AssetBundleRelation abRelation = new AssetBundleRelation(bundleName, lp);

            if (referName != null)
            {
                abRelation.AddReferBundle(referName);
            }

            relationDic.Add(bundleName, abRelation);

            yield return(LoadAssetBundle(bundleName));
        }
        else         // 已经在加载队列里了 , 只需要记录一下被依赖关系就行
        {
            if (referName != null)
            {
                AssetBundleRelation abRelation = relationDic[bundleName];
                abRelation.AddReferBundle(referName);
            }
        }
    }
    /// <summary>
    /// 加载包[没有继承MOno,没法走协程,只能通过上面的方法加载资源包]
    /// </summary>\
    ///
    /// <param name="bundleName"></param>
    /// <returns></returns>
    public IEnumerator Load(string bundleName)
    {
        while (!ManifestLoader.Instance.IsFinish)
        {
            yield return(null);
        }

        AssetBundleRelation assetBundleRelation = nameBundleDic[bundleName];

        // 先获取这个包所有的依赖关系
        string[] dependenceBundles = ManifestLoader.Instance.GetDependence(bundleName);

        // 添加这个包的所有依赖关系
        foreach (string item in dependenceBundles)
        {
            assetBundleRelation.AddDependence(item);

            // 加载这个包的所有依赖关系
            yield return(LoadDependence(item, bundleName, assetBundleRelation.Lp));
        }

        // 开始加载这个包
        yield return(assetBundleRelation.Load());
    }