您的位置:首页 > 移动开发 > Unity3D

AssetBundle资源打包

2016-06-15 16:03 411 查看
unity5.x AssetBundle 资源打包方法(在工程目录下创建一个ASB文件夹):

using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;

public class BASB
{

[@MenuItem("Asset/Build AssetBundles")]
public static void ExportAssetBundles6 ()
{
ExportAssetBundles5();
}

public static bool ExportAssetBundles5 (string assetBundleName = "a" ,
string assetBundleVariant = "unity3d" ,
string assetPath = "/ASB" ,
BuildAssetBundleOptions basbOptions  = BuildAssetBundleOptions.CollectDependencies,
BuildTarget bTarget = BuildTarget.StandaloneWindows)
{
//获取在Project视图中选择的所有游戏对象
Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);
int length = SelectedAsset.Length;
if(length == 0)
{
Debug.LogWarning("none asset is selected");
return false;
}
string[] assetNames = new string[length];
for(int i = 0 ; i < length ; i ++)
{
assetNames[i] =  AssetDatabase.GetAssetPath (SelectedAsset[i]);
}
AssetBundleBuild[] abbs = new AssetBundleBuild[1];
AssetBundleBuild sbb = new AssetBundleBuild();
sbb.assetBundleName = assetBundleName;
sbb.assetBundleVariant = assetBundleVariant;
//		sbb.assetNames = assetNames;
sbb.assetNames = AssetDatabase.GetDependencies(assetNames);
abbs[0] = sbb;
//		string path = Application.dataPath + "/ASB";
assetPath = Application.dataPath + assetPath;
AssetBundleManifest amf  = BuildPipeline.BuildAssetBundles(assetPath , abbs ,basbOptions , bTarget);
if(amf != null)
{
Debug.Log("create asb succeed ! file path : " + assetPath);
}
//刷新编辑器
AssetDatabase.Refresh ();

return amf != null;
}

//	[@MenuItem("Asset/Build AssetBundles From Directory of Files - single")]
//	static void ExportAssetBundles ()
//	{
//		// Get the selected directory
//		//获取选择的目录
//		string path = AssetDatabase.GetAssetPath (Selection.activeObject);
//		Debug.Log ("Selected Folder: " + path);
//		if (path.Length == 0) {
//			return;
//		}
//		path = path.Replace ("Assets/", "");
//		string [] fileEntries = Directory.GetFiles (Application.dataPath + "/" + path);
//		foreach (string fileName in fileEntries) {
//			if(fileName.Contains(".meta"))
//			{
//				continue;
//			}
//			string filePath = fileName.Replace ("\\", "/");
//			int index = filePath.LastIndexOf ("/");
//			filePath = filePath.Substring (index);
//			string localPath = "Assets/" + path;
//			if (index > 0)
//				localPath += filePath;
//			Object t = AssetDatabase.LoadMainAssetAtPath (localPath);
//			if (t != null) {
//				Debug.Log ("resName: "+t.name);
//				string bundlePath = "Assets/" + path + "/" + t.name + ".unity3d";
//				Debug.Log ("Building bundle at: " + bundlePath);
//				// Build the resource file from the active selection.
//				//从激活的选择编译资源文件
//				bool isSucc = BuildPipeline.BuildAssetBundle (t, null, bundlePath, basbOptions , bTarget);
//				Debug.Log("isSucc : " + isSucc);
//			}
//		}
//		// 刷新编辑器
//		AssetDatabase.Refresh ();
//	}
//	[@MenuItem("Asset/Build AssetBundles From Directory of Files - all")]
//	static void ExportAssetBundles2 ()
//	{
//		// Get the selected directory
//		//获取选择的目录
//		string path = AssetDatabase.GetAssetPath (Selection.activeObject);
//		Debug.Log ("Selected Folder: " + path);
//		if (path.Length == 0) {
//			return;
//		}
//		path = path.Replace ("Assets/", "");
//		string bundlePath = Application.dataPath + "/" + path;
//		string [] fileEntries = Directory.GetFiles (bundlePath);
//		List<Object> objs = new List<Object>();
//		foreach (string fileName in fileEntries) {
//			if(fileName.Contains(".meta"))
//			{
//				continue;
//			}
//			string filePath = fileName.Replace ("\\", "/");
//			int index = filePath.LastIndexOf ("/");
//			filePath = filePath.Substring (index);
//			string localPath = "Assets/" + path;
//			if (index > 0)
//				localPath += filePath;
//			Object t = AssetDatabase.LoadMainAssetAtPath (localPath);
//			if (t != null) {
//				objs.Add(t);
//				Debug.Log ("resName: "+t.name);
//			}
//		}
//		if(objs.Count == 0) return;
//		string[] filePNames = path.Split('/');
//		bundlePath += ("/" + filePNames[filePNames.Length - 1] + ".unity3d");
//		Debug.Log ("Building bundle at: " + bundlePath);
//		bool isSucc = BuildPipeline.BuildAssetBundle (null, objs.ToArray(), bundlePath, basbOptions , bTarget);
//		Debug.Log("isSucc : " + isSucc);
//		// 刷新编辑器
//		AssetDatabase.Refresh ();
//	}
//	[@MenuItem("Asset/Build AssetBundles From Selected of Files - single")]
//	static void ExportAssetBundles3 ()
//	{
//		//获取在Project视图中选择的所有游戏对象
//		Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);
//				//遍历所有的游戏对象
//		foreach (Object obj in SelectedAsset)
//		{
//			string sourcePath = AssetDatabase.GetAssetPath (obj);
//			//本地测试:建议最后将Assetbundle放在StreamingAssets文件夹下,如果没有就创建一个,因为移动平台下只能读取这个路径
//			//StreamingAssets是只读路径,不能写入
//			//服务器下载:就不需要放在这里,服务器上客户端用www类进行下载。
//			string targetPath = Application.dataPath + "/StreamingAssets/" + obj.name + ".assetbundle";
//			bool isSucc = BuildPipeline.BuildAssetBundle (obj, null, targetPath, basbOptions  , bTarget);
//			Debug.Log("isSucc : " + isSucc);
//		}
//		//刷新编辑器
//		AssetDatabase.Refresh ();
//	}
//	[@MenuItem("Asset/Build AssetBundles From Selected of Files - all")]
//	static void ExportAssetBundles4 ()
//	{
//		//获取在Project视图中选择的所有游戏对象
//		Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);
//
//		string path = Application.dataPath + "/StreamingAssets/" + "asb.assetbundle";
//		//打包资源
//		bool isSucc = BuildPipeline.BuildAssetBundle(null, SelectedAsset, path, basbOptions, bTarget);
//
//		Debug.Log("isSucc : " + isSucc);
//		//刷新编辑器
//		AssetDatabase.Refresh ();
//	}

}


也可使用创建AssetBundle资源包的编辑器窗口

using UnityEngine;
using UnityEditor;
public class MyEditor : EditorWindow
{

[MenuItem ("Custom/Build AssetBundles Window")]
static void AddWindow ()
{
//创建窗口
Rect  wr = new Rect (0,0,640,640);
MyEditor window = (MyEditor)EditorWindow.GetWindowWithRect (typeof (MyEditor),wr,true,"window");
window.Show();

}

string[] names = new string[3]{"a" , "unity3d" , "/ASB"};
BuildAssetBundleOptions[] babos = new BuildAssetBundleOptions[]{
BuildAssetBundleOptions.None,
BuildAssetBundleOptions.UncompressedAssetBundle,
BuildAssetBundleOptions.CollectDependencies,
BuildAssetBundleOptions.CompleteAssets ,
BuildAssetBundleOptions.DisableWriteTypeTree ,
BuildAssetBundleOptions.DeterministicAssetBundle ,
BuildAssetBundleOptions.ForceRebuildAssetBundle ,
BuildAssetBundleOptions.IgnoreTypeTreeChanges ,
BuildAssetBundleOptions.AppendHashToAssetBundleName

};
bool[] buildAssetBundleOptions = new bool[9];
BuildTarget[] buildTargets = new BuildTarget[]{BuildTarget.iOS , BuildTarget.Android , BuildTarget.StandaloneWindows};
int buildTarget = 2;

public void Awake ()
{

}
//绘制窗口时调用
void OnGUI ()
{
GUILayout.Space(20);
//输入框控件
names[0] = EditorGUILayout.TextField("assetBundleName:",names[0]);
names[1] = EditorGUILayout.TextField("assetBundleVariant:",names[1]);
names[2] = EditorGUILayout.TextField("assetPath:",names[2]);

GUILayout.Space(50);
//		GUILayout.BeginArea(new Rect(50,100 , 500,400));
GUILayout.Button("buildAssetBundleOptions" , GUILayout.Width(200));
for(int i = 0 ; i < buildAssetBundleOptions.Length ; i ++)
{
buildAssetBundleOptions[i] = GUILayout.Toggle(buildAssetBundleOptions[i] , babos[i].ToString());
}
//		GUILayout.EndArea();

GUILayout.Space(50);
//		GUILayout.BeginArea(new Rect(50,300 , 500,400));
GUILayout.Button("build target", GUILayout.Width(200));
buildTarget = GUILayout.Toolbar(buildTarget , new string[]{"ios" , "android" , "window"});
//		GUILayout.EndArea();

GUILayout.BeginArea(new Rect(160,560,350,100));
GUILayout.BeginHorizontal();
if(GUILayout.Button("关闭窗口",GUILayout.Width(150) , GUILayout.Height(50)))
{
//关闭窗口
this.Close();
}
if(GUILayout.Button("创建资源包",GUILayout.Width(150) , GUILayout.Height(50)))
{

BuildAssetBundleOptions bbo = BuildAssetBundleOptions.None;

for(int i = 0 ; i < buildAssetBundleOptions.Length ; i ++)
{
if(buildAssetBundleOptions[i])
{
if(bbo != BuildAssetBundleOptions.None)
{
bbo = bbo | babos[i];
}
else{
bbo = babos[i];
}
}
}
//			Debug.Log("bbo = " + bbo);
bool isSucc = BASB.ExportAssetBundles5(names[0],names[1],names[2],bbo ,buildTargets[buildTarget]);

this.ShowNotification(new GUIContent(isSucc ? "创建成功" : "创建失败"));
}
GUILayout.EndArea();
}

//更新
void Update()
{

}
//	void OnFocus()
//	{
//		Debug.Log("当窗口获得焦点时调用一次");
//	}
//
//	void OnLostFocus()
//	{
//		Debug.Log("当窗口丢失焦点时调用一次");
//	}
//
//	void OnHierarchyChange()
//	{
//		Debug.Log("当Hierarchy视图中的任何对象发生改变时调用一次");
//	}
//
//	void OnProjectChange()
//	{
//		Debug.Log("当Project视图中的资源发生改变时调用一次");
//	}
void OnInspectorUpdate()
{
//Debug.Log("窗口面板的更新");
//这里开启窗口的重绘,不然窗口信息不会刷新
this.Repaint();
}

void OnSelectionChange()
{
//当窗口出去开启状态,并且在Hierarchy视图中选择某游戏对象时调用
//		foreach(Transform t in Selection.transforms)
//		{
//			//有可能是多选,这里开启一个循环打印选中游戏对象的名称
//			Debug.Log("OnSelectionChange" + t.name);
//		}
}
//	void OnDestroy()
//	{
//		Debug.Log("当窗口关闭时调用");
//	}
}


如图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity c# AssetBundle