EditorScript: Create Transparent Version of Material

Hello everyone,

I have a huge amount of materials in my project. I want to generate a transparent version of each of those materials via an editor script.

// Other function that calls _MatToPreviewMat for each Material I need a transparent version of and saves an asset of it.

internal Material _MatToPreviewMat(Material mat) {
    var previewMat = new Material(mat);
    previewMat.name = $"P{mat.name}";
    previewMat.SetOverrideTag("RenderType", "Transparent");
    previewMat.EnableKeyword("_ALPHAPREMULTIPLY_ON");
    previewMat.EnableKeyword("_SURFACE_TYPE_TRANSPARENT");
    previewMat.renderQueue = 3000;
    previewMat.SetShaderPassEnabled("DepthOnly", false);
    previewMat.SetShaderPassEnabled("SHADOWCASTER", false);
    previewMat.color = mat.color.ChangeA(80 / 255f);
    return previewMat;
}

Unfortuantely this isn’t working. The generated preview material still shows RenderType: Opaque.
Note: Other properties like the color have been overwritten as intended. So it’s not a problem of EditorUtility.SetDirty() or alike.

9526363--1344229--upload_2023-12-13_9-50-53.png

Any ideas?

Context:

  • I use URP in Unity 2022.3.
  • The Materials use the shader “Universal Render Pipeline/Lit”

Kind regards
kfina

Hey Kfinia,

So when I edit the materials, I used this code to make it transparent.

material.SetInt(“_Surface”, 1);
material.SetInt(“_RenderQueueType”, 4);

Not exactly sure what the RenderQueueType is doing in this instance.

Now my issue it that it doesn’t work until you click the created material in the editor. Trying to figure that piece out now.

-Major Batman