Building AssetBundles

Building AssetBundles

There are three class methods you can use to buildAssetBundles:

An example of how to build an AssetBundle

Building asset bundles is done through editor scripting. There isbasic example of this in the scripting documentation forBuildPipeline.BuildAssetBundle.

For the sake of this example, copy and paste the script from thelink above into a new C# script called ExportAssetBundles. Thisscript should be placed in a folder named Editor, so that it worksinside the Unity Editor.

Now in the Assets menu, youshould see two new menu options.

  1. Build AssetBundle FromSelection - Track dependencies. This will build the currentobject into an asset bundle and include all of its dependencies.For example if you have a prefab that consists of severalhierarchical layers then it will recursively add all the childobjects and components to the asset bundle.

  2. Build AssetBundle FromSelection - No dependency tracking. This is the opposite ofthe previous method and will only include the single asset you haveselected.

For this example, you should create a new prefab. First create anew Cube by going to GameObject -> Create Other ->Cube, 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 windowand select Build AssetBundle From Selection - Trackdependencies. At this point you will be presented with awindow to save the “bundled” asset. If you created a new foldercalled “AssetBundles” and saved the cube asCube.unity3d, your project windowwill now look something like this.

At this point you can move theAssetBundle Cube.unity3d elsewhereon your local storage, or upload it to a server of your choice.

An example of how to change the properties of the assets whenbuilding an Asset Bundle

You can use AssetDatabase.ImportAsset toforce reimporting the asset right beforecalling BuildPipeline.BuildAssetBundle,and then useAssetPostprocessor.OnPreprocessTexture toset the required properties. The following example will show youhow to set different texture compressions when building the AssetBundle.


// 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;
    [MenuItem("Assets/Build AssetBundle From Selection - PVRTC_RGB4")]
    static void ExportResourceRGB4 () {
        textureFormat = TextureImporterFormat.PVRTC_RGB4;
    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.
            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 usingthe AssetDatabase.ImportAssetOptions.

In a test environment, you sometimes need to test a change thatrequire AssetBundles to be rebuilt. In these cases, it is advisableto use the option BuildAssetBundleOptions.UncompressedAssetBundle whenyou build the AssetBundles. This makes it faster to build and loadthe AssetBundles but they will also be bigger and therefore takelonger to download.

Building AssetBundles in a production enviroment

When first using AssetBundles it may seem enough to manually buildthem as seen in the previous example. But as a project grows insize and the number of assets increases doing this process by handis not efficient. A better approach is to write a function thatbuilds all of the AssetBundles for a project. You can, for example,use a text file that maps Asset files to AssetBundle files.

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页