There are three class methods you can use to build AssetBundles:
Building asset bundles is done through editor scripting. There is basic example of this in the scripting documentation for BuildPipeline.BuildAssetBundle.
For the sake of this example, copy and paste the script from the link above into a new C# script called ExportAssetBundles. This script should be placed in a folder named Editor, so that it works inside the Unity Editor.
Now in the
menu, you should see two new menu options.For this example, you should create a new prefab. First create a new Cube by going to
, which will create a new cube in the Hierarchy View. Then drag the Cube from the Hierarchy View into the Project View, which will create a prefab of that object.You should then right click the Cube prefab in the project window and select Cube.unity3d, your project window will now look something like this.
. At this point you will be presented with a window to save the “bundled” asset. If you created a new folder called "AssetBundles" and saved the cube asAt this point you can move the AssetBundle Cube.unity3d elsewhere on your local storage, or upload it to a server of your choice.
You can use AssetDatabase.ImportAsset to force reimporting the asset right before calling BuildPipeline.BuildAssetBundle, and then use AssetPostprocessor.OnPreprocessTexture to set the required properties. The following example will show you how to set different texture compressions when building the Asset Bundle.
// Builds an asset bundle from the selected objects in the project view, // and changes the texture format using an AssetPostprocessor. using UnityEngine; using UnityEditor; public class ExportAssetBundles { // Store current texture format for the TextureProcessor. public static TextureImporterFormat textureFormat; [MenuItem("Assets/Build AssetBundle From Selection - PVRTC_RGB2")] static void ExportResourceRGB2 () { textureFormat = TextureImporterFormat.PVRTC_RGB2; ExportResource(); } [MenuItem("Assets/Build AssetBundle From Selection - PVRTC_RGB4")] static void ExportResourceRGB4 () { textureFormat = TextureImporterFormat.PVRTC_RGB4; ExportResource(); } static void ExportResource () { // Bring up save panel. string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d"); if (path.Length != 0) { // Build the resource file from the active selection. Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets); foreach (object asset in selection) { string assetPath = AssetDatabase.GetAssetPath((UnityEngine.Object) asset); if (asset is Texture2D) { // Force reimport thru TextureProcessor. AssetDatabase.ImportAsset(assetPath); } } BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets); Selection.objects = selection; } } }
// Changes the texture format when building the Asset Bundle. using UnityEngine; using UnityEditor; public class TextureProcessor : AssetPostprocessor { void OnPreprocessTexture() { TextureImporter importer = assetImporter as TextureImporter; importer.textureFormat = ExportAssetBundles.textureFormat; } }
You can also control how the asset is imported using the AssetDatabase.ImportAssetOptions.
When first using AssetBundles it may seem enough to manually build them as seen in the previous example. But as a project grows in size and the number of assets increases doing this process by hand is not efficient. A better approach is to write a function that builds all of the AssetBundles for a project. You can, for example, use a text file that maps Asset files to AssetBundle files.
Page last updated: 2013-02-27