转自:http://blog.youkuaiyun.com/zhou_xw6511/article/details/9795493
今天查看了一下如何打包资源Assetbundle 的东西 现在粘上
/// <summary>
/// 分开打包
/// 把project 视图中包含的资源一一打包
/// </summary>
[MenuItem("Assets/Build AssetBundle From Sence One By One ")]
static void ExportResourceOnebyOne()
{
//获取在Project视图中选择的所有游戏对象
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
//遍历所有游戏对象
foreach (Object asset in selection)
{
//得到物体的路径
// string sourcePath = AssetDatabase.GetAssetPath(asset);
//本地测试:建议最后将Assetbundle放在StreamingAssets文件夹下,如果没有就创建一个,因为移动平台下只能读取这个路径
//StreamingAssets 路径下的文件只能读取 不能写入
//放在服务器上的资源没有要求
string targetPath = Application.dataPath + "/StreamingAssets/" + asset.name + ".unity3d";
if (targetPath != null)
{
if (BuildPipeline.BuildAssetBundle(asset, null, targetPath, BuildAssetBundleOptions.CollectDependencies))
{
Debug.Log("create Assetbundle Success!");
}
else
{
Debug.Log("Create Assetbundle failed!");
}
}
}
AssetDatabase.Refresh(); //刷新编辑器
}
/// <summary>
/// 打包为一个资源包
/// </summary>
[MenuItem("Assets/Build AssetBundle From Sence all object ")]
static void ExoprtResourcuAll()
{
Caching.CleanCache();
string Path = Application.dataPath + "/StreamingAssets/ALL.unity3d";
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
{
}
}
/// <summary>
/// 打包PC平台的资源包文件
/// </summary>
[MenuItem("Assets/Build AssetBundle From Selection TO BuildTarget is PC -Track dependencies")]
static void ExportResourcePcTrackdependencies()
{
//保存打包资源的
string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d");
if (path.Length!=0)
{
Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
Selection.objects = selection;
}
}
/// <summary>
/// 打包PC 资源不关联相应的资源 只打包资源本省
/// </summary>
[MenuItem("Assets/Build AssetBundle From SelectionTO BuildTarget is PC -NO dependencies tracking")]
static void ExportResourcePcNodependenciesTracking()
{
string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d");
if (path.Length!=0)
{
BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, path);
}
}
/// <summary>
/// 把场景打包成Stream文件
///
/// 如何加载场景
/// WWW download = WWW.LoadFromCacheOrDownload("http://xxx/streamed.unity3d",0);
/// yield return download;
/// Application.LoadLevel("Level1");
///
///
/// </summary>
[MenuItem("build/BuildWebplayerStreamed")]
static void Build()
{
string[] levels = new string[] { "Sence1","Sence2","Sence3","......" };
BuildPipeline.BuildStreamedSceneAssetBundle(levels, "streamed.unity3d", BuildTarget.WebPlayer);
}