Convert Built-in Materials to HDRP by script

Hello!

I am trying to automate the process of converting a list of built-in materials to HDRP materials in my Unity project in an editor script. The function “Edit/Rendering/Materials/Convert Selected Built-in Materials to HDRP” appears promising and I am trying to find a way to call it.

I have searched the documentation and forums, but have not found a clear solution. Can anyone provide guidance on how to achieve this?

Any help would be greatly appreciated. Thank you in advance!

I couldn’t find a straightforward solution, however I was still able to solve the problem using reflection.

I found that there is a public class called MaterialUpgrader from the HDRP assembly that is designed to upgrade materials. The issue is the deriving classes that actually perform the upgrade process, as well as the method that aggregates them all, have been made private.

Below is the code for the solution I implemented:

using System.Collections.Generic;
using System.Reflection;
using UnityEditor.Rendering;
using UnityEditor.Rendering.HighDefinition;
using UnityEngine;

public static class UpgradeMaterialHelper
{
    public static void UpgradeMaterials(IEnumerable<Material> materials, MaterialUpgrader.UpgradeFlags upgradeFlags)
    {
        List<MaterialUpgrader> upgraders = CustomUpgradeStandardShaderMaterials.GetStandardShaderMaterialUpgraders();

        foreach (var material in materials)
        {
            MaterialUpgrader.Upgrade(material, upgraders, upgradeFlags);
        }
    }
}

public static class CustomUpgradeStandardShaderMaterials
{
    // This is a workaround for the fact that the UpgradeStandardShaderMaterials class is internal
    public static List<MaterialUpgrader> GetStandardShaderMaterialUpgraders()
    {
        System.Type typeRef = typeof(LightmappingHDRP).Assembly.GetType("UnityEditor.Rendering.HighDefinition.UpgradeStandardShaderMaterials");
        MethodInfo method = typeRef.GetMethod("GetHDUpgraders", BindingFlags.Static | BindingFlags.NonPublic);

        return method.Invoke(null, null) as List<MaterialUpgrader>;
    }
}