简单介绍:一个简单的assetbundle自动标记打包,并把bundle的名字与其中的资源对应写到json文件中,方便后面加载资源
注意Unity的Asset文件夹下要有对应的层级目录,Resources文件夹下要有对应名字的json文件
bundle打包脚本(放到Editor文件夹下)
BuildAssetBundle.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
namespace FWork
{
public class BuildAssetBundle
{
public static KeyValueInfo keyValueList = null;//存储设置label的ab配置数据
/// <summary>
/// 打包生成所有AssetBundles
/// </summary>
[MenuItem("AssetBundleTools/BuildAllAssetBundles")]
public static void BuildAllAB()
{
//(打包)AB的输出路径
string strABOutPathDIR = "";
BuildTarget buildTarget = BuildTarget.NoTarget;
buildTarget = GetActiveBuildTarget();
strABOutPathDIR = PathManger.GetABOutFile();
if (!Directory.Exists(strABOutPathDIR))
{
Directory.CreateDirectory(strABOutPathDIR);
}
//打包生成
BuildPipeline.BuildAssetBundles(strABOutPathDIR, BuildAssetBundleOptions.ChunkBasedCompression, buildTarget);
AssetDatabase.Refresh();
Debug.Log("AB资源打包完成-文件路径:" + strABOutPathDIR);
}
/// <summary>
/// 删除AssetBundle包文件
/// </summary>
[MenuItem("AssetBundleTools/DeleteAllAssetBundles")]
public static void DeleteAllABs()
{
//(打包)AB的输出路径
string strNeedDeleteDIR = "";
strNeedDeleteDIR = PathManger.GetABOutFile();
if (!string.IsNullOrEmpty(strNeedDeleteDIR))
{
try
{
//参数true 表示可以删除非空目录。
Directory.Delete(strNeedDeleteDIR, true);
//去除删除警告
File.Delete(strNeedDeleteDIR + ".meta");
//刷新
AssetDatabase.Refresh();
Debug.Log("资源删除完成!");
}
catch (System.Exception)
{
Debug.Log(strNeedDeleteDIR+"文件夹不存在");
}
}
}
/// <summary>
/// (自动)给资源文件(预设)添加标记
/// </summary>
[MenuItem("AssetBundleTools/Set AB Label")]
public static void SetABLabels()
{
keyValueList = new KeyValueInfo();
keyValueList.KeyValueList.Clear();
//需要给AB做标记的根目录
string strNeedSetABLableRootDIR = "";
//目录信息
DirectoryInfo[]