Search Unity

► Exclude from build: files, objects, components ◄

Discussion in 'Assets and Asset Store' started by _geo__, May 27, 2022.

  1. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,345
    Hi all,
    I have made an asset which allows you to exclude anything from your build (file, folder, script, GameObject, Component). It's super easy (just right-click and hit "exclude from build").

    ExcludeFromBuildTeaser.gif

    > Asset Store Page <
    https://assetstore.unity.com/packages/tools/utilities/exclude-from-build-222791?aid=1100lqC54
    Full Source Code included.

    Here is the thread which triggered me to grow my home made tool into a full featured asset:
    https://forum.unity.com/threads/excluding-folders-from-build.408375/page-2

    Features
    > Can exclude any file (yes scripts files too), GameObjects or Components
    > Test Mode (allows you to see your changes withou having to start a build)
    > Allows you to make multiple configurations per platform (called "groups")
    > FullSource code included
    > Overview window with handy shortcuts (so you always know which things are excluded and where to find them). You can also scan scenes for excluded objects or components.

    ExcludeFromBuildWindow.png
    If you want to know more then here is the manual (pdf):
    https://kamgam.com/unity/ExcludeFromBuildManual.pdf

    Hope you'll find it useful :)
     
    Last edited: Feb 6, 2023
    CosmoM and Lars-Steenhoff like this.
  2. like_a_guest

    like_a_guest

    Joined:
    Aug 6, 2018
    Posts:
    2
    Hey, can you use this on folders in packages too?
    Currently having a major issue with a project, that includes multiple build types on the android platform. One library from a package seems to get included and mess with the rendering pipeline, so one build type does not work anymore. Its a .o library inside a package, that is included via package manager. Would this tool be able to exclude the file from build?
     
  3. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,345
    Hi, good question.

    Sadly no. The tool only works on things WITHIN the Assets/ folder. Though package support seems to be an often requested feature. I have it on my todo list to look into it (no promises though).

    One way to solve this manually could be to turn the package into and embedded package and then use #ifdefs to comment out the problematic code segments. Caveat: this does require you to redo the changes for every package update. It's not very maintenance friendly.

    Another solution would be to use a different package manager manifest file for each platform (just an idea, I have not yet tried this myself).

    Sorry I don't have better news for you. I hope this answers your question.
     
    Last edited: Nov 3, 2022
  4. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,527
    Very interesting asset!

    I often make a build in a project with a lot of textures that are not needed/used in the build, but they are in the project as part of an asset that I got from the store, now the thing that unity does is the following, it compresses each and every texture in the project when making a build, taking a long time and if those textures are not even part of the build thats a wast of time and disk space. Do you think that with this asset will allow the compressing of those not part of the build textures will be skipped so I can have faster builds? Thanks
     
  5. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,345
    Hi,
    thank you.

    They way it works is that if you start a build the tool will hide all folders and files which are marked as excluded BEFORE the build starts. The files are then no longer in the AssetDatabase. Then Unity builds. Afterwards the files are made visible again.

    People reported significant build size reductions due to excluded textures. So I think, yes, it should do what you want. Caveat: I have not yet tested this exact (texture compression) use case.

    I am curious: I usually only see long lasting texture tasks after a platform switch, reimport or very first build. I guess you are already using the cache server?
     
    Last edited: Dec 20, 2022
    Lars-Steenhoff likes this.
  6. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,527
    I don’t use the cache server, and yes I do platform switch a lot, I also want to save on disk space by not compressing whats not needed in the build. I will buy the tool as it seems perfect for it.

    In thing that would make it even better if it could detect scene dependencies of textures, but I understand that may be out of scope for this asset.
    Thanks
     
  7. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,345
    I fear I don't quite understand what you want the tool to do here. There are some really good "reference finder" Assets out there which help visualizing what is referencing what. Not sure if that is what you want though.

    I don't mean to push it onto you but I really recommend using it. It makes a big difference. It's one of the first things I enable for any project. Otherwise platform switching is just such a pain.
     
  8. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,527
    One thing that will be nice to have is a hiding set, that I can enable / disable
    Yea I was thinking about a reference finder that automaticity set the excude tag to a folder if not referenced by the scenes in the build

    I see there is now https://docs.unity3d.com/Manual/UnityAccelerator.html accelerator as cache server only works on older unity versions. I will have a look again there. bit its still wasting a lot of disk space that I rather keep on my system SSD. thanks for the tips.
     
  9. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,345
    Ah okay, now I think I understand. Though in builds these unreferenced things should not be included (or processed) anyways (unless they are in a special folder like Resources, StreamingAssets).

    It might be a handy feature to "hide" things in the Editor to keep the project clean after importing 3rd party assets. I'll think about it. But I guess this would become a new asset since it's not really in the scope of "exclude from build".

    Thanks for the feedback :)
     
    Lars-Steenhoff likes this.
  10. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,527
    Or textures, they are always compressed no matter if they are in the build or not.

    I think unity has some plans to change this later. but seems to take forever to get released.
    First Link here #8
    Second one #6
    https://forum.unity.com/threads/exc...build-from-compression-at-build-time.1317945/

     
    _geo__ likes this.
  11. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,345
    I understand your problem and I agree that asset importing is a bit of a pain.

    I think the key part is "The compressed result is still an import artifact and it is cached in the asset database ..". Usually once you have the cache server (accelerator) enabled and you have built at least once for every target then you should have cache hits for about 99% of all textures and import times won't play a big role anymore (for building or platform switchting). But I am not entirly sure how the "uncompressed texture" workflow works (I rarely use uncompressed textures).

    I assume every time you do a platform switch the import artefacts are thrown away unless you have a cache server where they are stored. My understanding of the cache server is that it exists to retain the imported results and share them among coworkers. In your case (no cache server) all those textures have to be reimported at build time. I think you will have massive gains in build times once you switched to the cache server / accelator workflow.

    I think the topic may be a little out of scope for this thread though.
    Maybe continue in the original thread you linked?
     
    Lars-Steenhoff likes this.
  12. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,345
    Answer to a question raised in another thread:

    At the moment these are not configurable. I'll add a "can be moved to other location" feature to my list of things to investigate.

    However, if you want them to be located somewhere else you can look into to source files (ExcludeFromBuildData.cs and ExcludeFromBuildSettings.cs). Right at the start you will find the locations:

    upload_2023-5-8_17-5-29.png

    upload_2023-5-8_17-5-43.png

    Hope this helps anyone who wants to change the default location of the config files.
     
    ZarktorStudios likes this.
  13. Starbox

    Starbox

    Joined:
    Sep 17, 2014
    Posts:
    470
    Many thanks for the clarification.

    Three more things.

    1. The red thingy that's displayed on an excluded folder is a bit small. Could there be an option to switch it for a bigger sprite, especially for users who rely on small one-column displays in the Project tab?

    2. Also, the red sprite is displayed or not depending on the excluding group that is currently selected. Which means that unless we select the right group (the active one), it's totally possible to miss the fact that a folder is excluded by looking at the project tab directly. I think it would be great that another sprite reflects that status, but that comes in another color, like that of an inactive state, if the corresponding group is not selected.

    3. With this in mind, the right-click button may open a menu that displays the first function, exclude from build, and then underneath it, a list of all saved groups in which this folder will be excluded during the building, just to go faster and avoid having to look into all the groups manually in the EFB window.
     
    _geo__ likes this.
  14. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,345
    Hi, thanks for the feedback. I have written down your suggestions on my list of things to investigate (no promises though). The part about non-active groups not being shown is a good point.

    The icons themselves are based on internal Unity icons so I don't (yet) know if there are some variations with different colors. I'll have to check.

    They are drawn in the ProjectWindowDecorations.cs file.
    As a quick fix you could change the sizes like this:

    upload_2023-5-24_13-35-40.png

    To show the icon for all groups you could add a new method to the ExcludedFromBuildData, like this:
    Code (CSharp):
    1. public bool IsExcludedInAnyGroup(string guid)
    2. {
    3.    if (Groups == null || Groups.Count == 0)
    4.        return false;
    5.  
    6.    foreach (var group in Groups)
    7.    {
    8.        if (group.ExcludedObjects == null || group.ExcludedObjects.Count == 0)
    9.            continue;
    10.  
    11.        foreach (var obj in group.ExcludedObjects)
    12.        {
    13.            if (obj.AssetGUID == guid)
    14.                return true;
    15.        }
    16.    }
    17.  
    18.    return false;
    19. }
    And then again in the ProjectWindowDecorations.cs replace the IsExcluded with IsExcludedInAnyGroup.
    upload_2023-5-24_13-45-18.png

    That would show it for all groups and would not differentiate between the current or the other groups but it's a start.

    I hope this helps you out for now.
     
  15. Starbox

    Starbox

    Joined:
    Sep 17, 2014
    Posts:
    470
    Right, thanks.
     
  16. _eternal

    _eternal

    Joined:
    Nov 25, 2014
    Posts:
    304
    I always get this error when I create builds:

    Code (CSharp):
    1. ExcludeFromBuild: An error occurred while renaming files or folders: Access to the path '<path>' is denied. Path: '<path>'
    2.  
    3. ExcludeFromBuild: Maybe your IDE is locking some files. Please try to close it or make it unlock the files you are excluding.
    4.  
    5. ExcludeFromBuild: Renaming files or folders FAILED. Aborting and reverting. NOTICE: no files or folders will be excluded in this build!
    Is there any way for this asset to work while Visual Studio is opened? Maybe there's a way to tell VS to ignore certain files? This would be a huge limitation otherwise, because you normally wouldn't close VS every time you create a build (in fact, I usually forget about this until I see the error).
     
  17. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,345
    Hi, sometimes it is the windows defender that locks the files not VS. Could you please try adding your Unity project to the exclusions list.

    Usually having VS open is fine. I have just done a little test build with VS 2022 and it worked. Sadly this issue comes and goes. I do not really know when/why VS sometimes keeps locking some files so I am not sure how to resolve this in a better way (sorry).
     
  18. _eternal

    _eternal

    Joined:
    Nov 25, 2014
    Posts:
    304
    All right, thanks for your reply.

    It looks like my project folder was already excluded in Windows Defender. I know what you mean, though — file locking seems kind of random and hard to debug. I'll see if the problem is consistent or not.
     
    _geo__ likes this.
  19. sampenguin

    sampenguin

    Joined:
    Feb 1, 2011
    Posts:
    64
    Great asset, thanks for making it!

    Hoping you can answer a question before I go digging in source to figure it out... is there a way to easily query the Active Group selection from code to do some other branching in post-process build scripts? It would be great to be able to use this as a singular control variable for both OS and distribution platform logic in my build scripts (instead of the usual messy #defines approach).

    e.g. psuedo code
    Code (CSharp):
    1.  
    2. if (ExcludeFromBuild.ActiveGroup = "SteamWin64")
    3. {
    4.     // Don't run this post process build script
    5.     return;
    6. }
    7.  
    8. if (ExcludeFromBuild.ActiveGroup = "AndroidAmazon")
    9. {
    10.    // Do something specific for this build config
    11. }
    12.  
     
  20. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,345
    Hi,
    yes, you are actually close with your code.

    It's all stored in the ExcludeFromBuildData object which you can access via the static method
    ExcludeFromBuildData.GetOrCreateData()
    .

    Code (CSharp):
    1. if (ExcludeFromBuildData.GetOrCreateData().CurrentGroup.Name == "Whatever")
    2. {
    3.     // Don't run this post process build script
    4.     return;
    5. }
    The current group is an object with some properties. I think what you would want is the "Name" or maybe the "BuildTargets" to make it independent of the name.

    upload_2023-11-10_21-11-41.png

    Hope that helps. Feel free to ask again if you need anything :)
     
    Last edited: Nov 10, 2023
    sampenguin likes this.
  21. sampenguin

    sampenguin

    Joined:
    Feb 1, 2011
    Posts:
    64
    Thanks, I did end up finding that in the source so I can branch like this


    Code (CSharp):
    1.  
    2. var data = ExcludeFromBuildData.GetOrCreateData();
    3. Debug.Log($"[Build] Running custom post build scripts for {data.CurrentGroup.Name}...");
    4. switch (data.CurrentGroup.Name)
    5. {
    6.  
     
  22. sampenguin

    sampenguin

    Joined:
    Feb 1, 2011
    Posts:
    64
  23. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,345
    Hi, thank you for reporting this. I don't have access to my Pc today and tomorrow but I'll take a look asap. In the meantime please feel free to post or send me any more infos you find related to the issue.
     
  24. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,345
    Update: I have reproduced the issue and fixed it. Please contact me via PM or email (office@kamgam.com) and I will send you the new version for testing. Once I have your confirmation it works I will push it for all users.

    Hint: You can avoid this now if you DOCK the window. The reason for the vanishing Assets menu is that the "Exclude from Build" entry in it refreshes the Exclude Window which then triggers a re-focus of the window which closes all drop downs in the editor. If the window is docked it does not do that. - This is just a quick fix. In the real patch it works not matter if the the windows is docked or not.