To build assets into an AssetBundle, you must first assign assets to an AssetBundle, either in the Unity Editor or through a script. You can then create and use a script to build the AssetBundles. For information on the best approaches for organizing assets into AssetBundles, refer to Organizing assets into AssetBundles.
Note: This workflow describes the creation of AssetBundles with the built-in BuildPipeline.BuildAssetBundles API. An alternative is to use the Addressables package, which is built on top of AssetBundles and provides a UI to organize the assets in your project.
To assign a given asset to an AssetBundle in the Unity Editor, perform the following steps:
Assets folder (Project tab) More info/ character. For example, use the AssetBundle name environment/forest to create an AssetBundle named forest under an environment subfolder.You can assign an AssetBundle to a folder in your project. By default, all assets in that folder are assigned to the same AssetBundle as the folder. However, the AssetBundle assignments for individual assets takes precedence. To assign an AssetBundle to a folder:
You can also select multiple assets and assign them to an AssetBundle. However, assigning assets to an AssetBundle in this way overrides any existing AssetBundle assignments for those assets.
To assign assets to AssetBundles in your code, use the BuildPipeline.BuildAssetBundles method with an array of AssetBundleBuild structures. This approach overrides any AssetBundle assignments made in the Inspector.
If you want your script to respect AssetBundle assignments made in the Inspector, use AssetDatabase.GetAllAssetBundleNames and AssetDatabase.GetAssetPathsFromAssetBundle to retrieve the necessary information and populate the AssetBundleBuild array.