Create assetbundle foreach object in selection

I have a lot of prefabs which I want to export as separate assetbundles when selected in editor, so one bundle for each selected prefab.
The prefabs all share textures which I export before in another separate bundle.

Here´s my code which has an error:

static void Export() {

BuildAssetBundleOptions options = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.DeterministicAssetBundle;

BuildPipeline.PushAssetDependencies();
BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/99_PREFABS/textures.prefab"), null, "AssetBundles/textures.unity3d", options, BuildTarget.iPhone);

Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);

foreach(object g in selection) {
BuildPipeline.PushAssetDependencies();
BuildPipeline.BuildAssetBundle(g, selection,  "AssetBundles/" + g.ToString() + ".unity3d",  options,  BuildTarget.iPhone);
Selection.objects = selection;
BuildPipeline.PopAssetDependencies();
}
BuildPipeline.PopAssetDependencies();
}

There´s an error in line 12:
The best overloaded method match for `UnityEditor.BuildPipeline.BuildAssetBundle(UnityEngine.Object, UnityEngine.Object[ ], string, UnityEditor.BuildAssetBundleOptions, UnityEditor.BuildTarget)’ has some invalid arguments

Why?

does it work with Selection.activeObject instead of g?

Yes, it works, but all exported bundles are identical and contain the same (Selection.activeObject). :frowning:

EDIT: This works:

...
int i=0;
foreach(object g in selection) {
BuildPipeline.PushAssetDependencies();
BuildPipeline.BuildAssetBundle(selection[i], null,  "AssetBundles/" + g.ToString() + ".unity3d", options, BuildTarget.iPhone);         
BuildPipeline.PopAssetDependencies();
i++;
}
1 Like

why not make it

for(int i=0; i<selection.Length; i++)
{
      BuildPipeline.PushAssetDependencies();
      BuildPipeline.AssetBundle(selection[i], selection, "AssetBundles/" + selection[i].ToString()+ ".unity3d, options, BuildTarget.iPhone);
...
}

Right, of course!