Create bundle using only script

How can I create an asset bundle using only script? All the examples I find use "MenuItem", but this script will be in another project, accessed from a built-in app so the editor will not be available to use.

Asset bundles are created using BuildPipeline.BuildAssetBundles, but how do I pass an object as an input via script? I'm using 2021.3 in case that matters (company told me so I have to use this).

I have tried doing something like this

public static void BuildAllAssetBundles()
{
    AssetBundleBuild[] buildMap = new AssetBundleBuild[1];

    buildMap[0].assetBundleName = "pruebundle";

    string[] pruebundle = new string[1];
    pruebundle[0] = "C:\\Users\\user1\\Desktop\\Bundles\\prueba.gltf";

    buildMap[0].assetNames = pruebundle;

    BuildPipeline.BuildAssetBundles("C:\\Users\\user1\\Desktop\\Bundles", buildMap, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
}

but I don't think I'm doing it right. How do you create an asset bundle with ONLY script?


You need to build asset bundles in the project the assets are in and within the Unity editor.

Or in other words:
You cannot build asset bundles without the Unity editor.
You cannot build an asset bundle with assets that are not in the same project as the bundle build script.

Not sure what made you think that you can build asset bundles outside of the project containing the assets, but to the best of my knowledge and even after researching if there may be a way, this does not appear to be possible.

The assets will be in the project, but I want to access them via script to create the bundle instead of clicking on "create bundle" on the editor.

From my app I send the assets to the project, and trigger the script on that project that would read the assets folder and create a bundle of that asset.

Ah okay, in that case you need a way to flag them as part of a bundle via script.

I believe you need to write AssetImporter for these assets and assign the bundle name to the corresponding assetBundleName property:
https://docs.unity3d.com/ScriptReference/AssetImporter.html

Since I refactored the AssetDatabase I made a cheat sheet where you can sort the AssetDatabase methods by category, by sorting the first column (instructions at the top). Here's a quick dump of the bundle methods that let you get the assets by bundle name:

Asset.Bundle All [get] GetAllAssetBundleNames
Asset.Bundle
GetAllDependencies, GetDirectDependencies
GetAssetBundleDependencies
Asset.Bundle GetAllPaths GetAssetPathsFromAssetBundle
Asset.Bundle GetPaths GetAssetPathsFromAssetBundleAndAssetName
Asset.Bundle GetOwningBundle OwningBundle [get] GetImplicitAssetBundleName
Asset.Bundle GetOwningBundleVariant OwningBundleVariant [get] GetImplicitAssetBundleVariantName
Asset.Bundle Unused [get] GetUnusedAssetBundleNames
Asset.Bundle Remove RemoveAssetBundleName
Asset.Bundle RemoveUnused RemoveUnusedAssetBundleNames

1 Like

I found out that my runtime exporting script is wrong, so I couldn't use what I was exporting. I tried with a simple fbx and it worked. Thank you