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);
}
}