//加载一个依赖包
    public static Bundle LoadRelyBundle(string relyBundleName)
    {
        Bundle tmp = null;

        string[] AllDependencies = AssetsManifestManager.GetAllDependencies(relyBundleName);

        //加载依赖的依赖包
        for (int i = 0; i < AllDependencies.Length; i++)
        {
            LoadRelyBundle(AllDependencies[i]);
        }

        if (s_bundles.ContainsKey(relyBundleName))
        {
            tmp = s_bundles[relyBundleName];
            tmp.relyCount++;

            if (relyBundleName.ToLower().Contains("_res/shader/card_rely"))
            {
                Debug.LogWarning("LoadRelyBundle " + relyBundleName + " relyCount :" + s_bundles[relyBundleName].relyCount);
            }
        }
        else
        {
            string path = GetBundlePath(relyBundleName);
            tmp = AddRelyBundle(relyBundleName, AssetBundle.LoadFromFile(path));
        }

        return(tmp);
    }
Exemple #2
0
    static Bundle AddRelyBundle(string relyBundleName, AssetBundle asset)
    {
        Debug.LogWarning("Load ab====>" + relyBundleName);
        Bundle tmp = new Bundle();

        tmp.relyCount       = 1;
        tmp.bundle          = asset;
        tmp.mainAsset       = asset.mainAsset;
        tmp.allAsset        = asset.LoadAllAssets();
        tmp.allDependencies = AssetsManifestManager.GetAllDependencies(relyBundleName);

        if (s_bundles.ContainsKey(relyBundleName))
        {
            s_bundles[relyBundleName] = tmp;
        }
        else
        {
            s_bundles.Add(relyBundleName, tmp);
        }

        if (tmp.bundle == null)
        {
            Debug.LogError("AddRelyBundle: " + relyBundleName + " dont exist!");
        }

        return(tmp);
    }
Exemple #3
0
    static Bundle AddBundle(string bundleName, AssetBundle asset)
    {
        //Debug.Log("AddBundle " + bundleName);
        Debug.LogWarning("Load ab====>" + bundleName);
        Bundle bundleTmp = new Bundle();

        string[] AllDependencies = AssetsManifestManager.GetAllDependencies(bundleName);

        if (s_bundles.ContainsKey(bundleName))
        {
            s_bundles[bundleName] = bundleTmp;
        }
        else
        {
            s_bundles.Add(bundleName, bundleTmp);
        }

        bundleTmp.allDependencies = AllDependencies;

        if (asset != null)
        {
            bundleTmp.bundle      = asset;
            bundleTmp.bundle.name = bundleName;
            bundleTmp.mainAsset   = asset.mainAsset;
            bundleTmp.allAsset    = bundleTmp.bundle.LoadAllAssets();

            //延迟卸载资源,因为unity的资源卸载有时会异步
            Timer.DelayCallBack(5, (obj) => {
                if (bundleTmp.bundle != null)
                {
                    bundleTmp.bundle.Unload(false);
                }
            });

            //如果有缓存起来的回调这里一起回调
            if (LoadAsyncDict.ContainsKey(bundleName))
            {
                try
                {
                    LoadAsyncDict[bundleName](LoadState.CompleteState, bundleTmp.GetAeests(null));
                }
                catch (Exception e)
                {
                    Debug.Log("LoadAsync AddBundle " + e.ToString());
                }
            }
        }
        else
        {
            Debug.LogError("AddBundle: " + bundleName + " dont exist!");
        }

        return(bundleTmp);
    }
    static Dictionary <string, Bundle> s_bundles = new Dictionary <string, Bundle>();     //所有包

#if !UNITY_WEBGL
    /// <summary>
    /// 同步加载一个bundles
    /// </summary>
    /// <param name="name">bundle名</param>
    public static Bundle LoadBundle(string bundleName)
    {
        string path = GetBundlePath(bundleName);

        string[] AllDependencies = AssetsManifestManager.GetAllDependencies(bundleName);

        //加载依赖包
        for (int i = 0; i < AllDependencies.Length; i++)
        {
            //Debug.Log("" + AllDependencies[i] + " -> " + bundleName);

            LoadRelyBundle(AllDependencies[i]);
        }

        return(AddBundle(bundleName, AssetBundle.LoadFromFile(path)));
    }
    /// <summary>
    /// 卸载依赖包
    /// </summary>
    /// <param name="relyBundleName"></param>
    public static void UnLoadRelyBundle(string relyBundleName)
    {
        if (relyBundleName == "")
        {
            return;
        }

        //if (relyBundleName.Contains("res_models"))
        //{
        //    Debug.Log("UnLoadRelyBundle " + relyBundleName);
        //}

        string[] AllDependencies = AssetsManifestManager.GetAllDependencies(relyBundleName);

        //卸载依赖的依赖包
        for (int i = 0; i < AllDependencies.Length; i++)
        {
            UnLoadRelyBundle(AllDependencies[i]);
        }

        if (s_bundles.ContainsKey(relyBundleName))
        {
            if (relyBundleName.ToLower().Contains("card_black"))
            {
                Debug.LogWarning("UnLoadRelyBundle " + relyBundleName + " relyCount :" + s_bundles[relyBundleName].relyCount);
            }

            s_bundles[relyBundleName].relyCount--;

            if (s_bundles[relyBundleName].relyCount <= 0)
            {
                if (UnloadBundle(s_bundles[relyBundleName]))
                {
                    s_bundles.Remove(relyBundleName);
                }
            }
        }
        else
        {
            Debug.LogError("UnLoadRelyBundle: " + relyBundleName + " dont exist !");
        }
    }
    static Bundle AddRelyBundle(string relyBundleName, AssetBundle asset)
    {
        Bundle tmp = new Bundle();

        tmp.bundle          = asset;
        tmp.mainAsset       = asset.mainAsset;
        tmp.allAsset        = asset.LoadAllAssets();
        tmp.allDependencies = AssetsManifestManager.GetAllDependencies(relyBundleName);

        //if(relyBundleName.Contains("res_models"))
        //{
        //    for (int i = 0; i < tmp.allAsset.Length; i++)
        //    {
        //        Debug.Log("allAsset " + tmp.allAsset[i]);
        //    }
        //}

        if (s_bundles.ContainsKey(relyBundleName))
        {
            s_bundles[relyBundleName] = tmp;
            tmp.relyCount++;
        }
        else
        {
            s_bundles.Add(relyBundleName, tmp);
            tmp.relyCount = 1;
        }

        if (relyBundleName.ToLower().Contains("card_black"))
        {
            Debug.LogWarning("AddRelyBundle " + relyBundleName + " relyCount :" + s_bundles[relyBundleName].relyCount);
        }

        if (tmp.bundle == null)
        {
            Debug.LogError("AddRelyBundle: " + relyBundleName + " dont exist!");
        }

        return(tmp);
    }
Exemple #7
0
    public static Dictionary <string, Bundle> s_bundles = new Dictionary <string, Bundle>();     //所有包

#if !UNITY_WEBGL
    /// <summary>
    /// 同步加载一个bundles
    /// </summary>
    /// <param name="name">bundle名</param>
    public static Bundle LoadBundle(string bundleName)
    {
        string path = GetBundlePath(bundleName);

        string[] AllDependencies = AssetsManifestManager.GetAllDependencies(bundleName);

        //加载依赖包
        for (int i = 0; i < AllDependencies.Length; i++)
        {
            LoadRelyBundle(AllDependencies[i]);
        }

        if (!AssetsManifestManager.GetIsDependencies(bundleName))
        {
            return(AddBundle(bundleName, AssetBundle.LoadFromFile(path)));
        }
        //如果这个包被别人依赖,则当做依赖包处理
        else
        {
            return(LoadRelyBundle(bundleName));
        }
    }
Exemple #8
0
    /// <summary>
    /// 异步加载一个bundle
    /// </summary>
    /// <param name="bundleName">bundle名</param>
    public static void LoadBundleAsync(string bundleName, BundleLoadCallBack callBack)
    {
        string[] AllDependencies = AssetsManifestManager.GetAllDependencies(bundleName);

        string path = GetBundlePath(bundleName);

        LoadState state = new LoadState();

        int LoadCount = 0;

        if (AllDependencies.Length > 0)
        {
            //先加载依赖包
            for (int i = 0; i < AllDependencies.Length; i++)
            {
                if (AllDependencies[i] != "")
                {
                    LoadRelyBundleAsync(AllDependencies[i], (LoadState relyLoadState, Bundle RelyBundle) =>
                    {
                        if (RelyBundle != null && relyLoadState.isDone)
                        {
                            LoadCount++;
                            state.progress += 1 / ((float)AllDependencies.Length + 1);
                        }

                        //所有依赖包加载完毕加载资源包
                        if (LoadCount == AllDependencies.Length)
                        {
                            ResourceIOTool.AssetsBundleLoadAsync(path, (LoadState bundleLoadState, AssetBundle bundle) =>
                            {
                                if (bundleLoadState.isDone)
                                {
                                    callBack(LoadState.CompleteState, AddBundle(bundleName, bundle));
                                }
                                else
                                {
                                    state.progress += bundleLoadState.progress / ((float)AllDependencies.Length + 1);
                                    callBack(state, null);
                                }
                            });
                        }
                        else
                        {
                            callBack(state, null);
                        }
                    });
                }
            }
        }
        else
        {
            ResourceIOTool.AssetsBundleLoadAsync(path, (LoadState bundleLoadState, AssetBundle bundle) =>
            {
                if (bundleLoadState.isDone)
                {
                    callBack(LoadState.CompleteState, AddBundle(bundleName, bundle));
                }
                else
                {
                    state.progress += bundleLoadState.progress / ((float)AllDependencies.Length + 1);
                    callBack(state, null);
                }
            });
        }
    }