- Home /
Select material of Canvas Renderer
Hi all. I have a World Space UI canvas which I want to be affected by Sorting Layer/Order in Layer to be rendered in front of meshes. After reading this thread I understood this can be achieved by creating custom shader instead of Default UI shader and assiging it to UI game objects. The problem is that default Canvas Renderer editor doesn't exposing properties at all and I can't simply select material like for Mesh Renderer. So I decided to create my own editor for custom renderer which using its SetMaterial method to update material:
[CustomEditor(typeof(CanvasRenderer))]
internal class CanvasRendererEditor:Editor
{
private CanvasRenderer _canvasRenderer;
private void OnEnable()
{
_canvasRenderer = (CanvasRenderer)target;
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (_canvasRenderer != null)
{
Material currentMaterial= _canvasRenderer.GetMaterial(0);
Material updatedMaterial= EditorGUILayout.ObjectField(currentMaterial, typeof(Material), false) as Material;
if (updatedMaterial != null)
{
_canvasRenderer.SetMaterial(updatedMaterial, 0);
Undo.RecordObject(_canvasRenderer, "Updating material of canvas renderer");
}
}
}
}
Now I can select material: But when I tried to save the scene material immediately reset to default:
What I doing wrong? Thanks in advance.
canvasrenderermaterialselected.png
(13.0 kB)
canvasrenderermaterialreset.png
(11.7 kB)
Comment