static void OnShaderGraphSaved(Shader shader, object saveContext)
        {
            // In case the shader is not Universal
            if (!(saveContext is UniversalShaderGraphSaveContext universalSaveContext))
            {
                return;
            }

            if (!universalSaveContext.updateMaterials)
            {
                return;
            }

            // Iterate all Materials
            string[] materialGuids = AssetDatabase.FindAssets(kMaterialFilter);
            try
            {
                for (int i = 0, length = materialGuids.Length; i < length; i++)
                {
                    // Only update progress bar every 10 materials
                    if (i % 10 == 9)
                    {
                        EditorUtility.DisplayProgressBar(
                            "Checking material dependencies...",
                            $"{i} / {length} materials.",
                            i / (float)(length - 1));
                    }

                    // Get Material object
                    string   materialPath = AssetDatabase.GUIDToAssetPath(materialGuids[i]);
                    Material material     = AssetDatabase.LoadAssetAtPath <Material>(materialPath);

                    // Reset keywords
                    if ((material != null) && material.shader.name == shader.name)
                    {
                        ShaderUtils.UpdateMaterial(material, ShaderUtils.MaterialUpdateType.ModifiedShader);
                    }

                    material = null;

                    // Free the materials every 200 iterations, on big project loading all materials in memory can lead to a crash
                    if ((i % 200 == 0) && i != 0)
                    {
                        EditorUtility.UnloadUnusedAssetsImmediate(true);
                    }
                }
            }
            finally
            {
                EditorUtility.ClearProgressBar();
                EditorUtility.UnloadUnusedAssetsImmediate(true);
            }
        }
        static void OnShaderGraphSaved(Shader shader, object saveContext)
        {
            // In case the shader is not Universal
            if (!(saveContext is UniversalShaderGraphSaveContext universalSaveContext))
            {
                return;
            }

            if (!universalSaveContext.updateMaterials)
            {
                return;
            }

            // Iterate over all loaded Materials
            Material[] materials = Resources.FindObjectsOfTypeAll <Material>();
            try
            {
                for (int i = 0, length = materials.Length; i < length; i++)
                {
                    // Only update progress bar every 10 materials
                    if (i % 10 == 9)
                    {
                        EditorUtility.DisplayProgressBar(
                            "Checking material dependencies...",
                            $"{i} / {length} materials.",
                            i / (float)(length - 1));
                    }

                    // Reset keywords
                    if (materials[i].shader.name == shader.name)
                    {
                        ShaderUtils.UpdateMaterial(materials[i], ShaderUtils.MaterialUpdateType.ModifiedShader);
                    }
                }
            }
            finally
            {
                EditorUtility.ClearProgressBar();
            }
        }
Example #3
0
        }                                                               // null by now but use VFXURPSubOutput when there is a need to store URP specific data

        public override void SetupMaterial(Material material, bool hasMotionVector = false, bool hasShadowCasting = false, ShaderGraphVfxAsset shaderGraph = null)
        {
            ShaderUtils.UpdateMaterial(material, ShaderUtils.MaterialUpdateType.ModifiedShader);
            material.SetShaderPassEnabled("MotionVectors", hasMotionVector);
            material.SetShaderPassEnabled("ShadowCaster", hasShadowCasting);
        }