Unexpected bundle change due to material

Yesterday, I found there are some assetbundles changed between the two builds of same svn revision. After some investigation, I found they changed becasue there are more properties in the materials which are included in these assetbundles. These properties are added to a shader (Used by these materials) last week. After that, we did builds many times and didn’t modify these materials, but the assetbundles changed yesterday for an unkown reason.

I tried to reproduce this issue in a small project, and found something unexpected, here are steps:

  • Created a default unlit shader

  • Created a material, use the shader above

  • Set the shader and material addressable, and put them into two groups

  • Built Addressables, two assetbundles were generated (Assume BundleS for the assetbundle containing the shader, BundleM for the assetbundle containing the material)

  • Added a color property to the shader

  • Rebuilt Addressables, BundleS changed, but BundleM didn’t change

  • Reimported the material

  • Rebuilt Addressables, nothing changed

  • Clean Addressables and SBP cache

  • Rebuilt Addressables, BundleM changed

I expected cleaning cache dosen’t affect the generated assetbundles. So for steps above, there are two possibilities:

  • In step 6, BundleM also changed (So it won’t change after cleaning cache)
  • Or, in step 10, BundleM didn’t change

My questions are:

  • Is this an expected behaviour?
  • Dose someone have some workarounds?

Finally, I implemented a build task to solve this issue for our project. Maybe someone also needs this feature, so I posted here:

using System.Linq;
using UnityEditor.Build.Content;
using UnityEditor.Build.Pipeline.Injector;
using UnityEditor.Build.Pipeline.Interfaces;
using UnityEditor.Build.Pipeline.Utilities;
using UnityEngine;

namespace UnityEditor.Build.Pipeline.Tasks
{
    /// <summary>
    /// Calculates the dependency data for all materials.
    /// </summary>
    public class CalculateMaterialDependencyData : IBuildTask
    {
        /// <inheritdoc />
        public int Version { get { return 1; } }

#pragma warning disable 649
        [InjectContext(ContextUsage.In)]
        IBuildParameters m_Parameters;

        [InjectContext(ContextUsage.In)]
        IDependencyData m_DependencyData;

        [InjectContext(ContextUsage.In, true)]
        IBuildCache m_Cache;
#pragma warning restore 649

        /// <inheritdoc />
        public ReturnCode Run()
        {
            if (!m_Parameters.UseCache || m_Cache == null)
                return ReturnCode.SuccessNotRun;

            foreach (AssetLoadInfo dependencyInfo in m_DependencyData.AssetInfo.Values)
            {
                string path = AssetDatabase.GUIDToAssetPath(dependencyInfo.asset);
                if (path.EndsWith(".mat"))
                {
                    var prefabEntries = AssetDatabase.GetDependencies(path).Where(path => path.EndsWith(".shader")).Select(m_Cache.GetCacheEntry);
                    Hash128 shaderDependency = HashingMethods.Calculate(prefabEntries).ToHash128();
                    m_DependencyData.DependencyHash.Add(dependencyInfo.asset, shaderDependency);
                }
            }

            return ReturnCode.Success;
        }
    }
}
 .../Editor/Build/DataBuilders/BuildScriptPackedMode.cs                   | 1 +
1 file changed, 1 insertion(+)

diff --git a/Packages/com.unity.addressables/Editor/Build/DataBuilders/BuildScriptPackedMode.cs b/Packages/com.unity.addressables/Editor/Build/DataBuilders/BuildScriptPackedMode.cs
index 391f6f1..c6ae64d 100644
--- a/Packages/com.unity.addressables/Editor/Build/DataBuilders/BuildScriptPackedMode.cs
+++ b/Packages/com.unity.addressables/Editor/Build/DataBuilders/BuildScriptPackedMode.cs
@@ -1059,6 +1059,7 @@ namespace UnityEditor.AddressableAssets.Build.DataBuilders
             buildTasks.Add(new CreateBuiltInShadersBundle(builtinShaderBundleName));
             if (!string.IsNullOrEmpty(monoScriptBundleName))
                 buildTasks.Add(new CreateMonoScriptBundle(monoScriptBundleName));
+            buildTasks.Add(new CalculateMaterialDependencyData());
             buildTasks.Add(new PostDependencyCallback());
             // Packing

If a shader changes, all assetbundles containing a material which references this shader will be rebuilt. So the changes of the properties in the shader will be applied immediately.