打包到指定目录_二三言理清Untiy资源打包流程

本文介绍了Unity中AB资源打包的相关知识,包括按文件夹和文件打包的策略,建议根据实际使用场景进行打包,如每个UI界面单独打包,图片按功能打图集以减少内存占用。同时强调了减少资源冗余依赖的重要性,并提供了两种打包代码示例,一种是按文件名无脑打包,另一种是按项目需求定制打包。最后提到了通过搭建本地资源服务器进行加载。
摘要由CSDN通过智能技术生成

c11fa767ef8f11fd761f68ed48be09e5.png

简单讲一下AB资源打包相关的东东

a731a9f33519f8982fdcc858cbb35567.png
设置AssetBundle标签

AB的标签选中资源时就可以在右下角选择,屁股后面的None是后缀名,可以自定义。

我们可以按照文件夹来打,也可以逐个文件来搞,这个看项目的打包策略。

个人建议按照实际使用场景来打包,比如说每个UI界面单独打一个包,图片按功能打图集(包括Common),这样可以尽量减少游戏运行时的内存占用。此外要注意尽量减少资源间的冗余依赖,详情可以参考

Unity AssetBundle(1):Assets打包和依赖(Dependencies)理解​www.jianshu.com
df7181882e8f66150bad1182c7929e7e.png

接下来直接上打包代码,两种方式,一种是指定目录无脑按文件名打包(不推荐),一种是按照我们的设置进行打包(需要按照实际项目需求拓展,这里不做任何自动化相关的处理)

using System.IO;
using UnityEditor;
using UnityEngine;

public class AssetBundleBuilder : MonoBehaviour
{
    [MenuItem("HCResTool/ABBuild/AutoBuildABAsset")]
    static void buildABInfo()
    {
        //资源打包完成以后默认放到StreamingAsset下
        string assetBundlePath = Application.streamingAssetsPath;
        //指定的需要打包的资源目录地址
        string assetDir = Application.dataPath+"/Resources/UnPackRes";
        //清理之前打包的资源
        if (Directory.Exists(assetBundlePath))
        {
            Directory.Delete(assetBundlePath,true);
        }
        
        Directory.CreateDirectory(assetBundlePath);
        
        //刷新资源库
        AssetDatabase.Refresh();
        //清除所有的AssetBundleName
        ClearAssetBundlesName();
        
        //设置指定路径下所有需要打包的assetbundlename
        SetAssetBundlesName(assetDir);
        
        BuildPipeline.BuildAssetBundles(assetBundlePath, BuildAssetBundleOptions.None, EditorUserBuildSettings.activeBuildTarget);
        
        //刷新资源库
        AssetDatabase.Refresh();
    }

    [MenuItem("HCResTool/ABBuild/AutoBuildABAssetWithCustomTag")]
    static void buildABInfoWithCustomTag()
    {
        //资源打包完成以后默认放到StreamingAsset下
        string assetBundlePath = Application.streamingAssetsPath;
        //指定的需要打包的资源目录地址
        string assetDir = Application.dataPath+"/Resources/UnPackRes";
        //清理之前打包的资源
        if (Directory.Exists(assetBundlePath))
        {
            Directory.Delete(assetBundlePath,true);
        }
        
        Directory.CreateDirectory(assetBundlePath);
        
        
        BuildPipeline.BuildAssetBundles(assetBundlePath, BuildAssetBundleOptions.None, EditorUserBuildSettings.activeBuildTarget);
        
        //刷新资源库
        AssetDatabase.Refresh();  
    }

    /// <summary>
    /// 设置所有在指定路径下的AssetBundleName
    /// </summary>
    static void SetAssetBundlesName(string _assetsPath)
    {
        //先获取指定路径下的所有Asset,包括子文件夹下的资源
        DirectoryInfo dir = new DirectoryInfo(_assetsPath);  
        FileSystemInfo[] files = dir.GetFileSystemInfos(); //GetFileSystemInfos方法可以获取到指定目录下的所有文件以及子文件夹
 
        for (int i = 0; i < files.Length; i++)
        {
            if (files[i] is DirectoryInfo)  //如果是文件夹则递归处理
            {
                SetAssetBundlesName(files[i].FullName);
            }
            else if(!files[i].Name.EndsWith(".meta")) //如果是文件的话,则设置AssetBundleName,并排除掉.meta文件
            {
                SetABName(files[i].FullName);     //逐个设置AssetBundleName
            }
        }
 
    }
    
    /// <summary>
    /// 设置单个AssetBundle的Name
    /// </summary>
    ///<param name="filePath">
    static void SetABName(string assetPath)
    {
        string importerPath ="Assets"+assetPath.Substring(Application.dataPath.Length);  //这个路径必须是以Assets开始的路径
        AssetImporter assetImporter = AssetImporter.GetAtPath(importerPath);  //得到Asset
 
        string tempName=assetPath.Substring(assetPath.LastIndexOf(@"")+1); 
        string assetName = tempName.Remove(tempName.LastIndexOf(".")); //获取asset的文件名称
        assetImporter.assetBundleName = assetName;    //最终设置assetBundleName
    }
    
    /// <summary>
    /// 清除所有的AssetBundleName,由于打包方法会将所有设置过AssetBundleName的资源打包,所以自动打包前需要清理
    /// </summary>
    static void ClearAssetBundlesName()
    {
        //获取所有的AssetBundle名称
        string[] abNames = AssetDatabase.GetAllAssetBundleNames();
 
        //强制删除所有AssetBundle名称
        for (int i = 0; i < abNames.Length; i++)
        {
            AssetDatabase.RemoveAssetBundleName(abNames[i], true);
        }
    }
}

打包结果:

d4bafef819acf103eef6c03cf8bfa9a4.png
原始资源目录

5beb7c3a767ce7cb7f084d7bfa4b189c.png
打包结果

之后就可以参照

黄建泉:资源服务器快速搭建及AssetBundle资源下载实例​zhuanlan.zhihu.com
2e7f946dcb14a238990eedb742b746dc.png

搭建本地资源服务器进行加载啦,结束~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值