Search Unity

Question Is there a specification for build dependencies?

Discussion in 'Asset Database' started by D1234567890, Oct 13, 2022.

  1. D1234567890

    D1234567890

    Joined:
    Aug 11, 2019
    Posts:
    65
    I need to grab all shader variants from a shader that will be used in a build to do shader variant stripping in a different shader in OnProcessShader. I am trying to work it out by gathering all materials used in a build the same way Unity would. I could not find a reference as to how Unity gathers everything. This is what I have so far:

    SCENE

    Resources.FindObjectsOfTypeAll FAIL
    Finds anything in memory which can include materials outside of the scene.

    AssetDatabase.GetDependencies FAIL
    Passing the scene to this method can get more materials than Unity would if recursive is true and less if recursive is false.

    RESOURCES

    Resources.LoadAll PASS
    This will get everything in the Resources folder.

    ADDRESSABLES

    I am not sure how to handle these.

    SHADER VARIANT COLLECTION

    I might also need access to any of these but there doesn't appear to be a public API.

    ETC

    I may have missed some other approaches.

    So is there a specification on how Unity gathers assets to be put into a build?
     
  2. AndrewSkow

    AndrewSkow

    Unity Technologies

    Joined:
    Nov 17, 2020
    Posts:
    91
    I am not aware of a document that specifies exactly how shader usage is calculated in a build, although it does sound like a good idea to put together!

    Conceptually you are on the right track but I think there are a lot of details that would make it difficult to come up with exactly the same results, in all cases, as what the build would calculate. For example when finding usage of a shader, the build needs to exclude parts of the heirarchy that are flagged as "Editor only", and also exclude fields that are compiled out (with scripting defines) on the target platform. So searching materials that are used in the Editor view of the assets might include Materials that are actually stripped out of the build. And sometimes Materials that are in the built-in resources can be making it into a build and influence the Shader usage.

    The Shader Variant Collection is used to add usage keywords explicitly, e.g. for cases where the expected keywords are not being picked up by the build's processing. I think that is typically useful with AssetBundles, where only the usage within the scope of the single AssetBundle is considered. So a Material in one AssetBundle won't influence a Shader in another AssetBundle, and its usage keywords might need to be added explicitly using a ShaderVariantCollection in the Shader's AssetBundle. Meanwhile AssetBundles built for Addressables (with the Scriptable Build Pipeline) do consider usage across all AssetBundles. And a shader might also end up repeated in multiple AssetBundles.

    Sorry that is not a complete description, but hopefully gives you some idea of some of the considerations that go into the build's Shader variant calculations.
     
    D1234567890 likes this.
  3. D1234567890

    D1234567890

    Joined:
    Aug 11, 2019
    Posts:
    65
    @AndrewSkow Thanks for the reply.

    Any chance of there being a document made? I am finding this would also be useful for components too which I opened a related feature request for: https://forum.unity.com/threads/onbuild-callback.1418797/

    Would be very useful to know what goes into a build and to be able to act upon it somehow. I find it to be a pretty fundamental restriction for QoL improvements (automating laborious tasks).