Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Set Asset Bundle Name form Editor Menu Item Script

Discussion in 'Editor & General Support' started by alejobrainz, Jun 13, 2015.

  1. alejobrainz

    alejobrainz

    Joined:
    Sep 10, 2005
    Posts:
    288
    Hello. I'm trying to add a new editor menu item script to set the asset bundle name to match the file name on a selected set of files, but I have not found any way to do so. I'm looking at the AssetDatabase class but there are a few methods to get Asset Bundle Names, but none to set them and assign certain objects to that asset bundle name.

    Please advice,

    Alejandro.
     
    Last edited: Apr 6, 2016
  2. alejobrainz

    alejobrainz

    Joined:
    Sep 10, 2005
    Posts:
    288
    Figured it out. I had to use the AssetImporter Class:


    [MenuItem("AssetBundles/Set Asset Bundle From File Name",false, 0)]
    staticvoidSetAssetBundlesFromFileNames()
    {
    if (Selection.assetGUIDs.Length > 0) {
    foreach (ObjectassetinSelection.objects) {
    stringpath = AssetDatabase.GetAssetPath (asset);
    AssetImporterassetImporter = AssetImporter.GetAtPath (path);
    assetImporter.assetBundleName = asset.name;
    Debug.Log (Selection.assetGUIDs.Length + " Asset Bundles Assigned");
    }
    } else {
    Debug.Log ("No Assets Selected");
    }
    }
     
    kapiel, timsp, Zagule and 6 others like this.
  3. Zagule

    Zagule

    Joined:
    Aug 23, 2016
    Posts:
    4
    5 years later and it's still very useful. Thanks!