【Unity笔记】打包AssetBundle的方式

介绍一种在Unity编辑器选中prefab打包成ab的方式:
1.多个资源对象打包成一个ab文件
2.每个资源对象单独生成ab文件

using UnityEngine;
using UnityEditor;
using System.IO;

public class AssetBundleTools : Editor
{
        //ab文件输出目录
	private static readonly string destPath = Application.streamingAssetsPath; 
	public static string DestPath
	{
	   get {
		if (!Directory.Exists(destPath))
		    Directory.CreateDirectory(destPath);
		return destPath;
	    }
	}
	/// <summary>
	/// 所选资源打包为一个ab文件
	/// </summary>
	[MenuItem("AssetBundleTools/BuildMix")]
	private static void PackageAssetBundleMix()
	{
        AssetBundleBuild[] buildMap = new AssetBundleBuild[1];
		buildMap[0].assetBundleName = "bundlename"; //生成的ab名
		Object[] objs = Selection.objects;
		string[] assets = new string[objs.Length];
		for (int i = 0; i < objs.Length; i++)
		{
			assets[i] = AssetDatabase.GetAssetPath(objs[i]); //获取文件工程相对路径
		}
		buildMap[0].assetNames = assets;
		if (BuildPipeline.BuildAssetBundles(DestPath, buildMap, BuildAssetBundleOptions.None, EditorUserBuildSettings.activeBuildTarget))
		{
			Debug.Log(buildMap[0].assetNames + "Package Success");
		}
		else {
			Debug.Log(buildMap[0].assetNames + "Package Faild");
		}
		AssetDatabase.Refresh(); 
	}
	/// <summary>
        /// 所选文件分别打包为独立ab文件
        /// </summary>
	[MenuItem("AssetBundleTools/BuildSingle")]
	private static void PackageAssetBundleSingle()
	{
		Object[] objs = Selection.objects;
		AssetBundleBuild[] buildMap = new AssetBundleBuild[objs.Length];
		for (int i = 0; i < objs.Length; i++)
		{
			string[] assets = new string[] { AssetDatabase.GetAssetPath(objs[i]) };
			buildMap[i].assetBundleName = objs[i].name;
			buildMap[i].assetNames = assets;
		}
		if (BuildPipeline.BuildAssetBundles(DestPath, buildMap, BuildAssetBundleOptions.None, EditorUserBuildSettings.activeBuildTarget))
			Debug.Log("Package Success");
		else 
			Debug.Log("Package Faild");
		AssetDatabase.Refresh();
	}
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值