UI Mesh stays black

I’m trying to draw some of my UI elements with meshes, but they are always drawn black. material.SetColor() does nothing and setting the color of the canvas renderer also doesn’t work. Moreover, the Editor only shows the mesh if look at the UI canvas from the back side. The material I am using is white colored and its shader is the standard one. I also tried some other shades, which either display nothing at all or the same thing. Is this a bug or am I missing something?

using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
[RequireComponent(typeof(CanvasRenderer))]
[RequireComponent(typeof(RectTransform))]
public class UIScalableVertexDrawer : MonoBehaviour
{
    [SerializeField] private Material material;
    [SerializeField] private Color color;
    [SerializeField] private Vector3[] collapsedVertices;
    [SerializeField] private Vector3[] expandedVertices;
    [SerializeField] private int[] triangles;
    [SerializeField] [Range(-1, 2)] private float translationValue = 0;

    private CanvasRenderer canvasRenderer;
    private RectTransform rectTransform;
    private Material newMaterial;
   
    void Awake () {
        canvasRenderer = GetComponent<CanvasRenderer>();
        rectTransform = GetComponent<RectTransform>();
        newMaterial = new Material(material);
        newMaterial.SetColor("_Color", color);
    }
   
    void Update ()
    {
        var vertices = new Vector3[collapsedVertices.Length];
        for (int i = 0; i < Mathf.Min(collapsedVertices.Length, expandedVertices.Length); i++)
        {
            var fromVector = collapsedVertices[i];
            var toVector = expandedVertices[i];

            var difference = toVector - fromVector;
            var newVertexNormalized = fromVector + difference * translationValue;
            var newVertex = new Vector3(newVertexNormalized.x * rectTransform.rect.width, newVertexNormalized.y * rectTransform.rect.height);
            vertices[i] = newVertex;
        }
        var mesh = new Mesh
        {
            vertices = vertices,
            triangles = triangles,
        };
       
        //Apply the new Mesh
        canvasRenderer.Clear();
        canvasRenderer.SetColor(color);
        canvasRenderer.SetMaterial(newMaterial, null);
        canvasRenderer.SetMesh(mesh);
    }
}

Did you find out what to do, I am trying the same thing but I cant get the material to work, just getting a black mesh, or a transparent purple one if I use a UI shader.

Just struggled with a similar problem (I’m using vertex color in my CanvasRenderer, was getting that transparent purple mesh mentioned above), so hopefully this works: Try using color32 instead of color! CanvasRenderer incorrectly does the conversion, so you have to it yourself.

I’m having the same issue, cant seem to solve it. I just get a black mesh.

This is super old, but I was trying to figure this out myself as well which brought me here and then I figured out the cause. So for the sake of helping anyone else stuck on this in the future, the answer for me was simply that my material was using a lit shader. It appears that the material being fed to CanvasRenderer.SetMaterial must be unlit. This means no surface shaders and no built-in standard shaders, which I was attempting to use.