Unity AssetBundle 自动设置 assetBundle名

做项目时候有需求模块化更新,模块化的打bundle,作为程序开发人员,一次拿来多个模块的资源包,需要批量将资源包更改assetBundleName。

如图
在这里插入图片描述
例如:在BundleFloders目录下,有很多以文件夹为单位的资源包,需要全部设置assetBundleName
可以写一个工具一键自动生成对应的assetBundleName

代码如下:

using UnityEditor;
using UnityEngine;
using System.IO;

public class Tools {

	[MenuItem("Tools/AutoSet AssetBundleName")]
	// 获取目标文件夹目录
	public static void AutoSetBundleName(){
		string path = Path.Combine (Application.dataPath, "SetBundNameTest/BundleFloders");
		DirectoryInfo dir_info = new DirectoryInfo (path);
		// 获取所有子文件夹
		DirectoryInfo[] dir_arr = dir_info.GetDirectories ();

		for (int i = 0; i < dir_arr.Length; i++) {
			DirectoryInfo current_dir = dir_arr [i];

			string dir_name = current_dir.Name;
			// 通过子文件夹名拼接成 assetsbundle 名
			string assetbundle_name = string.Format ("auto_set_{0}", dir_name.ToLower());

			string dir_path = current_dir.FullName;
			string asset_path = dir_path.Replace (Application.dataPath, "Assets");
			// 通过在工程内的路径获取 AssetImporter
			AssetImporter ai = AssetImporter.GetAtPath(asset_path);

			ai.assetBundleName = assetbundle_name;
			// 也可以修改 AssetImporter 中 assetBundleVariant 属性
			ai.assetBundleVariant = "variant";
		}
	}
}

工具完成后会在MenuItem自定义目录中显示
在这里插入图片描述
点击AutoSet AssetBundleName 后,会在动将BundleFloders目录下所有文件夹设置assetBundleName
在这里插入图片描述

其他

更多Blog请见:https://yiyuan1130.github.io/
Github地址:https://github.com/yiyuan1130

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值