| using System.Collections;  | 
| using System.Collections.Generic;  | 
| using UnityEngine;  | 
| using UnityEditor;  | 
| using System.IO;  | 
|   | 
| public class AssetBundleBuildExtersion  | 
| {  | 
|     public static void Build(string output, string category, BuildAssetBundleOptions bundleOption, BuildTarget buildTarget, bool rebuild = true)  | 
|     {  | 
|         var assetBundles = AssetDatabase.GetAllAssetBundleNames();  | 
|         var targetAssetBundles = new List<string>();  | 
|   | 
|         if (category == "mobeffectshader")  | 
|         {  | 
|             for (int i = 0; i < assetBundles.Length; i++)  | 
|             {  | 
|                 var bundleName = assetBundles[i];  | 
|                 if (bundleName.StartsWith("gmodels") || bundleName.StartsWith("effect") || bundleName.StartsWith("graphic"))  | 
|                 {  | 
|                     targetAssetBundles.Add(bundleName);  | 
|                 }  | 
|             }  | 
|         }  | 
|         else  | 
|         {  | 
|             for (int i = 0; i < assetBundles.Length; i++)  | 
|             {  | 
|                 var bundleName = assetBundles[i];  | 
|                 if (bundleName.StartsWith(category))  | 
|                 {  | 
|                     targetAssetBundles.Add(bundleName);  | 
|                 }  | 
|             }  | 
|         }  | 
|   | 
|         if (targetAssetBundles.Count == 0)  | 
|             return;  | 
|   | 
|         var assets = new List<AssetBundleBuild>();  | 
|   | 
|         for (int i = 0; i < targetAssetBundles.Count; i++)  | 
|         {  | 
|             var assetBundleBuild = new AssetBundleBuild();  | 
|             assetBundleBuild.assetBundleName = targetAssetBundles[i];  | 
|             assetBundleBuild.assetNames = AssetDatabase.GetAssetPathsFromAssetBundle(targetAssetBundles[i]);  | 
|             assets.Add(assetBundleBuild);  | 
|         }  | 
|   | 
|         var rootPath = StringUtility.Contact(output, Path.AltDirectorySeparatorChar, category);  | 
|         var mainFile = StringUtility.Contact(output, Path.AltDirectorySeparatorChar, GetMainFestFileName(buildTarget));//改名字前  | 
|         var mainFileRename = StringUtility.Contact(output, Path.AltDirectorySeparatorChar, category, "_assetbundle");//改名字后  | 
|         var manifest = StringUtility.Contact(output, Path.AltDirectorySeparatorChar, GetMainFestFileName(buildTarget), ".manifest");  | 
|         var manifestRename = StringUtility.Contact(output, Path.AltDirectorySeparatorChar, category, "_assetbundle.manifest");  | 
|   | 
|         if (rebuild && Directory.Exists(rootPath))  | 
|             Directory.Delete(rootPath, true);  | 
|   | 
|         // if (!Directory.Exists(rootPath))  | 
|         //     Directory.CreateDirectory(rootPath);  | 
|   | 
|         if (File.Exists(mainFileRename))  | 
|         {  | 
|             File.Delete(mainFileRename);  | 
|         }  | 
|   | 
|         if (File.Exists(manifestRename))  | 
|         {  | 
|             File.Delete(manifestRename);  | 
|         }  | 
|   | 
|         BuildPipeline.BuildAssetBundles(output, assets.ToArray(), bundleOption, buildTarget);  | 
|   | 
|         if (File.Exists(mainFile))  | 
|             File.Move(mainFile, mainFileRename);  | 
|         if (File.Exists(manifest))  | 
|             File.Move(manifest, manifestRename);  | 
|     }  | 
|   | 
|     static string GetMainFestFileName(BuildTarget _target)  | 
|     {  | 
|         switch (_target)  | 
|         {  | 
|             case BuildTarget.StandaloneWindows:  | 
|                 return "standalone";  | 
|             case BuildTarget.Android:  | 
|                 return "android";  | 
|             case BuildTarget.iOS:  | 
|                 return "ios";  | 
|             default:  | 
|                 return string.Empty;  | 
|         }  | 
|     }  | 
|   | 
|     [MenuItem("Assets/BuildAssetBundle ( Test )")]  | 
|     public static void BuildTest()  | 
|     {  | 
|         if (Selection.activeObject != null)  | 
|         {  | 
|             var path = AssetDatabase.GetAssetPath(Selection.activeObject);  | 
|             var importer = AssetImporter.GetAtPath(path);  | 
|   | 
|             if (importer != null)  | 
|             {  | 
|                 var ab = importer.assetBundleName;  | 
|                 if (!string.IsNullOrEmpty(ab))  | 
|                 {  | 
|   | 
|                     var assetBundles = AssetDatabase.GetAllAssetBundleNames();  | 
|   | 
|                     List<string> targetAssetBundles = new List<string>();  | 
|                     for (int i = 0; i < assetBundles.Length; i++)  | 
|                     {  | 
|                         var bundleName = assetBundles[i];  | 
|                         if (bundleName == ab)  | 
|                         {  | 
|                             targetAssetBundles.Add(bundleName);  | 
|                         }  | 
|                     }  | 
|   | 
|                     var assets = new List<AssetBundleBuild>();  | 
|                     for (int i = 0; i < targetAssetBundles.Count; i++)  | 
|                     {  | 
|                         var assetBundleBuild = new AssetBundleBuild();  | 
|                         assetBundleBuild.assetBundleName = targetAssetBundles[i];  | 
|                         assetBundleBuild.assetNames = AssetDatabase.GetAssetPathsFromAssetBundle(targetAssetBundles[i]);  | 
|                         assets.Add(assetBundleBuild);  | 
|                     }  | 
|   | 
|                     var opt = BuildAssetBundleOptions.None  | 
|                         | BuildAssetBundleOptions.ChunkBasedCompression  | 
|                         | BuildAssetBundleOptions.DeterministicAssetBundle;  | 
|   | 
|                     var dirctory = Application.dataPath + "/BundleTest";  | 
|                     if (!Directory.Exists(dirctory))  | 
|                     {  | 
|                         Directory.CreateDirectory(dirctory);  | 
|                     }  | 
|                     BuildPipeline.BuildAssetBundles(dirctory, assets.ToArray(), opt, BuildTarget.Android);  | 
|                 }  | 
|             }  | 
|   | 
|         }  | 
|     }  | 
|   | 
| }  |