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

Custom BuildConfiguration Components

Discussion in 'Editor Workflows' started by sbsmith, May 1, 2021.

  1. sbsmith

    sbsmith

    Joined:
    Feb 7, 2013
    Posts:
    126
    Is there any documentation for making custom components for build configurations? I'd like to implement a custom Output Build Directory that uses environment variables in the path. When I try to implement IBuildComponent the compiler doesn't recognize it. The API docs looks like I only need to "using Unity.Build". Is it possible to make custom components? Am I missing something simple?
     
    andreiagmu and Lukas_Kastern like this.
  2. sbsmith

    sbsmith

    Joined:
    Feb 7, 2013
    Posts:
    126
    The simple thing that I was missing is that this feature is not compatible with Unity 2021 yet and will not be until the end of 2021.
     
  3. andreiagmu

    andreiagmu

    Joined:
    Feb 20, 2014
    Posts:
    175
    I'm using Unity 2020.3.19f1.
    In my case, I needed a custom BuildComponent to auto-build some Asset Bundles before the main build process.
    Here's a guide on how I did it:

    - I had to reference the assemblies Unity.Build and Unity.Build.Classic, to avoid compiler errors. (you may need to reference other Unity.Build assemblies, depending on what you want to do)
    The catch is that those Unity.Build assemblies aren't auto-referenced, so you NEED to create a custom assembly definition.

    - Create a new .asmdef file (Create -> Assembly Definition) and add the Unity.Build references you need there, in the Assembly Definition References list.

    - IMPORTANT: Put the next scripts in the same folder (or a subfolder) of your .asmdef file, to make them belong to your custom assembly.

    - I created a simple custom BuildComponent, BuildAssetBundlesSettings:
    Code (CSharp):
    1. using Unity.Build;
    2.  
    3. namespace YourGame.Build
    4. {
    5.     public class BuildAssetBundlesSettings : IBuildComponent
    6.     {
    7.         public bool BuildAssetBundles;
    8.     }
    9. }
    - Now you can already add that custom component to your build configuration. But a warning icon will appear, with the message "This component is not used by the build pipeline".

    - So, I had to create a new script, GameBuildCustomizer, that implements ClassicBuildPipelineCustomizer:
    Code (CSharp):
    1. using System;
    2. using Unity.Build.Classic;
    3.  
    4. namespace YourGame.Build
    5. {
    6.     public class GameBuildCustomizer : ClassicBuildPipelineCustomizer
    7.     {
    8.         // This code tells the build pipeline that it should use our custom build component.
    9.         public override Type[] UsedComponents { get; } =
    10.         {
    11.             typeof(BuildAssetBundlesSettings)
    12.         };
    13.  
    14.         // This code will execute before the main build process.
    15.         public override void OnBeforeBuild()
    16.         {
    17.             base.OnBeforeBuild();
    18.  
    19.             // Use our custom BuildComponent.
    20.             if (Context.TryGetComponent<BuildAssetBundlesSettings>(out var buildAssetBundlesComponent))
    21.             {
    22.                 if (buildAssetBundlesComponent.BuildAssetBundles)
    23.                 {
    24.                     // Build Asset Bundles here (this will run before main build)
    25.                 }
    26.             }
    27.         }
    28.     }
    29. }
    - After adding that script, the warning icon will be gone, and I can use my custom BuildComponent to toggle if I want to auto-build my Asset Bundles (after clicking "Build" or "Build and Run") on my BuildConfiguration.

    That's it for my guide! :D
     
    Last edited: Sep 25, 2021
    Oneiros90 likes this.
  4. Oneiros90

    Oneiros90

    Joined:
    Apr 29, 2014
    Posts:
    78
    Thank you for your guide @andreiagmu! Do you think that the code could be adapted to perform post-build operations? I want to create a
    ZipComponent
    that zips my build files when it is done
     
  5. andreiagmu

    andreiagmu

    Joined:
    Feb 20, 2014
    Posts:
    175
    I'm not sure, but I think that's possible to do. To be honest, I don't use BuildConfiguration/BuildComponents anymore.
    Did you check if there's any
    OnAfterBuild()
    override, similar to the
    OnBeforeBuild()
    I used in my example?

    Glad my guide helped you! :D
     
  6. Oneiros90

    Oneiros90

    Joined:
    Apr 29, 2014
    Posts:
    78
    Unfortunately there's no
    OnAfterBuild()
    , that's why I asked :(