Пример #1
0
    /// <summary>
    /// 异步加载一个bundle
    /// </summary>
    /// <param name="bundleName">bundle名</param>
    public static void LoadBundleAsync(string bundleName, BundleLoadCallBack callBack)
    {
        ResourcesConfig configTmp = ResourcesConfigManager.GetBundleConfig(bundleName);

        if (configTmp == null)
        {
            Debug.LogError("LoadBundleAsync: " + bundleName + " dont exist!");
            return;
        }

        string path = GetBundlePath(configTmp);

        LoadState state = new LoadState();
        Dictionary <string, LoadState> loadStateDict = new Dictionary <string, LoadState>();

        //先加载依赖包
        for (int i = 0; i < configTmp.relyPackages.Length; i++)
        {
            LoadRelyBundleAsync(configTmp.relyPackages[i], (LoadState relyLoadState, RelyBundle RelyBundle) =>
            {
                if (RelyBundle != null && relyLoadState.isDone)
                {
                    Debug.Log(RelyBundle.bundle.name);

                    loadStateDict.Add(RelyBundle.bundle.name, relyLoadState);
                    state.progress += 1 / ((float)configTmp.relyPackages.Length + 1);
                }

                //所有依赖包加载完毕加载资源包
                if (loadStateDict.Keys.Count == configTmp.relyPackages.Length)
                {
                    ResourceIOTool.AssetsBundleLoadAsync(path, (LoadState bundleLoadState, AssetBundle bundle) =>
                    {
                        if (bundleLoadState.isDone)
                        {
                            callBack(LoadState.CompleteState, AddBundle(bundleName, bundle));
                        }
                        else
                        {
                            state.progress += bundleLoadState.progress / ((float)configTmp.relyPackages.Length + 1);
                            callBack(state, null);
                        }
                    });
                }
                else
                {
                    callBack(state, null);
                }
            });
        }
    }
Пример #2
0
    static Dictionary <string, RelyBundle> s_relyBundle = new Dictionary <string, RelyBundle>(); //所有依赖包

    /// <summary>
    /// 同步加载一个bundles
    /// </summary>
    /// <param name="name">bundle名</param>
    public static Bundle LoadBundle(string bundleName)
    {
        ResourcesConfig configTmp = ResourcesConfigManager.GetBundleConfig(bundleName);

        string path = GetBundlePath(configTmp);

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

        return(AddBundle(bundleName, AssetBundle.LoadFromFile(path)));
    }
Пример #3
0
    static Bundle AddBundle(string bundleName, AssetBundle asset)
    {
        //Debug.Log("AddBundle " + bundleName);

        Bundle          bundleTmp = new Bundle();
        ResourcesConfig configTmp = ResourcesConfigManager.GetBundleConfig(bundleName);

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

        bundleTmp.bundleConfig = configTmp;

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

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

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

        return(bundleTmp);
    }
Пример #4
0
    public static void LoadAsync(string name, LoadCallBack callBack)
    {
        ResourcesConfig packData = ResourcesConfigManager.GetBundleConfig(name);

        if (packData == null)
        {
            return;
        }

        if (m_gameLoadType == ResLoadLocation.Resource)
        {
            ResourceIOTool.ResourceLoadAsync(packData.path, callBack);
        }
        else
        {
            AssetsBundleManager.LoadAsync(name, callBack);
        }
    }
Пример #5
0
    public static T Load <T>(string name) where T : UnityEngine.Object
    {
        ResourcesConfig packData = ResourcesConfigManager.GetBundleConfig(name);

        if (packData == null)
        {
            throw new Exception("Load Exception not find " + name);
        }

        if (m_gameLoadType == ResLoadLocation.Resource)
        {
            return(Resources.Load <T>(packData.path));
        }
        else
        {
            return(AssetsBundleManager.Load <T>(name));
        }
    }
Пример #6
0
    public static object Load(string name)
    {
        ResourcesConfig packData = ResourcesConfigManager.GetBundleConfig(name);

        if (packData == null)
        {
            throw new Exception("Load Exception not find " + name);
        }

        if (m_gameLoadType == ResLoadType.Resource)
        {
            return(Resources.Load(packData.path));
        }
        else
        {
            return(AssetsBundleManager.Load(name));
        }
    }
Пример #7
0
    static Bundle AddBundle(string bundleName, AssetBundle aess)
    {
        Bundle          bundleTmp = new Bundle();
        ResourcesConfig configTmp = ResourcesConfigManager.GetBundleConfig(bundleName);

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

        bundleTmp.bundleConfig = configTmp;

        if (aess != null)
        {
            bundleTmp.bundle      = aess;
            bundleTmp.bundle.name = bundleName;
            bundleTmp.mainAsset   = bundleTmp.bundle.mainAsset;
            bundleTmp.bundle.Unload(false);

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

        return(bundleTmp);
    }