Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Building an asset bundle from a specific asset bundle label

Discussion in 'Extensions & OnGUI' started by omrip32, Apr 24, 2017.

  1. omrip32

    omrip32

    Joined:
    Jan 4, 2016
    Posts:
    105
    Hi :)
    Let's say I added an asset bundle label to a cube prefab, which I want to create an asset bundle from.
    Is it possible to build an asset bundle from specifically that asset bundle label(instead of building the whole asset bundle databases) ?

    Thanks ;)
     
  2. omrip32

    omrip32

    Joined:
    Jan 4, 2016
    Posts:
    105
    The issue is that if I have a lot of labeled assets in the asset database and I want to build a single one, it will take ages..
     
  3. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,042
    Are you using the AssetBundleManager code that Unity published? i submitted code to that project that does exactly what you want. check it out here: https://bitbucket.org/Unity-Technol...file-view-default#AssetbundlesMenuItems.cs-38

    It works by selecting an asset, right clicking it and cilcking "Build asset bundle from current selection". you can modify that code to build asset bundles for particular labels as well.

    Even if you are not using this manager, you can go over this code and figure out how it is done.
     
  4. omrip32

    omrip32

    Joined:
    Jan 4, 2016
    Posts:
    105
    Great Liortal, thanks :)
    By the way, I figured out a way to do this (don't know if it's similiar to yours). It's inefficient but it works:
    1.I detach the assets that I don't want to build from their labels
    2.I build only the ones I want
    3.I re-attach all the labels back.
     
  5. omrip32

    omrip32

    Joined:
    Jan 4, 2016
    Posts:
    105
    Hi liortal,
    I checked the script - it's really nice - but it lacks something that I need (I might be wrong):
    I want the dependencies (if they are labeled as well) to be built into separate asset bundles.
    For example, if I have a cube with a material ,and both of these have labels, and I select the cube, I would the script to build 2 asset bundles: one for the cube and one for the material (and only those).
    I'm not sure that the script supports this.
    What do you think?

    Thanks:)
     
  6. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,042
    The script doesn't do this at the moment. You will need to see if there is any API that allows you to get the dependencies of a given asset bundles, and then add it to the list of AssetBundleBuild objects. I am not really sure you could do that , but i can check.
     
  7. omrip32

    omrip32

    Joined:
    Jan 4, 2016
    Posts:
    105
    I dont think the API supports it, at least from what I found but it would be great if you can prove me wrong. Anyway, take a look above at the ineffecient - but working - solution I found to the problem. Its not pretty , but it works
     
  8. omrip32

    omrip32

    Joined:
    Jan 4, 2016
    Posts:
    105
    By the way, my solution is done by script of course and not manually
     
  9. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,042
    What you want can be done. I will probably look into that tonight. perhaps it's also a good subject for a new blog post.. what do you think ?
     
  10. omrip32

    omrip32

    Joined:
    Jan 4, 2016
    Posts:
    105
    I agree, and I really appreciate your help!
    By the way, do you live in israel? We are currently looking for talented unity developers:)
     
  11. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,042
    I am already employed. PM me, i might be able to help you with finding someone
     
  12. omrip32

    omrip32

    Joined:
    Jan 4, 2016
    Posts:
    105
    Hey,
    Did you happen to find a better solution for this?
     
  13. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,042
    Hi, didn't have time to look at it. maybe tomorrow.
     
  14. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,042
    Sorry for the long delay, but here it is (you should be using Unity's AssetBundleManager):
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Linq;
    6.  
    7. namespace AssetBundles
    8. {
    9.     public class AssetBundlesMenuItems
    10.     {
    11.         const string kSimulationMode = "Assets/AssetBundles/Simulation Mode";
    12.  
    13.         [MenuItem(kSimulationMode)]
    14.         public static void ToggleSimulationMode()
    15.         {
    16.             AssetBundleManager.SimulateAssetBundleInEditor = !AssetBundleManager.SimulateAssetBundleInEditor;
    17.         }
    18.  
    19.         [MenuItem(kSimulationMode, true)]
    20.         public static bool ToggleSimulationModeValidate()
    21.         {
    22.             Menu.SetChecked(kSimulationMode, AssetBundleManager.SimulateAssetBundleInEditor);
    23.             return true;
    24.         }
    25.  
    26.         [MenuItem("Assets/AssetBundles/Build AssetBundles")]
    27.         static public void BuildAssetBundles()
    28.         {
    29.             BuildScript.BuildAssetBundles();
    30.         }
    31.  
    32.         [MenuItem ("Assets/AssetBundles/Build Player (for use with engine code stripping)")]
    33.         static public void BuildPlayer ()
    34.         {
    35.             BuildScript.BuildPlayer();
    36.         }
    37.  
    38.         [MenuItem("Assets/AssetBundles/Build AssetBundles from Selection")]
    39.         private static void BuildBundlesFromSelection()
    40.         {
    41.             // Get all selected *assets*
    42.             var assets = Selection.objects.Where(o => !string.IsNullOrEmpty(AssetDatabase.GetAssetPath(o))).ToArray();
    43.            
    44.             HashSet<string> processedBundles = new HashSet<string>();
    45.  
    46.             var assetBundleBuilds = GetBuildsForPaths (assets, processedBundles);
    47.            
    48.             BuildScript.BuildAssetBundles(assetBundleBuilds.ToArray());
    49.         }
    50.  
    51.         [MenuItem("Assets/AssetBundles/Build AssetBundles from Selection (including dependencies)")]
    52.         private static void BuildBundlesFromSelectionWithDependencies()
    53.         {
    54.             // Get all selected *assets*
    55.             var assets = Selection.objects.Where(o => !string.IsNullOrEmpty(AssetDatabase.GetAssetPath(o))).ToArray();
    56.  
    57.             HashSet<string> processedBundles = new HashSet<string>();
    58.  
    59.             var assetBundleBuilds = GetBuildsForPaths (assets, processedBundles);
    60.  
    61.             foreach (var o in assets)
    62.             {
    63.                 var paths = AssetDatabase.GetDependencies (new[] { AssetDatabase.GetAssetPath (o) });
    64.  
    65.                 assetBundleBuilds = assetBundleBuilds.Concat(GetBuildsForPaths (paths.Select (p => AssetDatabase.LoadAssetAtPath<Object> (p)).ToArray (), processedBundles)).ToList();
    66.             }
    67.  
    68.             BuildScript.BuildAssetBundles(assetBundleBuilds.ToArray());
    69.         }
    70.  
    71.         private static List<AssetBundleBuild> GetBuildsForPaths(Object[] assets, HashSet<string> processedBundles)
    72.         {
    73.             List<AssetBundleBuild> assetBundleBuilds = new List<AssetBundleBuild>();
    74.  
    75.             // Get asset bundle names from selection
    76.             foreach (var o in assets)
    77.             {
    78.                 var assetPath = AssetDatabase.GetAssetPath(o);
    79.                 var importer = AssetImporter.GetAtPath(assetPath);
    80.  
    81.                 if (importer == null)
    82.                 {
    83.                     continue;
    84.                 }
    85.  
    86.                 // Get asset bundle name & variant
    87.                 var assetBundleName = importer.assetBundleName;
    88.                 var assetBundleVariant = importer.assetBundleVariant;
    89.                 var assetBundleFullName = string.IsNullOrEmpty(assetBundleVariant) ? assetBundleName : assetBundleName + "." + assetBundleVariant;
    90.  
    91.                 // Only process assetBundleFullName once. No need to add it again.
    92.                 if (processedBundles.Contains(assetBundleFullName))
    93.                 {
    94.                     continue;
    95.                 }
    96.  
    97.                 processedBundles.Add(assetBundleFullName);
    98.  
    99.                 AssetBundleBuild build = new AssetBundleBuild();
    100.  
    101.                 build.assetBundleName = assetBundleName;
    102.                 build.assetBundleVariant = assetBundleVariant;
    103.                 build.assetNames = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleFullName);
    104.  
    105.                 assetBundleBuilds.Add(build);
    106.             }
    107.  
    108.             return assetBundleBuilds;
    109.         }
    110.     }
    111. }
    Let me know if it works for you :)
     
    trappist-1 likes this.
  15. omrip32

    omrip32

    Joined:
    Jan 4, 2016
    Posts:
    105
    Thanks, I'll check it out and let you know :):)
     
  16. unicoea

    unicoea

    Joined:
    Feb 18, 2013
    Posts:
    53
    Hi
    Nice script!
    I try it. But the message shows:
    "Assets/ScriptsForAssetBundleSystem/Editor/AssetbundlesMenuItems.cs(46,15): error CS1501: No overload for method `BuildAssetBundles' takes `1' arguments"

    How can i fix it?

    Thank you!
    unicoea
     
  17. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,042
    Which Unity version are you running that on?