Search Unity

How to programmatically create AssetBundles in editor Scripts

Discussion in 'Asset Bundles' started by Chico3001, May 25, 2020.

  1. Chico3001

    Chico3001

    Joined:
    Mar 10, 2018
    Posts:
    8
    Do you know if is there a way to add/remove objects using editor scripts to/from AssetBundles?

    i have found how to list objects, names, even delete AssetBundles names using AssetDatabase, but i can't see how to create or delete AssetBundles names nor how to add objects into an existing AssetBundle




    Or do you know if there is another way to add or delete objects to AssetBundles using scripts?

    Regards!!!
     
  2. I have no good answer for you, I haven't done any scripting builds and stuff like that, but just tips in case you missed them.
    1; Check on the Scriptable Pipeline, maybe there are some answers for you there:
    https://docs.unity3d.com/Packages/com.unity.scriptablebuildpipeline@1.7/api/index.html
    2; Try the Build Pipeline subforum, more people who does these things or at least try, are there.
     
  3. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Adding new asset bundle names - simply tag any asset with the new asset bundle name you want. it will be added to that list.

    Regarding adding objects to asset bundles - what do you mean by that? an asset bundle is built so u cannot simply "add a new asset into it" without rebuilding.
     
  4. Chico3001

    Chico3001

    Joined:
    Mar 10, 2018
    Posts:
    8
    No... I do know how to manually create an AssetBundle, what i'm looking for is to create an automatization script, in witch the user just need to press a button and the script automatically downloads the required names from a server, organizes the objects, tags them, adds them to the AssetBundle, creates the Assetbundle and uploads it to the server

    I have almost everything finished except where the script must desassign and delete unused Assetbundles, create the new AssetBundle Name and Variant, add the required objects and start packing the new AssetBundle
     
  5. steinbitglis

    steinbitglis

    Joined:
    Sep 22, 2011
    Posts:
    254
  6. onur-gg

    onur-gg

    Joined:
    Dec 14, 2015
    Posts:
    12
  7. Koyae

    Koyae

    Joined:
    Feb 5, 2015
    Posts:
    3
    As the above user implied, you can achieve this using a combination of the static method
    UnityEditor.AssetImporter.GetAtPath()
    followed by using the AssetImporter object this returns to call
    SetAssetBundleNameAndVariant()
    or by just setting the
    assetBundleName
    and/or
    assetBundleVariant
    field(s) directly.

    A simple implementation might look like this:

    Code (CSharp):
    1. public class BundleNamer : MonoBehaviour {
    2.     public void Start() {
    3.       #if UNITY_EDITOR
    4.         var importer = UnityEditor.AssetImporter.GetAtPath("Assets/Prefabs/x.prefab");
    5.         importer.assetBundleName = "bundle_1";
    6.       #endif
    7.     }
    8. }
    In more complicated scenarios, you'd probably actually loop over the files of certain asset-folders, and bundle them according to the name of the parent folder or something. Finer specifics depend on how your project is set up.

    The
    #if UNITY_EDITOR
    and
    #endif
    lines are not strictly necessary but will save you from having to remove the component that refers to this script every time you build, since having anything from
    UnityEditor
    will make your build fail when you try to export your project to EXE, WebGL, etc.
     
    Last edited: Apr 1, 2021
  8. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    For editor scripts that automatically bundle different assets into asset bundles, i discourage the usage of "tagging" assets in the project with an asset bundle name and variant.

    Instead, i'd go with the overload of BuildPipeline.BuildAssetBundles that takes an array of AssetBundleBuild objects (see the docs here, in the bottom part of the page).

    That page even has a short code sample to follow, to demonstrate how this is done:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public class BuildAssetBundlesBuildMapExample : MonoBehaviour
    5. {
    6.     [MenuItem("Example/Build Asset Bundles Using BuildMap")]
    7.     static void BuildMapABs()
    8.     {
    9.         // Create the array of bundle build details.
    10.         AssetBundleBuild[] buildMap = new AssetBundleBuild[2];
    11.  
    12.         buildMap[0].assetBundleName = "enemybundle";
    13.  
    14.         string[] enemyAssets = new string[2];
    15.         enemyAssets[0] = "Assets/Textures/char_enemy_alienShip.jpg";
    16.         enemyAssets[1] = "Assets/Textures/char_enemy_alienShip-damaged.jpg";
    17.  
    18.         buildMap[0].assetNames = enemyAssets;
    19.         buildMap[1].assetBundleName = "herobundle";
    20.  
    21.         string[] heroAssets = new string[1];
    22.         heroAssets[0] = "char_hero_beanMan";
    23.         buildMap[1].assetNames = heroAssets;
    24.  
    25.         BuildPipeline.BuildAssetBundles("Assets/ABs", buildMap, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
    26.     }
    27. }
     
    catallergy, TWalden and JB_JHT like this.