关于在Unity中打包AssetBundle文件

在项目制作中,对于数量角度的资源或者再资源优化的时候,我们最常用的方法就是打包Ab文件,下面给出打包的具体方法:

降下面的脚本放到Editor文件夹中即可:


using UnityEngine;
using System.Collections;
using UnityEditor;

public class ModelPackage: Editor
{
	
	//打包单个
	[MenuItem("Custom Editor/Create AssetBunldes WebPlayer")]
	static void CreateAssetBunldesWebPlayer ()
	{
		//获取在Project视图中选择的所有游戏对象
		Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);
		
		//遍历所有的游戏对象
		foreach (Object obj in SelectedAsset) {
			string webPlayertargetPath = Application.dataPath + "/StreamingAssets/WebPlayer/" + obj.name + ".assetbundle";			
			if (BuildPipeline.BuildAssetBundle (obj, null, webPlayertargetPath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.WebPlayer)) {
				Debug.Log (obj.name + ",webPlayer资源打包成功");
			} else {
				Debug.Log (obj.name + ",webPlaye资源打包失败");
			}
		}
		//刷新编辑器
		AssetDatabase.Refresh ();			
	}	
	//打包单个
	[MenuItem("Custom Editor/Create AssetBunldes PC")]
	static void CreateAssetBunldesPC ()
	{
		//获取在Project视图中选择的所有游戏对象
		Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);
		
		//遍历所有的游戏对象
		foreach (Object obj in SelectedAsset) {
			string pctargetPath = Application.streamingAssetsPath + "/PC/" + obj.name + ".assetbundle";
			if (BuildPipeline.BuildAssetBundle (obj, null, pctargetPath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.StandaloneWindows)) {
				Debug.Log (obj.name + ",pc资源打包成功");
			} else {
				Debug.Log (obj.name + ",pc资源打包失败");
			}
		}
		//刷新编辑器
		AssetDatabase.Refresh ();			
	}
	
	//打包单个
	[MenuItem("Custom Editor/Create AssetBunldes Android")]
	static void CreateAssetBunldesAndroid ()
	{
		//获取在Project视图中选择的所有游戏对象
		Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);
		
		//遍历所有的游戏对象
		foreach (Object obj in SelectedAsset) {
			string androidtargetPath = Application.dataPath + "/StreamingAssets/Android/" + obj.name + ".assetbundle";
			if (BuildPipeline.BuildAssetBundle (obj, null, androidtargetPath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.Android)) {
				Debug.Log (obj.name + ",Android资源打包成功");
			} else {
				Debug.Log (obj.name + ",Android资源打包失败");
			}
		}
		//刷新编辑器
		AssetDatabase.Refresh ();			
	}
	
	//打包单个
	[MenuItem("Custom Editor/Create AssetBunldes IOS")]
	static void CreateAssetBunldesIOS ()
	{
		//获取在Project视图中选择的所有游戏对象
		Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);
		
		//遍历所有的游戏对象
		foreach (Object obj in SelectedAsset) {
			string iPhonetargetPath = Application.dataPath + "/StreamingAssets/IOS/" + obj.name + ".assetbundle";			
			if (BuildPipeline.BuildAssetBundle (obj, null, iPhonetargetPath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.iPhone)) {
				Debug.Log (obj.name + ",iPhone资源打包成功");
			} else {
				Debug.Log (obj.name + ",iPhone资源打包失败");
			}
		}
		//刷新编辑器
		AssetDatabase.Refresh ();			
	}
	
	
	
	//打包单个
	[MenuItem("Custom Editor/Create AssetBunldes Main")]
	static void CreateAssetBunldesMain ()
	{
		//获取在Project视图中选择的所有游戏对象
		Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);
		
		//遍历所有的游戏对象
		foreach (Object obj in SelectedAsset) {
			//本地测试:建议最后将Assetbundle放在StreamingAssets文件夹下,如果没有就创建一个,因为移动平台下只能读取这个路径
			//StreamingAssets是只读路径,不能写入
			//服务器下载:就不需要放在这里,服务器上客户端用www类进行下载。
			string webPlayertargetPath = Application.dataPath + "/StreamingAssets/WebPlayer/" + obj.name + ".assetbundle";			
			if (BuildPipeline.BuildAssetBundle (obj, null, webPlayertargetPath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.WebPlayer)) {
				Debug.Log (obj.name + ",webPlayer资源打包成功");
			} else {
				Debug.Log (obj.name + ",webPlaye资源打包失败");
			}
			
			string androidtargetPath = Application.dataPath + "/StreamingAssets/Android/" + obj.name + ".assetbundle";
			if (BuildPipeline.BuildAssetBundle (obj, null, androidtargetPath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.Android)) {
				Debug.Log (obj.name + ",Android资源打包成功");
			} else {
				Debug.Log (obj.name + ",Android资源打包失败");
			}
			
			string iPhonetargetPath = Application.dataPath + "/StreamingAssets/IOS/" + obj.name + ".assetbundle";			
			if (BuildPipeline.BuildAssetBundle (obj, null, iPhonetargetPath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.iPhone)) {
				Debug.Log (obj.name + ",iPhone资源打包成功");
			} else {
				Debug.Log (obj.name + ",iPhone资源打包失败");
			}
			
			string pctargetPath = Application.streamingAssetsPath + "/PC/" + obj.name + ".assetbundle";
			if (BuildPipeline.BuildAssetBundle (obj, null, pctargetPath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.StandaloneWindows)) {
				Debug.Log (obj.name + ",pc资源打包成功");
			} else {
				Debug.Log (obj.name + ",pc资源打包失败");
			}
		}
		//刷新编辑器
		AssetDatabase.Refresh ();	
		
	}
	
	[MenuItem("Custom Editor/Create AssetBunldes ALL")]
	static void CreateAssetBunldesALL ()
	{
		
		Caching.CleanCache ();
		
		
		string Path = Application.dataPath + "/StreamingAssets/ALL.assetbundle";
		
		
		Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);
		
		foreach (Object obj in SelectedAsset) {
			Debug.Log ("Create AssetBunldes name :" + obj);
		}
		
		//这里注意第二个参数就行
		if (BuildPipeline.BuildAssetBundle (null, SelectedAsset, Path, BuildAssetBundleOptions.CollectDependencies)) {
			AssetDatabase.Refresh ();
		} else {
			
		}
	}
	
	[MenuItem("Custom Editor/Create Scene")]
	static void CreateSceneALL ()
	{
		//清空一下缓存
		Caching.CleanCache ();
		string Path = Application.dataPath + "/MyScene.unity3d";
		string  [] levels = {"Assets/Level.unity"};
		//打包场景
		BuildPipeline.BuildPlayer (levels, Path, BuildTarget.WebPlayer, BuildOptions.BuildAdditionalStreamedScenes);
		AssetDatabase.Refresh ();
	}
	
}


打包完成的文件保存在streamingAssets文件夹中,再通过www加载即可。



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值