I want to be able to draw both solid shapes and lines, much like how Flash has fills and strokes. My dilemma is that I have to decide whether to draw them on a canvas or not. I can draw a solid shape on a canvas using UnityEngine.UI.Graphic like this:
using UnityEngine;
using UnityEngine.UI;
[ExecuteInEditMode]
public class UpArrow : Graphic
{
public float size;
protected override void OnPopulateMesh ( VertexHelper vh )
{
if ( shouldDraw )
{
vh.Clear ();
Vector2 [] vecs = {
new Vector2 ( 0, halfSize ),
new Vector2 ( -halfSize, 0 ),
new Vector2 ( -quarterSize, 0 ),
new Vector2 ( -quarterSize, -halfSize ),
new Vector2 ( quarterSize, -halfSize ),
new Vector2 ( quarterSize, 0 ),
new Vector2 ( halfSize, 0 )
};
for ( int i = 0 ; i < vecs.Length ; i++ )
{
vh.AddVert ( vecs [ i ], color, Vector2.zero );
}
vh.AddTriangle ( 0, 1, 6 );
vh.AddTriangle ( 2, 3, 4 );
vh.AddTriangle ( 4, 5, 2 );
}
}
protected override void Awake ()
{
base.Awake ();
halfSize = size / 2;
quarterSize = size / 4;
}
private bool shouldDraw = true;
private float halfSize;
private float quarterSize;
}
However, if I want to draw lines with UnityEngine.LineRenderer I can’t use a canvas. And any solid shapes I draw using a CanvasRenderer will automatically go on top of any lines.
LineRenderer seems appealing because it gives the option of rounded end caps, which can be used to draw a solid circle if you make a line with its two ends in the same position. Is there any way to get that kind of functionality on a canvas? Or is there any way to get the functionality of Graphic outside of a canvas?