Unity简单资源加载框架(六):通过AB配置文件构建资源依赖树

一.增加BundleRef类

/****************************************************
    文件:BundleRef.cs
	作者:赵深圳
    邮箱: 1329346609@qq.com
    日期:2022/5/9 14:11:13
	功能:内存中的一个Bundle对象
*****************************************************/

using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 内存中的一个Bundle对象
/// </summary>
public class BundleRef  
{
    /// <summary>
    /// 这个bundle的配置信息
    /// </summary>
    public BundleInfo bundleInfo;

    /// <summary>
    /// 加载到内存中的bundle对象
    /// </summary>
    public AssetBundle bundle;

    /// <summary>
    /// 这个bundleRef被那些资源AssetRef所依赖
    /// </summary>
    public List<AssetRef> chilren;

    public BundleRef(BundleInfo bundle) 
    {
        this.bundleInfo = bundle;
    }
}


一.增加AssetRef类

/****************************************************
    文件:AssetRef.cs
	作者:赵深圳
    邮箱: 1329346609@qq.com
    日期:2022/5/9 14:11:25
	功能:内存中单个资源对象
*****************************************************/

using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 内存中单个资源对象
/// </summary>
public class AssetRef
{
    /// <summary>
    /// 这个资源的配置信息
    /// </summary>
    public AssetInfo assetInfo;

    /// <summary>
    /// 这个资源所属的BundleRef对象
    /// </summary>
    public BundleRef bundleRef;

    /// <summary>
    /// 这个资源所依赖的BundleRef对象列表
    /// </summary>
    public BundleRef[] dependencies;

    /// <summary>
    /// 从bundle文件中提取出来的资源对象
    /// </summary>
    public object asset;

    /// <summary>
    /// 这个资源是否是Prefab
    /// </summary>
    public bool isGameObject;

    /// <summary>
    /// 这个资源被哪些GameObject所依赖
    /// </summary>
    public List<GameObject> children;

    public AssetRef(AssetInfo asset)
    {
        this.assetInfo = asset;
    }
}

3.修改AssetLoader

/****************************************************
    文件:AssetLoader.cs
	作者:赵深圳
    邮箱: 1329346609@qq.com
    日期:2022/5/9 10:39:33
	功能:资源加载器
*****************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;

/// <summary>
/// 资源加载器
/// </summary>
public class AssetLoader : Singleton<AssetLoader>
{
    /// <summary>
    /// 平台对应的只读路径下的资源
    /// key 模块名字
    /// value 模块下所有的资源
    /// </summary>
    public Dictionary<string, Hashtable> base2Assets;


    public AssetLoader()
    {
        base2Assets = new Dictionary<string, Hashtable>();
    }

    /// <summary>
    /// 加载模块对应的全局AssetBundle资源管理文件
    /// </summary>
    /// <param name="moduleConfig">模块名字</param>
    /// <param name="onCompelte">加载完成后的回调</param>
    public async Task<ModuleABConfig> LoadAssetBundleConfig(string moduleName)
    {
#if UNITY_EDITOR
        if (GlobalConfig.BundleMode == false)
        {
            return null;
        }
        else
        {
            return await LoadAssetBundleConfig_Runtime(moduleName);
        }
#else
         return await LoadAssetBundleConfig_Runtime(moduleName);
#endif
    }

    /// <summary>
    /// 加载本地的模块的AB配置文件
    /// </summary>
    /// <param name="moudleName">模块的名字</param>
    /// <returns></returns>
    public async Task<ModuleABConfig> LoadAssetBundleConfig_Runtime(string moudleName)
    {
        string url = Application.streamingAssetsPath + "/" + moudleName + "/" + moudleName.ToLower() + ".json";

        UnityWebRequest request = UnityWebRequest.Get(url);

        await request.SendWebRequest();

        if (string.IsNullOrEmpty(request.error))
        {
            return LitJson.JsonMapper.ToObject<ModuleABConfig>(request.downloadHandler.text);
        }
        return null;
    }

    /// <summary>
    /// 根据模块的json文件创角内存中资源管理器
    /// </summary>
    /// <param name="moduleABConfig"></param>
    /// <returns></returns>
    public Hashtable ConfigAssembly(ModuleABConfig moduleABConfig) 
    {
        Dictionary<string, BundleRef> name2BundleRef = new Dictionary<string, BundleRef>();
        foreach (KeyValuePair<string,BundleInfo> keyValue in moduleABConfig.BundleInfoDic) 
        {
            string bundleName = keyValue.Key;
            BundleInfo bundleInfo = keyValue.Value;

            name2BundleRef[bundleName] = new BundleRef(bundleInfo);
        }

        Hashtable path2AssetRef = new Hashtable();

        for (int i = 0; i < moduleABConfig.AssetInfoArr.Length; i++)
        {
            AssetInfo assetInfo = moduleABConfig.AssetInfoArr[i];
            AssetRef assetRef = new AssetRef(assetInfo);
            assetRef.bundleRef = name2BundleRef[assetInfo.bundle_name];
            int count = assetInfo.dependenciesLst.Count;
            assetRef.dependencies = new BundleRef[count];

            for (int index = 0; index < count; index++)
            {
                string bundleName = assetInfo.dependenciesLst[index];
                assetRef.dependencies[index] = name2BundleRef[bundleName];
            }
            path2AssetRef.Add(assetInfo.asset_path,assetRef);
        }
        return path2AssetRef;
    }


}

4.修改ModuleManager

/****************************************************
    文件:ModuleManager.cs
	作者:赵深圳
    邮箱: 1329346609@qq.com
    日期:2022/5/9 10:40:6
	功能:模块管理器
*****************************************************/

using System;
using System.Collections;
using System.Threading.Tasks;
using UnityEngine;

/// <summary>
/// 模块管理器
/// </summary>
public class ModuleManager : Singleton<ModuleManager> 
{
    /// <summary>
    /// 加载一个模块 唯一对外API函数
    /// </summary>
    /// <param name="mooduleConfig">模块的配置文件</param>
    public async Task<bool> Load(ModuleConfig moduleConfig) 
    {
        if (GlobalConfig.HotUpdate == false)
        {
            if (GlobalConfig.BundleMode == false)
            {
                return true;
            }
            else
            {
                ModuleABConfig moduleABConfig = await AssetLoader.Instance.LoadAssetBundleConfig(moduleConfig.moduleName);
                if (moduleABConfig != null)
                {
                    Log.ZSZLog(LogCategory.Resource,"模块包含的AB包总数量:"+ moduleABConfig.BundleInfoDic.Count);
                    Hashtable path2AssetRef = AssetLoader.Instance.ConfigAssembly(moduleABConfig);
                    AssetLoader.Instance.base2Assets.Add(moduleConfig.moduleName,path2AssetRef);
                    return true;
                }
                else 
                {
                    return false;
                }
            }
        }
        else 
        {
            return await Downloader.Instance.Download(moduleConfig);
        }
    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值