using UnityEngine;
using UnityEditor;
using System.IO;
public class BuildAssetBundlesTool
{
static string targetDir = Application.dataPath + "/MyBundles";//AssetBundle的目录名
static string extensionName = ".assetbundle";//AssetBundle的扩展名
static Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
[MenuItem("Private Menu/Build Every")]
static void ExportAssetBundles()
{
if (!Directory.Exists(targetDir))
Directory.CreateDirectory(targetDir);
for (int i = 0; i < selection.Length; i++)
{
string targetPath = targetDir + Path.AltDirectorySeparatorChar + selection[i].name + extensionName; // AssetBundle 存储路径
if (File.Exists(targetPath))
File.Delete(targetPath);
//确保选中要打包的是【GameObject】,【Texture2D】,【Material】,【TextAsset】
//----如果没有此选项,那么打包的时候可以打包选中的任意类型,包括已经打包好的资源包。
if (!(selection[i] is TextAsset) && !(selection[i] is GameObject) && !(selection[i] is Texture2D) && !(selection[i] is Material))
continue;
if (BuildPipeline.BuildAssetBundle(selection[i], null, targetPath))//, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets))
{
Debug.Log(selection[i].name + ":OK~");
AssetDatabase.Refresh();
}
}
}
[MenuItem("Private Menu/Build Single")]
static void ExportResource()
{
if (!Directory.Exists(targetDir))
Directory.CreateDirectory(targetDir);
string targetPath = targetDir + Path.AltDirectorySeparatorChar + "Bundles" + extensionName;
if (File.Exists(targetPath))
File.Delete(targetPath);
if (BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, targetPath, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets))
{
Debug.Log("OK~");
}
}
}