Search Unity

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,156
    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.
     
    andrew_pixelnauts likes this.
  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,156
    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,156
    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,156
    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,156
    Hi, didn't have time to look at it. maybe tomorrow.
     
  14. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,156
    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:
    54
    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,156
    Which Unity version are you running that on?
     
  18. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,108
    Apologies for the necro. I see the work you did @liortal is on BitBucket, while their released version they mention is on GitHub. I can't even see the class 'AssetBundlesMenuItems' on the GitHub version. Not only that, but BuildPipeline.BuildAssetBundle(string name) is now labelled as obsolete in Unity. Any updates on where this is at?
     
  19. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,156
    Haven't followed up on this subject in a while. Share the link to github so I can see what u are referring to.
     
  20. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,108
    Well look further into it myself, the Asset Bundle Manager is fully deprecated now and replaced by the Asset Bundle Browser, which doesn't have that feature. And then again that's being replaced by Addressable Assets, coming next year to preview packages at some point. Jeeze I can't keep up lol.

    Asset Store (With Links To The GitHub): https://assetstore.unity.com/packages/tools/utilities/asset-bundle-browser-93571
    GitHub Repo: https://github.com/Unity-Technologies/AssetBundles-Browser
    Addressables Youtube:

    Adressables Forum: https://forum.unity.com/threads/addressables-are-here.536304/page-6
     
  21. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,156
    the AssetBundleManager was a simple reference implementation for a component that manages loading / unloading of asset bundles. you can still use it, even if it's deprecated or whatever.

    The asset bundle browser is just an editor tools for viewing and building asset bundles from what i know. I haven't used it myself.

    You woud like to build a specific asset into an asset bundle? is that the functionality you want to add ?
     
  22. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,108
    Well really it's just that I'd like to build one asset bundle out of the many many many asset bundles I have. As I may make changes to the content of only one of them, and building that single asset bundle will take me what, 2 minutes? But it will re-build all of them and take out the computer for an hour odd lol.
     
  23. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,156
    i will have a look, maybe i can fix something for u and submit a pull request for that. seems like pretty simple functionality.
     
    Nigey likes this.
  24. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,108
    Oh wow thanks man! I wasn't asking for that at all, but am super grateful lol.
     
  25. multimediamarkers

    multimediamarkers

    Joined:
    Feb 17, 2016
    Posts:
    22
    Any update on this topic?