UILineRender

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UILineRender : Graphic
{
    //  线宽度
    public float thiness = 10;
    //  线路径点
    public List<Vector2> points;

    protected override void OnPopulateMesh(VertexHelper vh)
    {
        //  清空
        vh.Clear();
        //  单点不成线
        if (points.Count < 2)
            return;
        //  绘制
        for(int i = 0; i < points.Count - 1; ++i)
        {
            DrawLine(points[i], points[i + 1], vh);
        }

    }

    //  绘制线
    void DrawLine(Vector2 start, Vector2 end, VertexHelper vh)
    {
        // 走向
        Vector3 dir = end - start;

        //  走向法线
        Vector3 tangent = (Quaternion.Euler(0, 0, 90) * dir).normalized;

        UIVertex vertex = UIVertex.simpleVert;
        vertex.color = color;

        int index = vh.currentVertCount;
        //  创建顶点
        vertex.position = (tangent * thiness / 2) + new Vector3(start.x, start.y, 0);
        vh.AddVert(vertex);

        vertex.position = (-tangent * thiness / 2) + new Vector3(start.x, start.y, 0);
        vh.AddVert(vertex);

        vertex.position = (tangent * thiness / 2) + new Vector3(end.x, end.y, 0);
        vh.AddVert(vertex);

        vertex.position = (-tangent * thiness / 2) + new Vector3(end.x, end.y, 0);
        vh.AddVert(vertex);
        //  创建三角形
        vh.AddTriangle(index, index + 1, index + 3);
        vh.AddTriangle(index + 3, index + 2, index);
        //  不是起点,需要创建连接部分
        if (index != 0)
        {
            vh.AddTriangle(index - 2, index - 1, index + 1);
            vh.AddTriangle(index + 1, index, index - 2);
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 表情是什么,我认为表情就是表现出来的情绪。表情可以传达很多信息。高兴了当然就笑了,难过就哭了。两者是相互影响密不可...
    Persistenc_6aea阅读 126,490评论 2 7
  • 16宿命:用概率思维提高你的胜算 以前的我是风险厌恶者,不喜欢去冒险,但是人生放弃了冒险,也就放弃了无数的可能。 ...
    yichen大刀阅读 6,135评论 0 4