I want to draw line from unityUI. (i dont want to use Line Renderer in unity).
So I figured out the coding below, but my problem is the line sizes are not constant.
public class MyUILineRenderer : Graphic
{
public Vector2[] pointPos;
public float[] angles;
public MyUIGridRenderer gridRenderer;
public Vector2Int gridSize = new Vector2Int(1, 1);
public float lineThickness = 0.5f;
public float width;
public float height;
public float unitWidth;
public float unitHeight;
public float angleHelp;
public bool calAngle;
public Color[] colors;
protected override void OnPopulateMesh(VertexHelper vh)
{
vh.Clear();
width = rectTransform.rect.width;
height = rectTransform.rect.height;
unitWidth = width / (float)gridSize.x;
unitHeight = height / (float)gridSize.y;
if (calAngle)
{
angles = new float[pointPos.Length];
}
if (pointPos.Length < 2)
{
return;
}
float angle = 0;
for (int i = 0; i < pointPos.Length; i++)
{
Vector2 point = pointPos[i];
if (i < pointPos.Length - 1)
{
if (calAngle)
{
angle = angles[i] = -GetAngle(pointPos[i], pointPos[i + 1]) + angleHelp;
}
else
{
angle = angles[i];
}
}
DrawLine(vh, point, angle);
}
int count = pointPos.Length * 2 - 2;
for (int i = 0; i < count; i += 2)
{
vh.AddTriangle(i + 0, i + 1, i + 3);
vh.AddTriangle(i + 0, i + 2, i + 3);
}
}
public float GetAngle(Vector2 currentPos, Vector2 targetPos) {
return (float)(Mathf.Atan2(targetPos.y - currentPos.y, targetPos.x - currentPos.x) * (180 / Mathf.PI));
}
private void DrawLine(VertexHelper vh, Vector2 point, float angle)
{
UIVertex vertex = UIVertex.simpleVert;
vertex.color = colors[0];
vertex.position = Quaternion.Euler(0, 0, angle) * new Vector3(-lineThickness, 0);
vertex.position += new Vector3(unitWidth * point.x, unitHeight * point.y);
vh.AddVert(vertex);
vertex.color = colors[1];
vertex.position = Quaternion.Euler(0, 0, angle) * new Vector3(lineThickness, 0);
vertex.position += new Vector3(unitWidth * point.x, unitHeight * point.y);
vh.AddVert(vertex);
}
}
and when i draw in unity like this
As you guys can see that line thickness is not constant.
How could i make line like picture below?