What is the appropriate way to set HDRP Material Sorting Priority using C# scripts for transparent materials?
This documentation page only explains setting the render priority of Mesh Renderers via scripting.
After a simple scripting test, here’s what I’ve discovered:
Using the following code:
material.SetFloat("_TransparentSortPriority", 1);
will set only that property, but will not change the actual sorting.
After using that code, if I select a different GameObject, and then the one with the material on it, or If I change the inspector window’s mode to “Debug” and then back to “Normal”, the material will “refresh”, and the renderQueue will also be set to 3001, and the actual sorting will update.
If I use the following code instead:
material.renderQueue = 3001;
renderQueue updates and the actual sorting does get changed.
However, the _TransparentSortPriority doesn’t update, so if I select another GameObject and then select the one with the material on it, or if I change the inspector window’s debug mode and back (causing the material to “refresh”), the actual sorting AND renderQueue reverts back to the original state, because _TransparentSortPriority never changed.
Obviously both of these are not ideal, so I’m using the following:
public Material material;
public int sortingPriority;
[ContextMenu("ApplySortingPriority")]
private void ChangeSortingPriority()
{
material.renderQueue = 3000 + sortingPriority;
material.SetFloat("_TransparentSortPriority", sortingPriority);
}
This seems to handle the problems of using either individually, but surely this is not what the HDRP devs intended? Will this stay the same, or will this change in future versions of HDRP?