- Home /
How do you set asset bundle name on prefabs through code?
Hi,
I've been away from Unity for a year so I'm a bit confused by what has changed :)
I have an old project where I had placed all assets that where to be built into asset bundles in a folder named "AssetBundles". Now I want to migrate this system to the new awesome 5.* asset bundle system. I could go through all the assets manually and set them to: "go_cool.unity3d" etc but I have about 500 of them so I thought I would write a script that would just set the asset bundle name to the same name as the asset for all assets in that folder.
I couldn't figure out how to do this so I though an AssetPostProcessor should do the trick. This seems to work in setting assetImport.assetName on textures. But I can't figure out how to do this on prefabs/gameobjects. The "on post process all assets" is a static method for some reason. The "on pre process model" is not called on game objects. The "OnPostprocessGameObjectWithUserProperties" I can't find any information about how to trigger this one.
Sincerely thankful for any help :)
/J
Did you solved this issue? I need to set/get asset bundles names also through code
Answer by YinXiaozhou · Sep 19, 2016 at 05:44 AM
This should work for any assets(Not scripts maybe).
[MenuItem("Test/ChangeBundleName")]
static void ChangeBundleName()
{
string assetPath = AssetDatabase.GetAssetPath(Selection.activeInstanceID);
AssetImporter.GetAtPath(assetPath).SetAssetBundleNameAndVariant("testBundle", "");
}
made some changes on your code in order to rename a bunch
[$$anonymous$$enuItem("Assets/ChangeBundleName")]
static void ChangeBundleName()
{
var assets = Selection.objects.Where(o => !string.IsNullOrEmpty(AssetDatabase.GetAssetPath(o))).ToArray();
foreach (var a in assets)
{
string assetPath = AssetDatabase.GetAssetPath(a);
AssetImporter.GetAtPath(assetPath).SetAssetBundleNameAndVariant(a.name, "");
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613195800im_/https://answers.unity.com/themes/thub/images/avi.jpg)