Unity简单资源加载框架(二):打包AB包一(开始打包AB包)

一:目录结构:

 二:打包代码:

/****************************************************
    文件:ABEditor.cs
	作者:赵深圳
    邮箱: 1329346609@qq.com
    日期:2022/5/7 15:54:51
	功能:AB包编辑器
*****************************************************/

using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;

/// <summary>
/// 生成AB包编辑器工具
/// </summary>
public class ABEditor  
{
    /// <summary>
    /// 需要打包的资源所在目标
    /// </summary>
    public static string rootPath = Application.dataPath + "/1SimpleABManager/Download";

    /// <summary>
    /// AB包输出路径
    /// </summary>
    public static string outABPath=Application.streamingAssetsPath;

    /// <summary>
    /// 记录每个资源所有在的AB包文件
    /// key为路径
    /// value为AB包名字
    /// </summary>
    public static Dictionary<string, string> m_Asset2BundleDic = new Dictionary<string, string>();
    /// <summary>
    /// 所有需要打包的资源对应的AssetBundleBuild
    /// 一个文件对应一个AssetBundleBuild
    /// </summary>
    public static List<AssetBundleBuild> m_AssetBundleBuildLst = new List<AssetBundleBuild>();

    /// <summary>
    /// 打包AB包
    /// </summary>
    [MenuItem("ZSTools/BuildABTools")]
    public static void BuildAssetBundle() 
    {
        Log.ZSZLog(LogCategory.Normal,"开始打AB包!!!");

        if (Directory.Exists(outABPath))
        {
            Directory.Delete(outABPath,true);
        }

        //打包所有的文件夹

        DirectoryInfo rootDir = new DirectoryInfo(rootPath);
        //获取资源路径下所有的文件夹
        //这里获取的文件夹,不包括自身,所有在资源所在文件(Application.dataPath + "/1SimpleABManager/Download")下不要放打包资源,会打包不到
        //放在资源所在文件所在的下一级文件,如果需要放在资源文件夹中,请把自身加入到数组中
        DirectoryInfo[] dirs = rootDir.GetDirectories();     
        foreach (DirectoryInfo moduleDir in dirs)
        {
            string moduleName = moduleDir.Name;
            m_AssetBundleBuildLst.Clear();
            m_Asset2BundleDic.Clear();
            ScanChildDireations(moduleDir);
            AssetDatabase.Refresh();
            string moduleOutPath = outABPath + "/" + moduleName;
            if (Directory.Exists(moduleOutPath))
            {
                Directory.Delete(moduleOutPath, true);
            }
            Directory.CreateDirectory(moduleOutPath);
            //打包
            BuildPipeline.BuildAssetBundles(moduleOutPath,m_AssetBundleBuildLst.ToArray(),BuildAssetBundleOptions.None,EditorUserBuildSettings.activeBuildTarget);

            AssetDatabase.Refresh();
        }

        Log.ZSZLog(LogCategory.Normal,"打包AB包结束!!!");
    }

    /// <summary>
    /// 根据指定的文件夹
    /// 将这个文件夹下所有的一级子文件夹打成一个AB包
    /// 递归这个文件夹下所有的子文件夹
    /// </summary>
    /// <param name="directoryInfo"></param>
    private static void ScanChildDireations(DirectoryInfo directoryInfo)
    {
        if (directoryInfo.Name.EndsWith("CSProject")|| directoryInfo.Name.EndsWith(".cs"))
        {
            return;
        }

        ScanCurrDirectory(directoryInfo);
        
        //递归当前路径下的所有文件夹
        //使所有的一级文件夹为一个模块
        DirectoryInfo[] dirs = directoryInfo.GetDirectories();
        foreach (var item in dirs)
        {
            ScanChildDireations(item);
        }
    }

    /// <summary>
    /// 遍历当前路径下的文件,并打成一个AB包
    /// </summary>
    /// <param name="directoryInfo"></param>
    private static void ScanCurrDirectory(DirectoryInfo directoryInfo) 
    {
        List<string> assetNameLst = new List<string>();

        //获取指定文件夹中所有的文件
        FileInfo[] fileInfoArr = directoryInfo.GetFiles();

        foreach (FileInfo fileInfo in fileInfoArr)
        {
            if (fileInfo.FullName.EndsWith(".meta"))
            {
                continue;
            }
            //assetName的格式类似 "Assets/GAssets/Launch/Sphere.prefab"
            string assetName = fileInfo.FullName.Substring(Application.dataPath.Length-"Assets".Length).Replace('\\','/');
            assetNameLst.Add(assetName);
        }

        if (assetNameLst.Count > 0)
        {
            string assetBundleName = directoryInfo.FullName.Substring(Application.dataPath.Length + 1).Replace('\\','_').ToLower();
            AssetBundleBuild build = new AssetBundleBuild();
            build.assetBundleName = assetBundleName;
            build.assetNames = new string[assetNameLst.Count];
            for (int i = 0; i < assetNameLst.Count; i++)
            {
                build.assetNames[i] = assetNameLst[i];
                m_Asset2BundleDic.Add(assetNameLst[i],assetBundleName);
            }
            m_AssetBundleBuildLst.Add(build);
        }
    } 

}

Unity是一款用于游戏开发的引擎,它支持将游戏资源打包AB中。AB即Asset Bundle,它的作用是将游戏资源按照一定规则打包成一个文件,方便进行异步加载和更新。 Unity提取AB资源需要以下步骤: 第一步,将AB导入Unity项目中。 将AB放置到Unity项目中的Assets文件夹中,然后在“Project”面板中选择该文件夹,可以看到AB被放在其中。此时可以在左下角的“Inspector”面板中看到该AB的信息。 第步,加载AB。 编写脚本,在脚本中调用AssetBundle.LoadFromFile()方法加载AB文件,并将加载后的AssetBundle对象保存下来。代码如下: AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.dataPath + "/AssetBundles/myassetbundle"); 第三步,读取AB中的资源。 使用AssetBundle对象调用LoadAsset()或LoadAssetAsync()方法,可以加载AB中的资源。这里需要注意,要使用资源的完整名称(括路径和文件名)来加载,代码如下: GameObject prefab = assetBundle.LoadAsset<GameObject>("assets/prefabs/myPrefab.prefab"); 第四步,卸载AB。 使用AssetBundle.Unload()或AssetBundle.UnloadAsync()方法可以卸载AB。卸载后,AB中的资源也会被释放。 以上就是Unity提取AB资源的基本步骤。需要注意的是,在使用AB时,不仅要注意资源的路径和名称,还要考虑到加载和卸载的时机,以达到最优效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值