using System.IO;
using UnityEditor;
using UnityEngine;
public class GenerateAssetBundles : EditorWindow {
string[] LevelPath;
string LevelOutputPath = @"E:\temp\BoXing_imj\Assets\BUNDLES\Levels\StandAlone\";
BuildTarget Level_platform, Level_platform2;
string PrefabFolderPath = @"Assets\1\1Export";
string PrefabOutputPath = @"E:\temp\BoXing_imj\Assets\BUNDLES\DLC";
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void OnEnable(){
LevelPath = new string[1];
LevelPath[0] = @"Assets\game1.unity";
Level_platform = BuildTarget.StandaloneWindows;
Level_platform2 = BuildTarget.StandaloneWindows;
}
// Add menu item named "My Window" to the Window menu
[MenuItem("Window/Creat Asset Bundle")]
public static void ShowWindow()
{
//Show existing window instance. If one doesn't exist, make one.
EditorWindow.GetWindow(typeof(GenerateAssetBundles));
}
//-----------------------------------------------------------------
void OnGUI(){
GUILayout.Label ("Level bundle", EditorStyles.boldLabel);
LevelPath[0] = EditorGUILayout.TextField ("Level path", LevelPath[0]);
LevelOutputPath = EditorGUILayout.TextField ("Output folder path", LevelOutputPath);
Level_platform = (BuildTarget) EditorGUILayout.EnumPopup("Build target:", Level_platform);
if(GUILayout.Button ("Creat level bundle")){
Generator( LevelPath, LevelOutputPath + Path.GetFileName(LevelPath[0])+"3d", Level_platform);
}
if(GUILayout.Button ("Creat ALL levels bundle")){
GeneratingAllLevelBundle(Level_platform);
}
EditorGUILayout.Space();
GUILayout.Label ("Prefab bundle", EditorStyles.boldLabel);
PrefabFolderPath = EditorGUILayout.TextField ("Folder path", PrefabFolderPath);
PrefabOutputPath = EditorGUILayout.TextField ("Out Folder path", PrefabOutputPath);
Level_platform2 = (BuildTarget) EditorGUILayout.EnumPopup("Build target:", Level_platform2);
if(GUILayout.Button ("Creat prefab bundle")){
GeneratorPrefab( PrefabFolderPath, PrefabOutputPath, "*.prefab", Level_platform2);
}
EditorGUILayout.Space();
}
//------------------------------------------------
void GeneratingAllLevelBundle(BuildTarget platform){
string platformBuild = "";
string[] levels = new string[1];
string output;
if(platform == BuildTarget.StandaloneWindows) platformBuild = @"StandAlone\";
else
if(platform == BuildTarget.WebPlayer) platformBuild = @"WebPlayer\";
else
if(platform == BuildTarget.Android) platformBuild = @"Android\";
levels[0]=@"Assets\Base.unity";
output=@"E:\temp\BoXing_imj\Assets\BUNDLES\Levels\"+platformBuild+@"Base.unity3d";
BuildPipeline.BuildStreamedSceneAssetBundle(levels, output, platform);
levels[0]=@"Assets\game1.unity";
output=@"E:\temp\BoXing_imj\Assets\BUNDLES\Levels\"+platformBuild+@"game1.unity3d";
BuildPipeline.BuildStreamedSceneAssetBundle(levels, output, platform);
levels[0]=@"Assets\game2.unity";
output=@"E:\temp\BoXing_imj\Assets\BUNDLES\Levels\"+platformBuild+@"game2.unity3d";
BuildPipeline.BuildStreamedSceneAssetBundle(levels, output, platform);
}
[MenuItem("Assets/Build AssetBundle- Scene")]
static void Generator(string[] levels, string outputPath, BuildTarget platform) {
BuildPipeline.BuildStreamedSceneAssetBundle(levels, outputPath, platform);
}
[MenuItem("Assets/Build AssetBundle- Image")]
static void Generator2() {
string[] levels = new string[1];
levels[0] = @"Assets/_TestScene.unity";
BuildPipeline.BuildStreamedSceneAssetBundle(levels, @"Assets/Streamed-TestScene.unity3d", BuildTarget.StandaloneWindows);
}
//------------------------------------------------
[MenuItem("Assets/Generate AssetBundles from Prefabs")]
static void DoGenerateAssetBundles() {
GeneratorPrefab("*.prefab");
}
[MenuItem("Assets/Generate AssetBundles from Prefabs", true)]
static bool DoGenerateAssetBundlesValidate() {
return Directory.Exists(AssetDatabase.GetAssetPath(Selection.activeObject));
}
//[MenuItem("Assets/Generate AssetBundles from FBX")]
//static void DoGenerateAssetBundlesFbx() {
// Generator("*.jpg");
//}
//[MenuItem("Assets/Generate AssetBundles from FBX", true)]
//static bool DoGenerateAssetBundlesFbxValidate() {
// return Directory.Exists(AssetDatabase.GetAssetPath(Selection.activeObject));
//}
static void GeneratorPrefab(string extansion) {
string srcFolder = AssetDatabase.GetAssetPath(Selection.activeObject);
string label = "";
switch(Application.systemLanguage) {
case SystemLanguage.English:
label = "Save bundles to directory";
break;
case SystemLanguage.Russian:
label = "Укажите папку для сохранения ресурсов";
break;
}
string dstFolder = EditorUtility.SaveFolderPanel(label, srcFolder, "");
if(dstFolder == "") return;
int count = 0;
foreach(string fname in Directory.GetFiles(srcFolder, extansion, SearchOption.AllDirectories)) {
string name = Path.GetFileNameWithoutExtension(fname);
Object obj = AssetDatabase.LoadMainAssetAtPath(fname);
if(obj != null) {
Debug.Log("Saving File : " + obj.name);
BuildPipeline.BuildAssetBundle(obj, null, dstFolder + Path.DirectorySeparatorChar + name + ".unity3d",
BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
count++;
}
}
Debug.Log("Total saved files count : " + count);
}
static void GeneratorPrefab(string srcFolder, string dstFolder, string extansion, BuildTarget platform) {
int count = 0;
string outFolder = dstFolder + Path.DirectorySeparatorChar;
if(platform == BuildTarget.StandaloneWindows) outFolder = dstFolder + Path.DirectorySeparatorChar + @"StandAlone\";
else
if(platform == BuildTarget.WebPlayer) outFolder = dstFolder + Path.DirectorySeparatorChar + @"WebPlayer\";
else
if(platform == BuildTarget.Android) outFolder = dstFolder + Path.DirectorySeparatorChar + @"Android\";
foreach(string fname in Directory.GetFiles(srcFolder, extansion, SearchOption.AllDirectories)) {
string name = Path.GetFileNameWithoutExtension(fname);
Object obj = AssetDatabase.LoadMainAssetAtPath(fname);
if(obj != null) {
Debug.Log("Saving File : " + obj.name);
BuildPipeline.BuildAssetBundle(obj, null, outFolder + name + ".unity3d",
BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, platform);
count++;
}
}
Debug.Log("Total saved files count : " + count);
}
}