Friday, 29 June 2018

Creating Asset bundle for device specific in Unity

1. Create a new project in unity
2. Create a new folder “Editor” under Assets folder






3. Create a new script as ExportAssetBundles.cs under Editor
4. Copy and paste the below code, you can tweak the below to create asset bundle for specific devices.

using UnityEngine;
using UnityEditor;

public class ExportAssetBundles
{

    [MenuItem("Assets/Build AssetBundle From Selection - Track dependencies")]
    static void ExportResurce()
    {
        // Bring up save panel
        string basename = Selection.activeObject ? Selection.activeObject.name : "New Resource";
        string path = EditorUtility.SaveFilePanel("Save Resources", "", basename, ""); // use this to save in particular folder destination
        //string path = "Assets/AssetBundles/test.prefab";

        if (path.Length != 0)
        {
            // Build the resource file from the active selection.
            Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);

            // for Android
          /*  BuildPipeline.BuildAssetBundle(Selection.activeObject,
                                           selection, path + ".android.unity3d",
                                           BuildAssetBundleOptions.CollectDependencies |
                                           BuildAssetBundleOptions.CompleteAssets,
                                           BuildTarget.Android);

            // for iPhone
            BuildPipeline.BuildAssetBundle(Selection.activeObject,
                                           selection, path + ".iphone.unity3d",
                                           BuildAssetBundleOptions.CollectDependencies |
                                           BuildAssetBundleOptions.CompleteAssets,
                                           BuildTarget.iOS);

            // for WebPlayer
            BuildPipeline.BuildAssetBundle(Selection.activeObject,
                                           selection, path + ".unity3d",
                                           BuildAssetBundleOptions.CollectDependencies |
                                           BuildAssetBundleOptions.CompleteAssets,
                                           BuildTarget.WebPlayer);*/

            // for WebPlayer
            BuildPipeline.BuildAssetBundle(Selection.activeObject,
                                           selection, path + ".unity3d",
                                           BuildAssetBundleOptions.CollectDependencies |
                                           BuildAssetBundleOptions.CompleteAssets,
                                           BuildTarget.StandaloneWindows);

            Selection.objects = selection;
        }
    }

5. Now you will be able to see a new option under Assets at the very bottom as “Build AssetBundle From Selection”
6. Now go to hierarch view and create a 3D cube.
7. Create a new prefabs folder under Assets
8. Drag the cube on to the prefabs folder, now you will be able to see a cube prefab
9. Select the cube and execute the “Build AssetBundle From Selection” option under Assets tab.
10. Now you will be able to see an popup prompting to select the destination folder to save the asset bundle
11. That’s it you have now created an Asset bundle


No comments:

Post a Comment