Skip to content

Instantly share code, notes, and snippets.

@Scub3d
Created September 18, 2018 21:00
Show Gist options
  • Save Scub3d/e29151fed765b69a8631dccf01d93f76 to your computer and use it in GitHub Desktop.
Save Scub3d/e29151fed765b69a8631dccf01d93f76 to your computer and use it in GitHub Desktop.
Place inside your Unity projects Asset folder, as such: Assets -> Scripts -> Editor. Allows you to easily mark assets to be bundled
using UnityEngine;
using UnityEditor;
using System.IO;
public class AssetBundler : Editor {
[MenuItem("Assets/Build AssetBundles")]
static void BuildAllAssetBundles() {
string exportPath = ASSET_BUNDLE_OUTPUT_PATH; // Change to fit your needs
BuildPipeline.BuildAssetBundles(exportPath, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows64); // Change Options to fit your needs
}
private static AssetImporter assetImporter;
private static string ASSETS_TO_BUNDLE_PATH = Application.dataPath + @"\Resources\ResourcesToPutInBundle";
private static string ASSET_BUNDLE_OUTPUT_PATH = Application.dataPath + @"\Resources\AssetBundles";
private static string ASSET_BUNDLE_VARIANT = "";
// Comment out method when not being used //
[MenuItem("Assets/Mark Assets To Be Bundled")]
static void MarkAssetsToBeBundled () {
// This method assumes that you have created a directory to store all assets that you want to be marked for a bundled.
// This method then iterates through all subdirectories and marks every prefab in those subdirectories that you wish to be bundled.
DirectoryInfo rootDirectory = new DirectoryInfo(ASSETS_TO_BUNDLE_PATH); // Get root directory
DirectoryInfo[] subDirectories = rootDirectory.GetDirectories(); // Get all subdirectories
foreach(DirectoryInfo assetDirectory in subDirectories) { // Iterate through all subdirectories
FileInfo[] Files = assetDirectory.GetFiles("*.prefab"); // Get all prefab files in asset's directory
foreach(FileInfo file in Files) { // Iterate through all files in the subdirectory
assetImporter = AssetImporter.GetAtPath(ASSETS_TO_BUNDLE_PATH + @"\" + assetDirectory.Name + @"\" + file.Name); // Attempt to get asset importer
if(assetImporter != null) { // If the assetImporter doesn't work, skip
assetImporter.SetAssetBundleNameAndVariant(file.Name, ASSET_BUNDLE_VARIANT); // Mark asset (.prefab) to be bundled
assetImporter.SaveAndReimport(); // Save and reimport the asset
}
}
}
}
// Comment out method when not being used //
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment