简单讲一下AB资源打包相关的东东
AB的标签选中资源时就可以在右下角选择,屁股后面的None是后缀名,可以自定义。
我们可以按照文件夹来打,也可以逐个文件来搞,这个看项目的打包策略。
个人建议按照实际使用场景来打包,比如说每个UI界面单独打一个包,图片按功能打图集(包括Common),这样可以尽量减少游戏运行时的内存占用。此外要注意尽量减少资源间的冗余依赖,详情可以参考
Unity AssetBundle(1):Assets打包和依赖(Dependencies)理解www.jianshu.com接下来直接上打包代码,两种方式,一种是指定目录无脑按文件名打包(不推荐),一种是按照我们的设置进行打包(需要按照实际项目需求拓展,这里不做任何自动化相关的处理)
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);
}
}
}
打包结果:
之后就可以参照
黄建泉:资源服务器快速搭建及AssetBundle资源下载实例zhuanlan.zhihu.com搭建本地资源服务器进行加载啦,结束~